From d8b85e60eca31db525dd73728fe029c91cc4bf81 Mon Sep 17 00:00:00 2001 From: Jaron Date: Wed, 5 Feb 2025 20:43:43 -0600 Subject: [PATCH] Moving to an event bus strategy for commands --- src/services/connectors/gateway.ts | 16 ++++++++-------- src/services/processor.ts | 8 +++++--- 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/src/services/connectors/gateway.ts b/src/services/connectors/gateway.ts index 241b9c5..5a7873f 100644 --- a/src/services/connectors/gateway.ts +++ b/src/services/connectors/gateway.ts @@ -205,15 +205,15 @@ export class Gateway { // Generate context const context = new Context(this.client, this.api, { message }); // Dispatch event for the command - // this.bus.emit(`${HandlerType.ChatCommands}:${command}`, context, args); + this.bus.emit(`${HandlerType.ChatCommands}:${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 7d2fbe8..7f0c7e3 100644 --- a/src/services/processor.ts +++ b/src/services/processor.ts @@ -24,6 +24,7 @@ export class Processor { const handlers = this.handlers[type].get(e) || []; handlers.push(handler); this.handlers[type].set(e, handlers); + this.bus.on(`${type}:${e}`, async (context: Context | null, payload: any) => { this.execute(type, e, context, payload) }); } }; @@ -50,9 +51,10 @@ export class Processor { } }; - public async execute(type: HandlerType.Events, event: string, context: Context | null, payload: EventPayload): Promise - public async execute(type: HandlerType.Interactions, event: string, context: Context | null, payload: InteractionData): Promise - public async execute(type: Omit, event: string, context: Context, args: any[]): Promise + // TODO: Fix overloads + // public async execute(type: HandlerType.Events, event: string, context: Context | null, payload: EventPayload): Promise + // public async execute(type: HandlerType.Interactions, event: string, context: Context | null, payload: InteractionData): Promise + // public async execute(type: Omit, event: string, context: Context, args: any[]): Promise public async execute(type: HandlerType, event: string, context: Context | null, argsOrPayload: any[] | EventPayload) { const handlers = this.handlers[type].get(event);