From 00a057792b3bf449c0ceb235046e3b7e881541ee Mon Sep 17 00:00:00 2001 From: Ethan Lane Date: Sun, 26 Sep 2021 14:12:21 +0100 Subject: [PATCH 1/4] 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 2/4] 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 3/4] 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 4/4] 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");