diff --git a/packages/core/src/explorers/command/command.explorer.ts b/packages/core/src/explorers/command/command.explorer.ts index 7e70e4cc..766c37d0 100644 --- a/packages/core/src/explorers/command/command.explorer.ts +++ b/packages/core/src/explorers/command/command.explorer.ts @@ -20,7 +20,7 @@ export class CommandExplorer implements ClassExplorer { private readonly buildApplicationCommandService: BuildApplicationCommandService, private readonly externalContextCreator: ExternalContextCreator, private readonly optionService: OptionService, - ) {} + ) { } private readonly discordParamFactory = new DiscordParamFactory(); private readonly event: keyof ClientEvents = 'interactionCreate'; @@ -68,19 +68,10 @@ export class CommandExplorer implements ClassExplorer { groupName?: string, ): void { this.discordClientService - .getClient() - .on( - this.event, + .addCommandHandler( + commandName, async (...eventArgs: ClientEvents['interactionCreate']) => { const [interaction] = eventArgs; - if ( - (!interaction.isChatInputCommand() && - !interaction.isContextMenuCommand()) || - interaction.commandName !== commandName - ) { - return; - } - if ( interaction.isChatInputCommand() && ((groupName && diff --git a/packages/core/src/services/client.service.ts b/packages/core/src/services/client.service.ts index dd0a53ed..11d49a5e 100644 --- a/packages/core/src/services/client.service.ts +++ b/packages/core/src/services/client.service.ts @@ -4,7 +4,7 @@ import { OnApplicationBootstrap, OnApplicationShutdown, } from '@nestjs/common'; -import { Client, WebhookClient, WebhookClientData } from 'discord.js'; +import { Client, WebhookClient, WebhookClientData, ClientEvents } from 'discord.js'; import { SetupClientFactory } from '../definitions/interfaces/discord-module-async-options'; import { DiscordModuleOption } from '../definitions/interfaces/discord-module-options'; @@ -12,13 +12,13 @@ import { OptionService } from './option.service'; @Injectable() export class ClientService - implements OnApplicationBootstrap, OnApplicationShutdown -{ + implements OnApplicationBootstrap, OnApplicationShutdown { private readonly logger = new Logger(ClientService.name); private webhookClient: WebhookClient; private client: Client; + private commmandHandlers: Map Promise> = new Map(); - constructor(private discordOptionService: OptionService) {} + constructor(private discordOptionService: OptionService) { } initClient(options: DiscordModuleOption): void { this.discordOptionService.updateOptions(options); @@ -45,6 +45,10 @@ export class ClientService return this.webhookClient; } + addCommandHandler(commandName: string, handlerFunc: () => Promise) { + this.commmandHandlers.set(commandName, handlerFunc); + } + async onApplicationBootstrap(): Promise { const { autoLogin, failOnLogin } = this.discordOptionService.getClientData(); @@ -53,6 +57,8 @@ export class ClientService try { await this.client.login(); + // Add executeCommands, theres probably a better place but its just POC + this.client.on('interactionCreate', this.executeCommands) } catch (error) { this.logger.error('Failed to connect to Discord API'); this.logger.error(error); @@ -74,4 +80,21 @@ export class ClientService return new WebhookClient(webhookOptions); } + + private async executeCommands(...eventArgs: ClientEvents['interactionCreate']) { + const [interaction]: interaction = eventArgs; + if ( + (!interaction.isChatInputCommand() && + !interaction.isContextMenuCommand()) || + !this.commmandHandlers.has(interaction.commandName) + ) { + return; + } + try { + await this.commmandHandlers.has(interaction.commandName) + } + catch { + this.logger.error(`Failed to execute command: ${interaction.commandName}`) + } + } }