From 4173c96a5560df1cfe98cac713583f6e02f87c69 Mon Sep 17 00:00:00 2001 From: GRZ4NA Date: Sun, 8 Aug 2021 04:03:00 +0200 Subject: [PATCH] MAJOR CHANGE: swapped calling function arguments + replaced parameters list with function that fetches parameter (earlier getParam) --- src/Command.ts | 61 +++++++++++++++++++++++++++++--------------------- src/Help.ts | 10 +++++---- src/types.ts | 7 ++++-- 3 files changed, 46 insertions(+), 32 deletions(-) diff --git a/src/Command.ts b/src/Command.ts index 9ef82a4..a79a459 100644 --- a/src/Command.ts +++ b/src/Command.ts @@ -28,8 +28,11 @@ export class Command { guilds?: string[]; visible: boolean; private function: ( - interaction?: Message | CommandInteraction, - cmdParams?: InputParameter[] + params: ( + query: string, + returnType?: "value" | "object" + ) => ParameterResolvable | InputParameter | null, + interaction?: Message | CommandInteraction ) => | void | string @@ -103,15 +106,21 @@ export class Command { ) { const fnResult = await this.function.call( this, - interaction, - cmdParams + function (query: string, returnType?: "value" | "object") { + return returnType === "object" + ? cmdParams?.find((p) => p.name === query) || null + : cmdParams?.find((p) => p.name === query)?.value || + null; + }, + interaction ); if ( - "content" in (fnResult as ReplyMessageOptions) || - "embeds" in (fnResult as ReplyMessageOptions) || - "files" in (fnResult as ReplyMessageOptions) || - "components" in (fnResult as ReplyMessageOptions) || - "sticker" in (fnResult as ReplyMessageOptions) + fnResult instanceof Object && + ("content" in (fnResult as any) || + "embeds" in (fnResult as any) || + "files" in (fnResult as any) || + "components" in (fnResult as any) || + "sticker" in (fnResult as any)) ) { await interaction.reply(fnResult as ReplyMessageOptions); } else if (typeof fnResult == "string") { @@ -144,16 +153,26 @@ export class Command { }, 2000); const fnResult = await this.function.call( this, - interaction, - cmdParams + function ( + query: string, + returnType?: "value" | "object" + ) { + return returnType === "object" + ? cmdParams?.find((p) => p.name === query) || + null + : cmdParams?.find((p) => p.name === query) + ?.value || null; + }, + interaction ); try { if ( - "content" in (fnResult as ReplyMessageOptions) || - "embeds" in (fnResult as ReplyMessageOptions) || - "files" in (fnResult as ReplyMessageOptions) || - "components" in (fnResult as ReplyMessageOptions) || - "sticker" in (fnResult as ReplyMessageOptions) + fnResult instanceof Object && + ("content" in (fnResult as any) || + "embeds" in (fnResult as any) || + "files" in (fnResult as any) || + "components" in (fnResult as any) || + "sticker" in (fnResult as any)) ) { interaction.replied ? await interaction.editReply( @@ -251,16 +270,6 @@ export class Command { }; } - getParam( - query: string, - list: InputParameter[], - returnType?: "value" | "object" - ): ParameterResolvable | InputParameter | null { - return returnType === "object" - ? list.find((p) => p.name === query) || null - : list.find((p) => p.name === query)?.value || null; - } - private static processPhrase( phrase?: string | string[] ): string[] | undefined { diff --git a/src/Help.ts b/src/Help.ts index 1ac8086..8dd3100 100644 --- a/src/Help.ts +++ b/src/Help.ts @@ -23,15 +23,15 @@ export class HelpMessage extends Command { type: "string", }, ], - function: (_, a) => { + function: (param, _) => { const helpMsg = new MessageEmbed(); helpMsg.setColor(params.color); helpMsg.setTimestamp(); helpMsg.setFooter(botName || ""); if (helpMsg != null) { - if (a && a[0]) { + if (param("command_name")) { const cmd: Command | null = cmdManager.get( - a[0].value?.toString() || "", + param("command_name")?.toString() || "", "ALL" ); if (cmd) { @@ -75,7 +75,9 @@ export class HelpMessage extends Command { } } else { throw new ReferenceError( - `Command "${a[0].value}" does not exist` + `Command "${param( + "command_name" + )}" does not exist` ); } } else { diff --git a/src/types.ts b/src/types.ts index 5a6689e..8241101 100644 --- a/src/types.ts +++ b/src/types.ts @@ -30,8 +30,11 @@ export interface CommandBuilder { guilds?: string[]; visible?: boolean; function: ( - interaction?: Message | CommandInteraction, - cmdParams?: InputParameter[] + params: ( + query: string, + returnType?: "value" | "object" + ) => ParameterResolvable | InputParameter | null, + interaction?: Message | CommandInteraction ) => | void | string