From 4e1c41824623d94a79b4aaedc4daf8395991c03f Mon Sep 17 00:00:00 2001 From: Ethan Lane Date: Wed, 22 Sep 2021 19:54:49 +0100 Subject: [PATCH 01/13] 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}/`)) { From a3230cad201b9c60be13eee3815ece49bb548d60 Mon Sep 17 00:00:00 2001 From: Ethan Lane Date: Wed, 22 Sep 2021 19:59:40 +0100 Subject: [PATCH 02/13] 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', () => { From 3392e6f0310ba03d17d30b0bfa375354101e28b2 Mon Sep 17 00:00:00 2001 From: Ethan Lane Date: Fri, 24 Sep 2021 18:46:49 +0100 Subject: [PATCH 03/13] 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", From b10ac370079f8d75ba1adca008f727c988a94987 Mon Sep 17 00:00:00 2001 From: Ethan Lane Date: Fri, 24 Sep 2021 18:52:12 +0100 Subject: [PATCH 04/13] 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', () => { From 00a057792b3bf449c0ceb235046e3b7e881541ee Mon Sep 17 00:00:00 2001 From: Ethan Lane Date: Sun, 26 Sep 2021 14:12:21 +0100 Subject: [PATCH 05/13] Make command loader use the default class --- src/client/events.ts | 4 ++-- src/client/util.ts | 8 ++++---- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/client/events.ts b/src/client/events.ts index 417c24d..53d7448 100644 --- a/src/client/events.ts +++ b/src/client/events.ts @@ -20,7 +20,7 @@ export class Events { // Emit when a message is sent // Used to check for commands - public onMessage(message: Message): IEventResponse { + public async onMessage(message: Message): Promise { if (!message.guild) return { valid: false, message: "Message was not sent in a guild, ignoring.", @@ -42,7 +42,7 @@ export class Events { message: "Command name was not found", }; - const res = this._util.loadCommand(name, args, message); + const res = await this._util.loadCommand(name, args, message); if (!res.valid) { return { diff --git a/src/client/util.ts b/src/client/util.ts index bc675e0..9046a8b 100644 --- a/src/client/util.ts +++ b/src/client/util.ts @@ -16,7 +16,7 @@ export interface IUtilResponse extends IBaseResponse { // Util Class export class Util { - public loadCommand(name: string, args: string[], message: Message): IUtilResponse { + public async loadCommand(name: string, args: string[], message: Message): Promise { if (!message.member) return { valid: false, message: "Member is not part of message", @@ -37,9 +37,9 @@ export class Util { if (existsSync(`${process.cwd()}/${folder}/`)) { if (existsSync(`${process.cwd()}/${folder}/${name}.ts`)) { - const commandFile = require(`${process.cwd()}/${folder}/${name}.ts`); - const command = new commandFile[name]() as Command; - + const commandFile = require(`${process.cwd()}/${folder}/${name}.ts`).default; + const command = new commandFile() as Command; + const requiredRoles = command._roles; if (!command._category) command._category = "none"; From 3a9f1965ae7e2d6db9e6ccbd9a08bc77696d1cf5 Mon Sep 17 00:00:00 2001 From: Ethan Lane Date: Sun, 26 Sep 2021 14:13:09 +0100 Subject: [PATCH 06/13] Update documentation --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 68fa801..96076be 100644 --- a/README.md +++ b/README.md @@ -50,7 +50,7 @@ The code below will reply to the user with 'PONG' when they type {PREFIX}ping import { Command, ICommandContext } from "vylbot-core"; -export class Ping extends Command { +export default class Ping extends Command { constructor() { super(); this._roles = [ "Moderator" ]; From 799b04e936c0f6fe7df9e0eda5feecf05d143d52 Mon Sep 17 00:00:00 2001 From: Ethan Lane Date: Sun, 26 Sep 2021 14:32:56 +0100 Subject: [PATCH 07/13] Update tests --- tests/__mocks/commands/noCategory.ts | 4 +-- tests/__mocks/commands/normal.ts | 4 +-- tests/__mocks/commands/roles.ts | 4 +-- tests/client/events.test.ts | 26 ++++++++-------- tests/client/util.test.ts | 46 ++++++++++++++-------------- 5 files changed, 42 insertions(+), 42 deletions(-) diff --git a/tests/__mocks/commands/noCategory.ts b/tests/__mocks/commands/noCategory.ts index 9d6d973..e71cd53 100644 --- a/tests/__mocks/commands/noCategory.ts +++ b/tests/__mocks/commands/noCategory.ts @@ -1,7 +1,7 @@ import { Command } from "../../../src/type/command"; -export class noCategory extends Command { +export default class noCategory extends Command { constructor() { super(); } -} \ No newline at end of file +} diff --git a/tests/__mocks/commands/normal.ts b/tests/__mocks/commands/normal.ts index 03695f4..3e3a74f 100644 --- a/tests/__mocks/commands/normal.ts +++ b/tests/__mocks/commands/normal.ts @@ -1,8 +1,8 @@ import { Command } from "../../../src/type/command"; -export class normal extends Command { +export default class normal extends Command { constructor() { super(); this._category = "General"; } -} \ No newline at end of file +} diff --git a/tests/__mocks/commands/roles.ts b/tests/__mocks/commands/roles.ts index d9343c7..1e0321e 100644 --- a/tests/__mocks/commands/roles.ts +++ b/tests/__mocks/commands/roles.ts @@ -1,8 +1,8 @@ import { Command } from "../../../src/type/command"; -export class roles extends Command { +export default class roles extends Command { constructor() { super(); this._roles = [ "Moderator" ]; } -} \ No newline at end of file +} diff --git a/tests/client/events.test.ts b/tests/client/events.test.ts index 2c9ad44..69baac9 100644 --- a/tests/client/events.test.ts +++ b/tests/client/events.test.ts @@ -10,7 +10,7 @@ beforeEach(() => { }); describe('OnMessage', () => { - test('Given Message Is Valid Expect Message Sent', () => { + test('Given Message Is Valid Expect Message Sent', async () => { process.env = { BOT_TOKEN: 'TOKEN', BOT_PREFIX: '!', @@ -30,7 +30,7 @@ describe('OnMessage', () => { const events = new Events(); - const result = events.onMessage(message); + const result = await events.onMessage(message); expect(result.valid).toBeTruthy(); @@ -41,7 +41,7 @@ describe('OnMessage', () => { expect(result.context?.message).toBe(message); }); - test('Given Guild Is Null, Expect Failed Result', () => { + test('Given Guild Is Null, Expect Failed Result', async () => { process.env = { BOT_TOKEN: 'TOKEN', BOT_PREFIX: '!', @@ -61,13 +61,13 @@ describe('OnMessage', () => { const events = new Events(); - const result = events.onMessage(message); + const result = await events.onMessage(message); expect(result.valid).toBeFalsy(); expect(result.message).toBe("Message was not sent in a guild, ignoring."); }); - test('Given Author Is A Bot, Expect Failed Result', () => { + test('Given Author Is A Bot, Expect Failed Result', async () => { process.env = { BOT_TOKEN: 'TOKEN', BOT_PREFIX: '!', @@ -87,13 +87,13 @@ describe('OnMessage', () => { const events = new Events(); - const result = events.onMessage(message); + const result = await events.onMessage(message); expect(result.valid).toBeFalsy(); expect(result.message).toBe("Message was sent by a bot, ignoring."); }); - test('Given Message Content Was Not A Command, Expect Failed Result', () => { + test('Given Message Content Was Not A Command, Expect Failed Result', async () => { process.env = { BOT_TOKEN: 'TOKEN', BOT_PREFIX: '!', @@ -113,13 +113,13 @@ describe('OnMessage', () => { const events = new Events(); - const result = events.onMessage(message); + const result = await events.onMessage(message); expect(result.valid).toBeFalsy(); expect(result.message).toBe("Message was not a command, ignoring."); }); - test('Given Message Had No Command Name, Expect Failed Result', () => { + test('Given Message Had No Command Name, Expect Failed Result', async () => { process.env = { BOT_TOKEN: 'TOKEN', BOT_PREFIX: '!', @@ -139,13 +139,13 @@ describe('OnMessage', () => { const events = new Events(); - const result = events.onMessage(message); + const result = await events.onMessage(message); expect(result.valid).toBeFalsy(); expect(result.message).toBe("Command name was not found"); }); - test('Given Command Failed To Execute, Expect Failed Result', () => { + test('Given Command Failed To Execute, Expect Failed Result', async () => { process.env = { BOT_TOKEN: 'TOKEN', BOT_PREFIX: '!', @@ -165,7 +165,7 @@ describe('OnMessage', () => { const events = new Events(); - const result = events.onMessage(message); + const result = await events.onMessage(message); expect(result.valid).toBeFalsy(); expect(result.message).toBe("Command failed"); @@ -182,4 +182,4 @@ describe('OnReady', () => { expect(console.log).toBeCalledWith("Ready"); }); -}); \ No newline at end of file +}); diff --git a/tests/client/util.test.ts b/tests/client/util.test.ts index 9787ed4..c79e15d 100644 --- a/tests/client/util.test.ts +++ b/tests/client/util.test.ts @@ -10,7 +10,7 @@ beforeEach(() => { }); describe('LoadCommand', () => { - test('Given Successful Exection, Expect Successful Result', () => { + test('Given Successful Exection, Expect Successful Result', async () => { process.env = { BOT_TOKEN: 'TOKEN', BOT_PREFIX: '!', @@ -34,12 +34,12 @@ describe('LoadCommand', () => { const util = new Util(); - const result = util.loadCommand("normal", [ "first" ], message); + const result = await util.loadCommand("normal", [ "first" ], message); expect(result.valid).toBeTruthy(); }); - test('Given Member Is Null, Expect Failed Result', () => { + test('Given Member Is Null, Expect Failed Result', async () => { process.env = { BOT_TOKEN: 'TOKEN', BOT_PREFIX: '!', @@ -56,13 +56,13 @@ describe('LoadCommand', () => { const util = new Util(); - const result = util.loadCommand("normal", [ "first" ], message); + const result = await util.loadCommand("normal", [ "first" ], message); expect(result.valid).toBeFalsy(); expect(result.message).toBe("Member is not part of message"); }); - test('Given Folder Does Not Exist, Expect Failed Result', () => { + test('Given Folder Does Not Exist, Expect Failed Result', async () => { process.env = { BOT_TOKEN: 'TOKEN', BOT_PREFIX: '!', @@ -86,13 +86,13 @@ describe('LoadCommand', () => { const util = new Util(); - const result = util.loadCommand("normal", [ "first" ], message); + const result = await util.loadCommand("normal", [ "first" ], message); expect(result.valid).toBeFalsy(); expect(result.message).toBe("Command folder does not exist"); }); - test('Given File Does Not Exist, Expect Failed Result', () => { + test('Given File Does Not Exist, Expect Failed Result', async () => { process.env = { BOT_TOKEN: 'TOKEN', BOT_PREFIX: '!', @@ -117,13 +117,13 @@ describe('LoadCommand', () => { const util = new Util(); - const result = util.loadCommand("normal", [ "first" ], message); + const result = await util.loadCommand("normal", [ "first" ], message); expect(result.valid).toBeFalsy(); expect(result.message).toBe("File does not exist"); }); - test('Given User Does Have Role, Expect Successful Result', () => { + test('Given User Does Have Role, Expect Successful Result', async () => { process.env = { BOT_TOKEN: 'TOKEN', BOT_PREFIX: '!', @@ -147,12 +147,12 @@ describe('LoadCommand', () => { const util = new Util(); - const result = util.loadCommand("roles", [ "first" ], message); + const result = await util.loadCommand("roles", [ "first" ], message); expect(result.valid).toBeTruthy(); }); - test('Given User Does Not Have Role, Expect Failed Result', () => { + test('Given User Does Not Have Role, Expect Failed Result', async () => { process.env = { BOT_TOKEN: 'TOKEN', BOT_PREFIX: '!', @@ -176,13 +176,13 @@ describe('LoadCommand', () => { const util = new Util(); - const result = util.loadCommand("roles", [ "first" ], message); + const result = await util.loadCommand("roles", [ "first" ], message); expect(result.valid).toBeFalsy(); expect(result.message).toBe("You require the `Moderator` role to run this command"); }); - test('Given Command Category Is Null, Expect Successful Result', () => { + test('Given Command Category Is Null, Expect Successful Result', async () => { process.env = { BOT_TOKEN: 'TOKEN', BOT_PREFIX: '!', @@ -206,12 +206,12 @@ describe('LoadCommand', () => { const util = new Util(); - const result = util.loadCommand("noCategory", [ "first" ], message); + const result = await util.loadCommand("noCategory", [ "first" ], message); expect(result.valid).toBeTruthy(); }); - test('Given command is set to disabled, Expect command to not fire', () => { + test('Given command is set to disabled, Expect command to not fire', async () => { process.env = { BOT_TOKEN: 'TOKEN', BOT_PREFIX: '!', @@ -239,14 +239,14 @@ describe('LoadCommand', () => { const util = new Util(); - const result = util.loadCommand("normal", [ "first" ], message); + const result = await 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', () => { + test('Given command COMMANDS_DISABLED_MESSAGE is empty, Expect default message sent', async () => { process.env = { BOT_TOKEN: 'TOKEN', BOT_PREFIX: '!', @@ -273,14 +273,14 @@ describe('LoadCommand', () => { const util = new Util(); - const result = util.loadCommand("normal", [ "first" ], message); + const result = await 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', () => { + test('Given a different command is disabled, Expect command to still fire', async () => { process.env = { BOT_TOKEN: 'TOKEN', BOT_PREFIX: '!', @@ -305,12 +305,12 @@ describe('LoadCommand', () => { const util = new Util(); - const result = util.loadCommand("normal", [ "first" ], message); + const result = await util.loadCommand("normal", [ "first" ], message); expect(result.valid).toBeTruthy(); }); - test('Given a different command is disabled with this one, Expect command to not fire', () => { + test('Given a different command is disabled with this one, Expect command to not fire', async () => { process.env = { BOT_TOKEN: 'TOKEN', BOT_PREFIX: '!', @@ -335,7 +335,7 @@ describe('LoadCommand', () => { const util = new Util(); - const result = util.loadCommand("normal", [ "first" ], message); + const result = await util.loadCommand("normal", [ "first" ], message); expect(result.valid).toBeFalsy(); expect(result.message).toBe("Command is disabled"); @@ -418,4 +418,4 @@ describe('LoadEvents', () => { expect(result.valid).toBeFalsy(); expect(result.message).toBe("Event folder does not exist"); }); -}); \ No newline at end of file +}); From 5ce09606068a341a53d022559f77715e1de5bf51 Mon Sep 17 00:00:00 2001 From: Ethan Lane Date: Wed, 29 Sep 2021 17:52:21 +0100 Subject: [PATCH 08/13] Remove useless async code --- src/client/events.ts | 4 ++-- src/client/util.ts | 2 +- tests/client/util.test.ts | 44 +++++++++++++++++++-------------------- 3 files changed, 25 insertions(+), 25 deletions(-) diff --git a/src/client/events.ts b/src/client/events.ts index 53d7448..417c24d 100644 --- a/src/client/events.ts +++ b/src/client/events.ts @@ -20,7 +20,7 @@ export class Events { // Emit when a message is sent // Used to check for commands - public async onMessage(message: Message): Promise { + public onMessage(message: Message): IEventResponse { if (!message.guild) return { valid: false, message: "Message was not sent in a guild, ignoring.", @@ -42,7 +42,7 @@ export class Events { message: "Command name was not found", }; - const res = await this._util.loadCommand(name, args, message); + const res = this._util.loadCommand(name, args, message); if (!res.valid) { return { diff --git a/src/client/util.ts b/src/client/util.ts index 9046a8b..428976e 100644 --- a/src/client/util.ts +++ b/src/client/util.ts @@ -16,7 +16,7 @@ export interface IUtilResponse extends IBaseResponse { // Util Class export class Util { - public async loadCommand(name: string, args: string[], message: Message): Promise { + public loadCommand(name: string, args: string[], message: Message): IUtilResponse { if (!message.member) return { valid: false, message: "Member is not part of message", diff --git a/tests/client/util.test.ts b/tests/client/util.test.ts index c79e15d..fe046e2 100644 --- a/tests/client/util.test.ts +++ b/tests/client/util.test.ts @@ -10,7 +10,7 @@ beforeEach(() => { }); describe('LoadCommand', () => { - test('Given Successful Exection, Expect Successful Result', async () => { + test('Given Successful Exection, Expect Successful Result', () => { process.env = { BOT_TOKEN: 'TOKEN', BOT_PREFIX: '!', @@ -34,12 +34,12 @@ describe('LoadCommand', () => { const util = new Util(); - const result = await util.loadCommand("normal", [ "first" ], message); + const result = util.loadCommand("normal", [ "first" ], message); expect(result.valid).toBeTruthy(); }); - test('Given Member Is Null, Expect Failed Result', async () => { + test('Given Member Is Null, Expect Failed Result', () => { process.env = { BOT_TOKEN: 'TOKEN', BOT_PREFIX: '!', @@ -56,13 +56,13 @@ describe('LoadCommand', () => { const util = new Util(); - const result = await util.loadCommand("normal", [ "first" ], message); + const result = util.loadCommand("normal", [ "first" ], message); expect(result.valid).toBeFalsy(); expect(result.message).toBe("Member is not part of message"); }); - test('Given Folder Does Not Exist, Expect Failed Result', async () => { + test('Given Folder Does Not Exist, Expect Failed Result', () => { process.env = { BOT_TOKEN: 'TOKEN', BOT_PREFIX: '!', @@ -86,13 +86,13 @@ describe('LoadCommand', () => { const util = new Util(); - const result = await util.loadCommand("normal", [ "first" ], message); + const result = util.loadCommand("normal", [ "first" ], message); expect(result.valid).toBeFalsy(); expect(result.message).toBe("Command folder does not exist"); }); - test('Given File Does Not Exist, Expect Failed Result', async () => { + test('Given File Does Not Exist, Expect Failed Result', () => { process.env = { BOT_TOKEN: 'TOKEN', BOT_PREFIX: '!', @@ -117,13 +117,13 @@ describe('LoadCommand', () => { const util = new Util(); - const result = await util.loadCommand("normal", [ "first" ], message); + const result = util.loadCommand("normal", [ "first" ], message); expect(result.valid).toBeFalsy(); expect(result.message).toBe("File does not exist"); }); - test('Given User Does Have Role, Expect Successful Result', async () => { + test('Given User Does Have Role, Expect Successful Result', () => { process.env = { BOT_TOKEN: 'TOKEN', BOT_PREFIX: '!', @@ -147,12 +147,12 @@ describe('LoadCommand', () => { const util = new Util(); - const result = await util.loadCommand("roles", [ "first" ], message); + const result = util.loadCommand("roles", [ "first" ], message); expect(result.valid).toBeTruthy(); }); - test('Given User Does Not Have Role, Expect Failed Result', async () => { + test('Given User Does Not Have Role, Expect Failed Result', () => { process.env = { BOT_TOKEN: 'TOKEN', BOT_PREFIX: '!', @@ -176,13 +176,13 @@ describe('LoadCommand', () => { const util = new Util(); - const result = await util.loadCommand("roles", [ "first" ], message); + const result = util.loadCommand("roles", [ "first" ], message); expect(result.valid).toBeFalsy(); expect(result.message).toBe("You require the `Moderator` role to run this command"); }); - test('Given Command Category Is Null, Expect Successful Result', async () => { + test('Given Command Category Is Null, Expect Successful Result', () => { process.env = { BOT_TOKEN: 'TOKEN', BOT_PREFIX: '!', @@ -206,12 +206,12 @@ describe('LoadCommand', () => { const util = new Util(); - const result = await util.loadCommand("noCategory", [ "first" ], message); + const result = util.loadCommand("noCategory", [ "first" ], message); expect(result.valid).toBeTruthy(); }); - test('Given command is set to disabled, Expect command to not fire', async () => { + test('Given command is set to disabled, Expect command to not fire', () => { process.env = { BOT_TOKEN: 'TOKEN', BOT_PREFIX: '!', @@ -239,14 +239,14 @@ describe('LoadCommand', () => { const util = new Util(); - const result = await util.loadCommand("normal", [ "first" ], message); + 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', async () => { + test('Given command COMMANDS_DISABLED_MESSAGE is empty, Expect default message sent', () => { process.env = { BOT_TOKEN: 'TOKEN', BOT_PREFIX: '!', @@ -273,14 +273,14 @@ describe('LoadCommand', () => { const util = new Util(); - const result = await util.loadCommand("normal", [ "first" ], message); + 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', async () => { + test('Given a different command is disabled, Expect command to still fire', () => { process.env = { BOT_TOKEN: 'TOKEN', BOT_PREFIX: '!', @@ -305,12 +305,12 @@ describe('LoadCommand', () => { const util = new Util(); - const result = await util.loadCommand("normal", [ "first" ], message); + 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', async () => { + test('Given a different command is disabled with this one, Expect command to not fire', () => { process.env = { BOT_TOKEN: 'TOKEN', BOT_PREFIX: '!', @@ -335,7 +335,7 @@ describe('LoadCommand', () => { const util = new Util(); - const result = await util.loadCommand("normal", [ "first" ], message); + const result = util.loadCommand("normal", [ "first" ], message); expect(result.valid).toBeFalsy(); expect(result.message).toBe("Command is disabled"); From 1a8baecfa13254cc4a3253ede9eb09e18f04f175 Mon Sep 17 00:00:00 2001 From: Ethan Lane Date: Mon, 4 Oct 2021 19:43:53 +0100 Subject: [PATCH 09/13] Improvements --- .gitignore | 3 +++ CODE_OF_CONDUCT.md | 2 +- CONTRIBUTING.md | 50 ++++++++++++++++++++++------------------------ package.json | 5 +++-- 4 files changed, 31 insertions(+), 29 deletions(-) diff --git a/.gitignore b/.gitignore index 948756c..e436cc5 100644 --- a/.gitignore +++ b/.gitignore @@ -113,3 +113,6 @@ events/ # Linux Environment Files *.swp + +# macOS Environment Files +.DS_Store \ No newline at end of file diff --git a/CODE_OF_CONDUCT.md b/CODE_OF_CONDUCT.md index 6c4a60e..743fe01 100644 --- a/CODE_OF_CONDUCT.md +++ b/CODE_OF_CONDUCT.md @@ -61,7 +61,7 @@ representative at an online or offline event. Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at -getgravitysoftware@gmail.com. +ethan@vylpes.com. All complaints will be reviewed and investigated promptly and fairly. All community leaders are obligated to respect the privacy and security of the diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 9720b10..2e2d3cb 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -12,7 +12,7 @@ This project and everyone participating in it is governed by the VylBot Core Cod > **Note:** Please don't file an issue to ask a question. You'll get faster results by using the resources below. -You can ask a question about the project by emailing us at `getgravitysoftware@gmail.com`. +You can ask a question about the project by emailing us at `ethan@vylpes.com`. You can ask a question about the project in the `#development` channel in the [Discord Server](https://discord.gg/UyAhAVp). @@ -32,7 +32,7 @@ There are a few conventions that have developed over time for this project. When We won't accept pull requests unless these checks pass. If yours fail, simply fix what the bot says until it passes and then get a repo member to review your code. -The rules for the code is based upon [Gravity Software's Config Repo](https://github.com/GetGravitySoft/Config) +The rules for the code is based upon [Vylpes' Config Repo](https://github.com/vylpes/Config) * Variable names should use **Camel Case** * Functions should put **braces on the same line** @@ -119,26 +119,26 @@ Enhancement suggestions are tracked as GitHub issues. After you've determined th Unsure where to begin contributing to VylBot Core? You can start by looking through these `good first` and `help wanted` issues: -* [Good first issue](https://github.com/getgravitysoft/vylbot-core/labels/good%20first%20issue) - issues which should only require a few lines of code, and a test or two. -* [Help wanted](https://github.com/getgravitysoft/vylbot-core/labels/help%20wanted) - issues which should be a bit more involved than `good first` issues. +* [Good first issue](https://github.com/vylpes/vylbot-core/labels/good%20first%20issue) - issues which should only require a few lines of code, and a test or two. +* [Help wanted](https://github.com/vylpes/vylbot-core/labels/help%20wanted) - issues which should be a bit more involved than `good first` issues. #### Prerequisites -In order to download necessary tools, clone the repository, and install dependencies via `npm` you need network access. +In order to download necessary tools, clone the repository, and install dependencies via `yarn` you need network access. You'll need the following tools: * Git * NodeJS -Install and build all of the dependencies using `npm` +Install and build all of the dependencies using `yarn` ```bash -cd VylBot Core -npm install -cp config.json.template config.json +cd vylbot-core +yarn install +cp .env.template .env ``` -And then use your text editor of choice to fill in the `config.json` file. +And then use your text editor of choice to fill in the `.env` file. #### Build and Run @@ -147,15 +147,15 @@ If you want to understand how VylBot Core works or want to debug an issue, you'l First, fork the VylBot Core repository so that you can make a pull request. Then, clone your fork locally: ```bash -git clone https://github.com//VylBot Core.git +git clone https://github.com//vylbot-core.git ``` Occasionally, you will want to merge changes in the upstream repository (the official code repo) with your fork. ```bash -cd VylBot Core +cd vylbot-core git checkout master -git pull https://github.com/getgravitysoft/VylBot Core.git master +git pull https://github.com/vylpes/vylbot-core.git master ``` Manage any merge conflicts, commit them, and then push them to your fork. @@ -202,20 +202,18 @@ As well as eslint's recommended defaults. Example -```js -function ban (member) { - let reason = "Example reason"; +```ts +// hello.ts +import { Command, ICommandContext } from "vylbot-core"; - let args = [ - "one", - "two" - ]; +export class hello extends Command { + constructor() { + super(); + } - member.ban(reason).then(() => { - // handle then here - }).catch(err => { - // handle error here - }); + public override execute(context: ICommandContext) { + context.message.reply("Hello"); + } } ``` @@ -251,7 +249,7 @@ There are a few conventions that have developed over time for this project. When We won't accept pull requests unless these checks pass. If yours fail, simply fix what the bot says until it passes and then get a repo member to review your code. -The rules for the code is based upon [Gravity Software's Config Repo](https://github.com/getgravitysoft/config) +The rules for the code is based upon [Vylpes' Config Repo](https://github.com/vylpes/config) * Variable names should use **Camel Case** * Functions should put **braces on the same line** diff --git a/package.json b/package.json index db996d9..3c6070e 100644 --- a/package.json +++ b/package.json @@ -5,12 +5,13 @@ "main": "./src/index", "scripts": { "build": "tsc", - "test": "jest --coverage", + "test": "jest", + "test:coverage": "jest --coverage", "lint": "eslint ." }, "author": "Vylpes", "license": "MIT", - "funding": "https://ko-fi.com/gravitysoftware", + "funding": "https://ko-fi.com/vylpes", "dependencies": { "discord.js": "^12.3.1", "dotenv": "^10.0.0" From f6d57a1fda5666bd19f302ab3f6a1a1bfc93a29c Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Mon, 4 Oct 2021 18:45:44 +0000 Subject: [PATCH 10/13] Bump tmpl from 1.0.4 to 1.0.5 Bumps [tmpl](https://github.com/daaku/nodejs-tmpl) from 1.0.4 to 1.0.5. - [Release notes](https://github.com/daaku/nodejs-tmpl/releases) - [Commits](https://github.com/daaku/nodejs-tmpl/commits/v1.0.5) --- updated-dependencies: - dependency-name: tmpl dependency-type: indirect ... Signed-off-by: dependabot[bot] --- yarn.lock | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/yarn.lock b/yarn.lock index dc93067..59ded77 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2984,9 +2984,9 @@ throat@^6.0.1: integrity sha512-8hmiGIJMDlwjg7dlJ4yKGLK8EsYqKgPWbG3b4wjJddKNwc7N7Dpn08Df4szr/sZdMVeOstrdYSsqzX6BYbcB+w== tmpl@1.0.x: - version "1.0.4" - resolved "https://registry.yarnpkg.com/tmpl/-/tmpl-1.0.4.tgz#23640dd7b42d00433911140820e5cf440e521dd1" - integrity sha1-I2QN17QtAEM5ERQIIOXPRA5SHdE= + version "1.0.5" + resolved "https://registry.yarnpkg.com/tmpl/-/tmpl-1.0.5.tgz#8683e0b902bb9c20c4f726e3c0b69f36518c07cc" + integrity sha512-3f0uOEAQwIqGuWW2MVzYg8fV/QNnc/IpuJNG837rLuczAaLVHslWHZQj4IGiEl5Hs3kkbhwL9Ab7Hrsmuj+Smw== to-fast-properties@^2.0.0: version "2.0.0" From e302f2ee4dd6362e478a5362da00ba0d8650a031 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Mon, 4 Oct 2021 18:45:45 +0000 Subject: [PATCH 11/13] Bump ansi-regex from 5.0.0 to 5.0.1 Bumps [ansi-regex](https://github.com/chalk/ansi-regex) from 5.0.0 to 5.0.1. - [Release notes](https://github.com/chalk/ansi-regex/releases) - [Commits](https://github.com/chalk/ansi-regex/compare/v5.0.0...v5.0.1) --- updated-dependencies: - dependency-name: ansi-regex dependency-type: indirect ... Signed-off-by: dependabot[bot] --- yarn.lock | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/yarn.lock b/yarn.lock index dc93067..08e6de0 100644 --- a/yarn.lock +++ b/yarn.lock @@ -848,9 +848,9 @@ ansi-escapes@^4.2.1: type-fest "^0.21.3" ansi-regex@^5.0.0: - version "5.0.0" - resolved "https://registry.yarnpkg.com/ansi-regex/-/ansi-regex-5.0.0.tgz#388539f55179bf39339c81af30a654d69f87cb75" - integrity sha512-bY6fj56OUQ0hU1KjFNDQuJFezqKdrAyFdIevADiqrWHwSlbmBNMHp5ak2f40Pm8JTFyM2mqxkG6ngkHO11f/lg== + version "5.0.1" + resolved "https://registry.yarnpkg.com/ansi-regex/-/ansi-regex-5.0.1.tgz#082cb2c89c9fe8659a311a53bd6a4dc5301db304" + integrity sha512-quJQXlTSUGL2LH9SUXo8VwsY4soanhgo6LNSm84E1LBcE8s3O0wpdiRzyR9z/ZZJMlMWv37qOOb9pdJlMUEKFQ== ansi-styles@^3.2.1: version "3.2.1" From 16ac225a442f136cd8d7e5f128b663e835311158 Mon Sep 17 00:00:00 2001 From: Ethan Lane Date: Mon, 4 Oct 2021 19:51:21 +0100 Subject: [PATCH 12/13] v2.0.0 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 3c6070e..3e490c5 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "vylbot-core", - "version": "1.0.5", + "version": "2.0.0", "description": "A discord client based upon discord.js", "main": "./src/index", "scripts": { From 7c85aec9716278a78d4384bf28d5da1b488c24dd Mon Sep 17 00:00:00 2001 From: Ethan Lane Date: Mon, 11 Oct 2021 16:22:18 +0100 Subject: [PATCH 13/13] v2.0.1 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 3e490c5..eb1267d 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "vylbot-core", - "version": "2.0.0", + "version": "2.0.1", "description": "A discord client based upon discord.js", "main": "./src/index", "scripts": {