From c4b7dc3c44452f35758edb0c114aea32c34735fc Mon Sep 17 00:00:00 2001 From: GRZ4NA Date: Sun, 5 Sep 2021 16:18:45 +0200 Subject: [PATCH] getList method with type filtering --- src/managers/CommandManager.ts | 27 +++++++++++++++++++-------- src/structures/BaseCommand.ts | 2 ++ src/structures/Bot.ts | 8 ++++++-- src/structures/Help.ts | 3 ++- 4 files changed, 29 insertions(+), 11 deletions(-) diff --git a/src/managers/CommandManager.ts b/src/managers/CommandManager.ts index be99581..9f4b253 100644 --- a/src/managers/CommandManager.ts +++ b/src/managers/CommandManager.ts @@ -6,12 +6,13 @@ import { TextCommand } from "../structures/TextCommand.js"; import { CommandMessageStructure, PhraseOccurrenceData } from "../types/TextCommand.js"; import { applicationState } from "../state.js"; import { MessageCommand } from "../structures/MessageCommand.js"; +import { CommandType } from "../types/BaseCommand.js"; /** * @class Command manager */ export class CommandManager { - private readonly _textCommandsList: TextCommand[] = []; + private readonly _chatCommandsList: TextCommand[] = []; private readonly _messagesCommandsList: BaseCommand[] = []; @@ -46,7 +47,7 @@ export class CommandManager { */ public get(phrase: string): TextCommand | null { let command: TextCommand | null = null; - this._textCommandsList.map((c) => { + this._chatCommandsList.map((c) => { if (c.name == phrase) { command = c; } @@ -61,11 +62,21 @@ export class CommandManager { } /** - * List of commands registered in the manager - * @type {Array} {@link Command} + * + * @param {CommandType} filter - type of commands included in the list + * @returns {BaseCommand[]} List of commands registered in the manager */ - get list() { - return Object.freeze([...this._textCommandsList]); + public getList(filter?: CommandType): readonly BaseCommand[] { + switch (filter) { + case "CHAT": + return Object.freeze([...this._chatCommandsList]); + case "MESSAGE": + return Object.freeze([...this._messagesCommandsList]); + case "USER": + return Object.freeze([...this._userCommandsList]); + default: + return Object.freeze([...this._chatCommandsList, ...this._messagesCommandsList, ...this._userCommandsList]); + } } /** @@ -99,7 +110,7 @@ export class CommandManager { throw new Error('Parameter with defined choices must have a "string" type'); } }); - this._textCommandsList.push(command); + this._chatCommandsList.push(command); } else { throw new TypeError("Invalid argument type"); } @@ -219,7 +230,7 @@ export class CommandManager { private findTextPhraseOccurrence(phrase?: string): PhraseOccurrenceData | null { let returnValue: PhraseOccurrenceData | null = null; - this._textCommandsList.map((c) => { + this._chatCommandsList.map((c) => { if (phrase == c.name) { returnValue = { command: c, diff --git a/src/structures/BaseCommand.ts b/src/structures/BaseCommand.ts index 43ec922..e71096e 100644 --- a/src/structures/BaseCommand.ts +++ b/src/structures/BaseCommand.ts @@ -106,6 +106,8 @@ export class BaseCommand { } } + public toObject() {} + private createAccessor(args: InputParameter[]) { return function (query: string, returnType?: "value" | "object") { return returnType === "object" ? args.find((p) => p.name === query) || null : args.find((p) => p.name === query)?.value || null; diff --git a/src/structures/Bot.ts b/src/structures/Bot.ts index 2f07408..dadea95 100644 --- a/src/structures/Bot.ts +++ b/src/structures/Bot.ts @@ -258,11 +258,15 @@ export class Bot extends EventEmitter { process.stdout.write("Registering commands... "); await axios.put( `https://discord.com/api/v8/applications/${this.applicationId}/commands`, - this.commands.list.filter((c) => !Array.isArray(c.guilds) && c.slash).map((c) => c.toObject()), + this.commands + .getList() + .filter((c) => !Array.isArray(c.guilds) && c instanceof TextCommand && c.slash) + .map((c) => c.toObject()), { headers: { Authorization: `Bot ${this.token}` } } ); const guilds: any = {}; - await this.commands.list + await this.commands + .getList() .filter((c) => Array.isArray(c.guilds) && c.guilds.length > 0) .map((c) => { c.guilds?.map(async (g) => { diff --git a/src/structures/Help.ts b/src/structures/Help.ts index 074ebe0..d57cf36 100644 --- a/src/structures/Help.ts +++ b/src/structures/Help.ts @@ -64,8 +64,9 @@ export class HelpMessage extends TextCommand { helpMsg.setTitle(params.title); helpMsg.setDescription(params.bottomText); - cmdManager.list.map((c) => { + cmdManager.getList("CHAT").map((c) => { if ( + c instanceof TextCommand && c.visible && ((Array.isArray(c.guilds) && c.guilds.length > 0 && c.guilds.find((g) => i?.guild?.id === g)) || !Array.isArray(c.guilds) || c.guilds.length === 0) ) {