add cmd meta

This commit is contained in:
DjDeveloperr 2021-04-08 15:10:50 +05:30
parent d681dc41a7
commit 97ce6d1f36
4 changed files with 39 additions and 4 deletions

View file

@ -72,6 +72,8 @@ export interface CommandOptions {
} }
export class Command implements CommandOptions { export class Command implements CommandOptions {
static meta?: CommandOptions
name: string = '' name: string = ''
description?: string description?: string
category?: string category?: string
@ -486,12 +488,16 @@ export class CommandsManager {
/** Add a Command */ /** Add a Command */
add(cmd: Command | typeof Command): boolean { add(cmd: Command | typeof Command): boolean {
// eslint-disable-next-line new-cap if (!(cmd instanceof Command)) {
if (!(cmd instanceof Command)) cmd = new cmd() const CmdClass = cmd
cmd = new CmdClass()
Object.assign(cmd, CmdClass.meta ?? {})
}
if (this.exists(cmd, cmd.extension?.subPrefix)) if (this.exists(cmd, cmd.extension?.subPrefix))
throw new Error( throw new Error(
`Failed to add Command '${cmd.toString()}' with name/alias already exists.` `Failed to add Command '${cmd.toString()}' with name/alias already exists.`
) )
if (cmd.name === '') throw new Error('Command has no name')
this.list.set( this.list.set(
`${cmd.name}-${ `${cmd.name}-${
this.list.filter((e) => this.list.filter((e) =>

View file

@ -253,7 +253,7 @@ export class SlashClient extends HarmonyEventEmitter<SlashClientEvents> {
respond: (options: { respond: (options: {
status?: number status?: number
headers?: Headers headers?: Headers
body?: string | Uint8Array | FormData body?: any
}) => Promise<void> }) => Promise<void>
}): Promise<false | Interaction> { }): Promise<false | Interaction> {
if (req.method.toLowerCase() !== 'post') return false if (req.method.toLowerCase() !== 'post') return false

29
test/meta.ts Normal file
View file

@ -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()

View file

@ -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 { SLASH_ID, SLASH_PUB_KEY, SLASH_TOKEN } from './config.ts'
import { listenAndServe } from 'https://deno.land/std@0.90.0/http/server.ts' import { listenAndServe } from 'https://deno.land/std@0.90.0/http/server.ts'