From 74c76c7c00ab9cf4f5db69a6861b2e5e0fe91b50 Mon Sep 17 00:00:00 2001 From: Andromeda Date: Sun, 18 Feb 2024 16:37:40 +0000 Subject: [PATCH] PoC: Only one event listener for all comannds --- .../src/explorers/command/command.explorer.ts | 19 ++++-------- packages/core/src/services/client.service.ts | 31 ++++++++++++++++--- 2 files changed, 33 insertions(+), 17 deletions(-) diff --git a/packages/core/src/explorers/command/command.explorer.ts b/packages/core/src/explorers/command/command.explorer.ts index 7e70e4cc..f23bb8e9 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 && @@ -96,8 +87,10 @@ export class CommandExplorer implements ClassExplorer { collectors: [], } as EventContext); - if (returnReply) { + if (returnReply && !interaction.isAutocomplete()) { await interaction.reply(returnReply); + }else if (returnReply && interaction.isAutocomplete()) { + await interaction.respond(returnReply); } } catch (exception) { if ( diff --git a/packages/core/src/services/client.service.ts b/packages/core/src/services/client.service.ts index dd0a53ed..10ca1438 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, Interaction, CacheType } 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: (...eventArgs: ClientEvents['interactionCreate']) => 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] = eventArgs; + if ( + (!interaction.isChatInputCommand() && + !interaction.isContextMenuCommand()) || + !this.commmandHandlers.has(interaction.commandName) + ) { + return; + } + try { + await this.commmandHandlers.get(interaction.commandName)(...eventArgs) + } + catch { + this.logger.error(`Failed to execute command: ${interaction.commandName}`) + } + } }