Skip to content

Commit

Permalink
PoC: Only one event listener for all comannds
Browse files Browse the repository at this point in the history
  • Loading branch information
89Q12 authored Jan 12, 2024
1 parent 652609d commit ede23f6
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 16 deletions.
15 changes: 3 additions & 12 deletions packages/core/src/explorers/command/command.explorer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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 &&
Expand Down
31 changes: 27 additions & 4 deletions packages/core/src/services/client.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,21 @@ 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';
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<string, () => Promise<void>> = new Map();

constructor(private discordOptionService: OptionService) {}
constructor(private discordOptionService: OptionService) { }

initClient(options: DiscordModuleOption): void {
this.discordOptionService.updateOptions(options);
Expand All @@ -45,6 +45,10 @@ export class ClientService
return this.webhookClient;
}

addCommandHandler(commandName: string, handlerFunc: () => Promise<void>) {
this.commmandHandlers.set(commandName, handlerFunc);
}

async onApplicationBootstrap(): Promise<void> {
const { autoLogin, failOnLogin } =
this.discordOptionService.getClientData();
Expand All @@ -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);
Expand All @@ -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}`)
}
}
}

0 comments on commit ede23f6

Please sign in to comment.