diff --git a/.env.template b/.env.template index e27b438..e372718 100644 --- a/.env.template +++ b/.env.template @@ -10,4 +10,7 @@ BOT_TOKEN= BOT_PREFIX=v! FOLDERS_COMMANDS=commands -FOLDERS_EVENTS=events \ No newline at end of file +FOLDERS_EVENTS=events + +COMMANDS_DISABLED= +COMMANDS_DISABLED_MESSAGE=This command is disabled. \ No newline at end of file diff --git a/README.md b/README.md index ccb9235..68fa801 100644 --- a/README.md +++ b/README.md @@ -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! diff --git a/src/client/util.ts b/src/client/util.ts index 311ec00..bc675e0 100644 --- a/src/client/util.ts +++ b/src/client/util.ts @@ -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 { + valid: false, + message: "Command is disabled", + }; + } + const folder = process.env.FOLDERS_COMMANDS; if (existsSync(`${process.cwd()}/${folder}/`)) { diff --git a/tests/client/util.test.ts b/tests/client/util.test.ts index f99668d..9787ed4 100644 --- a/tests/client/util.test.ts +++ b/tests/client/util.test.ts @@ -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', () => {