From 97ce6d1f369cd61a2f7eabd2ddd0e35166dfd53f Mon Sep 17 00:00:00 2001 From: DjDeveloperr Date: Thu, 8 Apr 2021 15:10:50 +0530 Subject: [PATCH] add cmd meta --- src/commands/command.ts | 10 ++++++++-- src/interactions/slashClient.ts | 2 +- test/meta.ts | 29 +++++++++++++++++++++++++++++ test/slash-http.ts | 2 +- 4 files changed, 39 insertions(+), 4 deletions(-) create mode 100644 test/meta.ts diff --git a/src/commands/command.ts b/src/commands/command.ts index bbe4dee..b2d5fb1 100644 --- a/src/commands/command.ts +++ b/src/commands/command.ts @@ -72,6 +72,8 @@ export interface CommandOptions { } export class Command implements CommandOptions { + static meta?: CommandOptions + name: string = '' description?: string category?: string @@ -486,12 +488,16 @@ export class CommandsManager { /** Add a Command */ add(cmd: Command | typeof Command): boolean { - // eslint-disable-next-line new-cap - if (!(cmd instanceof Command)) cmd = new cmd() + if (!(cmd instanceof Command)) { + const CmdClass = cmd + cmd = new CmdClass() + Object.assign(cmd, CmdClass.meta ?? {}) + } if (this.exists(cmd, cmd.extension?.subPrefix)) throw new Error( `Failed to add Command '${cmd.toString()}' with name/alias already exists.` ) + if (cmd.name === '') throw new Error('Command has no name') this.list.set( `${cmd.name}-${ this.list.filter((e) => diff --git a/src/interactions/slashClient.ts b/src/interactions/slashClient.ts index ddd7a13..29e808c 100644 --- a/src/interactions/slashClient.ts +++ b/src/interactions/slashClient.ts @@ -253,7 +253,7 @@ export class SlashClient extends HarmonyEventEmitter { respond: (options: { status?: number headers?: Headers - body?: string | Uint8Array | FormData + body?: any }) => Promise }): Promise { if (req.method.toLowerCase() !== 'post') return false diff --git a/test/meta.ts b/test/meta.ts new file mode 100644 index 0000000..654133e --- /dev/null +++ b/test/meta.ts @@ -0,0 +1,29 @@ +import { + CommandClient, + Command, + Intents, + CommandOptions, + CommandContext +} from '../mod.ts' +import { TOKEN } from './config.ts' + +const client = new CommandClient({ + prefix: '!', + token: TOKEN, + intents: Intents.None +}) + +class Ping extends Command { + static meta: CommandOptions = { + name: 'ping', + aliases: 'pong' + } + + execute(ctx: CommandContext): void { + ctx.message.reply('Pong!') + } +} + +client.commands.add(Ping) + +client.connect() diff --git a/test/slash-http.ts b/test/slash-http.ts index 893feaf..6ed7188 100644 --- a/test/slash-http.ts +++ b/test/slash-http.ts @@ -1,4 +1,4 @@ -import { SlashClient } from '../../mod.ts' +import { SlashClient } from '../mod.ts' import { SLASH_ID, SLASH_PUB_KEY, SLASH_TOKEN } from './config.ts' import { listenAndServe } from 'https://deno.land/std@0.90.0/http/server.ts'