diff --git a/README.md b/README.md index c97de53..1aed8a7 100644 --- a/README.md +++ b/README.md @@ -70,7 +70,7 @@ const avatarCommand = createCommandHandler({ description: 'The user to get the avatar for', required: true } - ] as const, + ], execute: async (context: Context, args) => { // Args will be typed here as { user: User } context.reply(args.user.avatarURL); @@ -85,8 +85,6 @@ client.applicationCommands.register('avatar', ApplicationCommandType.Chat, avata // <...> ``` -> NOTE: The `as const` is required to ensure the types are resolved correctly. This is to work around some TypeScript weirdness. - ## Hydration Hydration allows you to fetch and fill data on Discord objects. This is useful for when you need to fetch additional data about an object. diff --git a/src/services/connectors/gateway.ts b/src/services/connectors/gateway.ts index d67e24c..a1e3f90 100644 --- a/src/services/connectors/gateway.ts +++ b/src/services/connectors/gateway.ts @@ -204,14 +204,16 @@ export class Gateway { this.client.messages.set(message.id, message); // Generate context const context = new Context(this.client, this.api, { message }); + // Dispatch event for the command + this.bus.emit(`command:${command}`, context, args); // // Check if command exists - if (this.processor[HandlerType.ChatCommands].has(command)) { - // Execute the handler - this.processor[HandlerType.ChatCommands].execute(command, context, args); - } else { - // TODO: Custom unknown command handler - context.reply(`Unknown command: ${command}`); - } + // if (this.processor[HandlerType.ChatCommands].has(command)) { + // // Execute the handler + // this.processor[HandlerType.ChatCommands].execute(command, context, args); + // } else { + // // TODO: Custom unknown command handler + // context.reply(`Unknown command: ${command}`); + // } } }; diff --git a/src/services/processor.ts b/src/services/processor.ts index 5472c27..cdbde1f 100644 --- a/src/services/processor.ts +++ b/src/services/processor.ts @@ -180,7 +180,7 @@ export class Processor { has: (event: string) => this.handlers[HandlerType.Interactions].has(event) ?? this.handlers[HandlerType.Interactions].get(event)?.length! > 0 }; - static isCommandHandler = (handler: Handler): handler is CommandHandler => { + static isCommandHandler = (handler: Handler): handler is CommandHandler => { if (typeof handler === 'object') { return 'args' in handler && Trait.execute in handler && handler instanceof CommandHandler; } else { diff --git a/src/types/handler.ts b/src/types/handler.ts index a943ad9..5127a0a 100644 --- a/src/types/handler.ts +++ b/src/types/handler.ts @@ -108,16 +108,16 @@ export type ArgsFromOptions = { : unknown | undefined; }; -export type CommandHandlerExecuteFunction = (context: Context, args: ArgsFromOptions) => Promise | any; +export type CommandHandlerExecuteFunction = (context: Context, args: ArgsFromOptions) => Promise | any; -export type CommandHandlerInput = { +export type CommandHandlerInput = { args: T; description: string; contexts?: ApplicationCommandContext[]; execute: CommandHandlerExecuteFunction; }; -export class CommandHandler { +export class CommandHandler { args: T; description: string; contexts?: ApplicationCommandContext[]; @@ -155,7 +155,7 @@ export class CommandHandler { }; // Helper function to infer type of `args` -export function createCommandHandler( +export function createCommandHandler( handler: CommandHandlerInput ): CommandHandler { return new CommandHandler(handler); @@ -166,6 +166,6 @@ export type EventHandlerFunction = (context: Context | null, payload: EventPaylo export type InteractionHandlerFunction

= (context: Context, payload: P) => Promise | any; export type HandlerWithoutContext = EventHandlerFunction; -export type HanderWithContext = InteractionHandlerFunction | CommandHandlerFunction | CommandHandler; +export type HanderWithContext = InteractionHandlerFunction | CommandHandlerFunction | CommandHandler; -export type Handler = InteractionHandlerFunction | InteractionHandlerFunction | EventHandlerFunction | CommandHandlerFunction | CommandHandler; \ No newline at end of file +export type Handler = InteractionHandlerFunction | InteractionHandlerFunction | EventHandlerFunction | CommandHandlerFunction | CommandHandler; \ No newline at end of file diff --git a/testing/demo.test.ts b/testing/demo.test.ts index dac118b..98fa030 100644 --- a/testing/demo.test.ts +++ b/testing/demo.test.ts @@ -62,7 +62,7 @@ client.applicationCommands.register('hello', ApplicationCommandType.Chat, create { name: "Bear", value: "Bear" } ] } - ] as const, + ], execute: async (context, args) => { context.reply( `\