Feature/3 disable commands #50

Merged
Vylpes merged 4 commits from feature/3-disable-commands into develop 2021-09-24 19:00:08 +01:00
4 changed files with 154 additions and 5 deletions

View file

@ -11,3 +11,6 @@ BOT_PREFIX=v!
FOLDERS_COMMANDS=commands
FOLDERS_EVENTS=events
COMMANDS_DISABLED=
COMMANDS_DISABLED_MESSAGE=This command is disabled.

View file

@ -14,12 +14,17 @@ BOT_PREFIX=v!
FOLDERS_COMMANDS=commands
FOLDERS_EVENTS=events
COMMANDS_DISABLED=
COMMANDS_DISABLED_MESSAGE=This command is disabled.
```
* **BOT_TOKEN:** Your bot's token, replace {TOKEN} with your bot token
* **BOT_PREFIX** The command prefix
* **FOLDERS_COMMANDS:** The folder which contains your commands
* **FOLDERS_EVENTS** The folder which contains your events
* **BOT_TOKEN:** Your bot's token, replace {TOKEN} with your bot token.
* **BOT_PREFIX:** The command prefix.
* **FOLDERS_COMMANDS:** The folder which contains your commands.
* **FOLDERS_EVENTS:** The folder which contains your events.
* **COMMANDS_DISABLED:** List of command file names that won't run, separated by commas.
* **COMMANDS_DISABLED_MESSAGE:** The message which is replied to the user who tries to run a disabled command.
Make sure that you **DO NOT** put your .env file into VCS!

View file

@ -22,6 +22,17 @@ export class Util {
message: "Member is not part of message",
};
const disabledCommands = process.env.COMMANDS_DISABLED?.split(',');
if (disabledCommands?.find(x => x == name)) {
message.reply(process.env.COMMANDS_DISABLED_MESSAGE || "This command is disabled.");
return {
VylpesTester commented 2021-09-24 18:13:59 +01:00 (Migrated from github.com)
Review

When this returns, what it should do is reply to the user also, to say its disabled

When this returns, what it should do is reply to the user also, to say its disabled
valid: false,
message: "Command is disabled",
};
}
const folder = process.env.FOLDERS_COMMANDS;
if (existsSync(`${process.cwd()}/${folder}/`)) {

View file

@ -210,6 +210,136 @@ describe('LoadCommand', () => {
expect(result.valid).toBeTruthy();
});
test('Given command is set to disabled, Expect command to not fire', () => {
process.env = {
BOT_TOKEN: 'TOKEN',
BOT_PREFIX: '!',
FOLDERS_COMMANDS: 'commands',
FOLDERS_EVENTS: 'events',
COMMANDS_DISABLED: 'normal',
COMMANDS_DISABLED_MESSAGE: 'disabled',
}
process.cwd = jest.fn().mockReturnValue("../../tests/__mocks");
fs.existsSync = jest.fn().mockReturnValue(true);
const message = {
member: {
roles: {
cache: {
find: jest.fn().mockReturnValue(true),
}
},
},
reply: jest.fn(),
} as unknown as Message;
const messageReply = jest.spyOn(message, 'reply');
const util = new Util();
const result = util.loadCommand("normal", [ "first" ], message);
expect(result.valid).toBeFalsy();
expect(result.message).toBe("Command is disabled");
expect(messageReply).toBeCalledWith("disabled");
});
test('Given command COMMANDS_DISABLED_MESSAGE is empty, Expect default message sent', () => {
process.env = {
BOT_TOKEN: 'TOKEN',
BOT_PREFIX: '!',
FOLDERS_COMMANDS: 'commands',
FOLDERS_EVENTS: 'events',
COMMANDS_DISABLED: 'normal',
}
process.cwd = jest.fn().mockReturnValue("../../tests/__mocks");
fs.existsSync = jest.fn().mockReturnValue(true);
const message = {
member: {
roles: {
cache: {
find: jest.fn().mockReturnValue(true),
}
},
},
reply: jest.fn(),
} as unknown as Message;
const messageReply = jest.spyOn(message, 'reply');
const util = new Util();
const result = util.loadCommand("normal", [ "first" ], message);
expect(result.valid).toBeFalsy();
expect(result.message).toBe("Command is disabled");
expect(messageReply).toBeCalledWith("This command is disabled.");
});
test('Given a different command is disabled, Expect command to still fire', () => {
process.env = {
BOT_TOKEN: 'TOKEN',
BOT_PREFIX: '!',
FOLDERS_COMMANDS: 'commands',
FOLDERS_EVENTS: 'events',
COMMANDS_DISABLED: 'anything',
}
process.cwd = jest.fn().mockReturnValue("../../tests/__mocks");
fs.existsSync = jest.fn().mockReturnValue(true);
const message = {
member: {
roles: {
cache: {
find: jest.fn().mockReturnValue(true),
}
},
},
reply: jest.fn(),
} as unknown as Message;
const util = new Util();
const result = util.loadCommand("normal", [ "first" ], message);
expect(result.valid).toBeTruthy();
});
test('Given a different command is disabled with this one, Expect command to not fire', () => {
process.env = {
BOT_TOKEN: 'TOKEN',
BOT_PREFIX: '!',
FOLDERS_COMMANDS: 'commands',
FOLDERS_EVENTS: 'events',
COMMANDS_DISABLED: 'normal,anything,',
}
process.cwd = jest.fn().mockReturnValue("../../tests/__mocks");
fs.existsSync = jest.fn().mockReturnValue(true);
const message = {
member: {
roles: {
cache: {
find: jest.fn().mockReturnValue(true),
}
},
},
reply: jest.fn(),
} as unknown as Message;
const util = new Util();
const result = util.loadCommand("normal", [ "first" ], message);
expect(result.valid).toBeFalsy();
expect(result.message).toBe("Command is disabled");
});
});
describe('LoadEvents', () => {