From 4e1c41824623d94a79b4aaedc4daf8395991c03f Mon Sep 17 00:00:00 2001 From: Ethan Lane Date: Wed, 22 Sep 2021 19:54:49 +0100 Subject: [PATCH 1/4] Add ability to disable commands --- .env.template | 4 +++- README.md | 11 +++++++---- src/client/util.ts | 9 +++++++++ 3 files changed, 19 insertions(+), 5 deletions(-) diff --git a/.env.template b/.env.template index e27b438..2448e78 100644 --- a/.env.template +++ b/.env.template @@ -10,4 +10,6 @@ BOT_TOKEN= BOT_PREFIX=v! FOLDERS_COMMANDS=commands -FOLDERS_EVENTS=events \ No newline at end of file +FOLDERS_EVENTS=events + +COMMANDS_DISABLED= \ No newline at end of file diff --git a/README.md b/README.md index ccb9235..4f94096 100644 --- a/README.md +++ b/README.md @@ -14,12 +14,15 @@ BOT_PREFIX=v! FOLDERS_COMMANDS=commands FOLDERS_EVENTS=events + +COMMANDS_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. 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..e1e1e58 100644 --- a/src/client/util.ts +++ b/src/client/util.ts @@ -22,6 +22,15 @@ export class Util { message: "Member is not part of message", }; + const disabledCommands = process.env.COMMANDS_DISABLED?.split(','); + + if (disabledCommands?.find(x => x == name)) { + return { + valid: false, + message: "Command is disabled", + }; + } + const folder = process.env.FOLDERS_COMMANDS; if (existsSync(`${process.cwd()}/${folder}/`)) { -- 2.43.4 From a3230cad201b9c60be13eee3815ece49bb548d60 Mon Sep 17 00:00:00 2001 From: Ethan Lane Date: Wed, 22 Sep 2021 19:59:40 +0100 Subject: [PATCH 2/4] Add tests --- tests/client/util.test.ts | 92 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 92 insertions(+) diff --git a/tests/client/util.test.ts b/tests/client/util.test.ts index f99668d..759490f 100644 --- a/tests/client/util.test.ts +++ b/tests/client/util.test.ts @@ -210,6 +210,98 @@ 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', + } + + 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"); + }); + + 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', () => { -- 2.43.4 From 3392e6f0310ba03d17d30b0bfa375354101e28b2 Mon Sep 17 00:00:00 2001 From: Ethan Lane Date: Fri, 24 Sep 2021 18:46:49 +0100 Subject: [PATCH 3/4] Add disabled command message reply --- .env.template | 3 ++- README.md | 2 ++ src/client/client.ts | 1 + src/client/util.ts | 2 ++ 4 files changed, 7 insertions(+), 1 deletion(-) diff --git a/.env.template b/.env.template index 2448e78..e372718 100644 --- a/.env.template +++ b/.env.template @@ -12,4 +12,5 @@ BOT_PREFIX=v! FOLDERS_COMMANDS=commands FOLDERS_EVENTS=events -COMMANDS_DISABLED= \ No newline at end of file +COMMANDS_DISABLED= +COMMANDS_DISABLED_MESSAGE=This command is disabled. \ No newline at end of file diff --git a/README.md b/README.md index 4f94096..68fa801 100644 --- a/README.md +++ b/README.md @@ -16,6 +16,7 @@ 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. @@ -23,6 +24,7 @@ COMMANDS_DISABLED= * **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/client.ts b/src/client/client.ts index 9337496..2eb5a37 100644 --- a/src/client/client.ts +++ b/src/client/client.ts @@ -21,6 +21,7 @@ export class CoreClient extends Client { if (!process.env.BOT_PREFIX) throw "BOT_PREFIX is not defined in .env"; if (!process.env.FOLDERS_COMMANDS) throw "FOLDERS_COMMANDS is not defined in .env"; if (!process.env.FOLDERS_EVENTS) throw "FOLDERS_EVENTS is not defined in .env"; + if (!process.env.COMMANDS_DISABLED_MESSAGE) throw "COMMANDS_DISABLED_MESSAGE is not defined in .env"; super.on("message", this._events.onMessage); super.on("ready", this._events.onReady); diff --git a/src/client/util.ts b/src/client/util.ts index e1e1e58..bc675e0 100644 --- a/src/client/util.ts +++ b/src/client/util.ts @@ -25,6 +25,8 @@ export class Util { 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", -- 2.43.4 From b10ac370079f8d75ba1adca008f727c988a94987 Mon Sep 17 00:00:00 2001 From: Ethan Lane Date: Fri, 24 Sep 2021 18:52:12 +0100 Subject: [PATCH 4/4] Update tests --- src/client/client.ts | 1 - tests/client/util.test.ts | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/src/client/client.ts b/src/client/client.ts index 2eb5a37..9337496 100644 --- a/src/client/client.ts +++ b/src/client/client.ts @@ -21,7 +21,6 @@ export class CoreClient extends Client { if (!process.env.BOT_PREFIX) throw "BOT_PREFIX is not defined in .env"; if (!process.env.FOLDERS_COMMANDS) throw "FOLDERS_COMMANDS is not defined in .env"; if (!process.env.FOLDERS_EVENTS) throw "FOLDERS_EVENTS is not defined in .env"; - if (!process.env.COMMANDS_DISABLED_MESSAGE) throw "COMMANDS_DISABLED_MESSAGE is not defined in .env"; super.on("message", this._events.onMessage); super.on("ready", this._events.onReady); diff --git a/tests/client/util.test.ts b/tests/client/util.test.ts index 759490f..9787ed4 100644 --- a/tests/client/util.test.ts +++ b/tests/client/util.test.ts @@ -218,6 +218,7 @@ describe('LoadCommand', () => { FOLDERS_COMMANDS: 'commands', FOLDERS_EVENTS: 'events', COMMANDS_DISABLED: 'normal', + COMMANDS_DISABLED_MESSAGE: 'disabled', } process.cwd = jest.fn().mockReturnValue("../../tests/__mocks"); @@ -233,6 +234,8 @@ describe('LoadCommand', () => { }, reply: jest.fn(), } as unknown as Message; + + const messageReply = jest.spyOn(message, 'reply'); const util = new Util(); @@ -240,6 +243,41 @@ describe('LoadCommand', () => { 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', () => { -- 2.43.4