Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PoC: Only one event listener for all commands #1152

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 6 additions & 13 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 All @@ -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 (
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, Interaction, CacheType } 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, (...eventArgs: ClientEvents['interactionCreate']) => 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: (...eventArgs: ClientEvents['interactionCreate']) => 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] = 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}`)
}
}
}