From 146d886d04bc16f28e3f50b7639086626d37c758 Mon Sep 17 00:00:00 2001 From: GRZ4NA Date: Mon, 2 Aug 2021 19:14:24 +0200 Subject: [PATCH] Arguments template - work in progress --- src/Arguments.ts | 46 ++++++++++++++++++++++++++++++++++++++++++++++ src/Command.ts | 29 +++++++++++++++++++++++------ 2 files changed, 69 insertions(+), 6 deletions(-) create mode 100644 src/Arguments.ts diff --git a/src/Arguments.ts b/src/Arguments.ts new file mode 100644 index 0000000..6312a18 --- /dev/null +++ b/src/Arguments.ts @@ -0,0 +1,46 @@ +export type ArgumentType = "STRING" | "BOOLEAN" | "INTEGER" | "FLOAT"; +export type ArgumentResolvable = string | boolean | number; +export interface Argument { + name: string; + isOptional?: boolean; + type?: ArgumentType; +} +export const ProcessArgument = ( + a: string, + type: ArgumentType +): ArgumentResolvable => { + switch (type) { + case "STRING": + return a; + case "INTEGER": + const numI = parseInt(a); + if (isNaN(numI)) { + throw new TypeError( + `Argument ${a} cannot be converted to ${type}` + ); + } else { + return numI; + } + case "FLOAT": + const numF = parseFloat(a); + if (isNaN(numF)) { + throw new TypeError( + `Argument ${a} cannot be converted to ${type}` + ); + } else { + return numF; + } + case "BOOLEAN": + if (a.toLowerCase() == "true") { + return true; + } else if (a.toLowerCase() == "false") { + return false; + } else { + throw new TypeError( + `Argument ${a} cannot be converted to ${type}` + ); + } + default: + throw new TypeError("Invalied type specified"); + } +}; diff --git a/src/Command.ts b/src/Command.ts index 3b4b9c9..9d0bcea 100644 --- a/src/Command.ts +++ b/src/Command.ts @@ -6,17 +6,19 @@ import { GuildMember, MessageEmbed, } from "discord.js"; +import { Argument, ArgumentResolvable } from "./Arguments.js"; //TYPE DEFINITIONS type GetMode = "ALL" | "PREFIX" | "NO_PREFIX"; type PermissionCheckTypes = "ALL" | "ANY"; export interface CommandMessageStructure { name: string; - arguments: string[]; + arguments: ArgumentResolvable[]; command: Command | null; } interface CommandBuilder { name: string; + arguments?: Argument[]; aliases?: string[] | string; keywords?: string[] | string; description?: string; @@ -26,7 +28,7 @@ interface CommandBuilder { visible?: boolean; function: ( message?: Message, - cmdArguments?: string[] + cmdArguments?: ArgumentResolvable[] ) => void | string | MessageEmbed | Promise; } interface PhraseOccurrenceData { @@ -48,6 +50,7 @@ export class PermissionsError { } export class Command { name: string; + arguments: Argument[]; aliases: string[]; keywords: string[]; description: string; @@ -57,7 +60,7 @@ export class Command { visible: boolean; private function: ( message?: Message, - cmdArguments?: string[] + cmdArguments?: ArgumentResolvable[] ) => void | string | MessageEmbed | Promise; /** @@ -76,10 +79,11 @@ export class Command { */ constructor(options: CommandBuilder) { this.name = options.name.split(" ").join("_"); + this.arguments = options.arguments || []; this.aliases = Command.processPhrase(options.aliases); this.keywords = Command.processPhrase(options.keywords); this.description = options.description || "No description"; - this.usage = options.usage || ""; + this.usage = options.usage || this.generateUsageFromArguments(); this.permissionCheck = options.permissionCheck == "ALL" || options.permissionCheck == "ANY" ? options.permissionCheck @@ -95,7 +99,10 @@ export class Command { * @param {string[]} [cmdArguments] - list of processed arguments passed in a Discord message * @returns *Promise* */ - async start(message?: Message, cmdArguments?: string[]): Promise { + async start( + message?: Message, + cmdArguments?: ArgumentResolvable[] + ): Promise { const memberPermissions: Readonly = message?.member?.permissions || new Permissions(0); if ( @@ -133,6 +140,16 @@ export class Command { return []; } } + + private generateUsageFromArguments(): string { + let usageTemplate: string = ""; + this.arguments.map((e) => { + usageTemplate += `[${e.name} (${e.type}${ + e.isOptional ? ", optional" : "" + })] `; + }); + return usageTemplate; + } } export class CommandManager { list: Command[]; @@ -233,7 +250,7 @@ export class CommandManager { this.list.push(command); return true; } catch (e) { - console.error(`ERROR! ${e.toString()}`); + console.error(`[❌ ERROR] ${e.toString()}`); return false; } }