diff --git a/src/handlers/command-handler/index.js b/src/handlers/command-handler/index.js index 95401739..cbd09e04 100644 --- a/src/handlers/command-handler/index.js +++ b/src/handlers/command-handler/index.js @@ -6,6 +6,9 @@ import registerCommands from "./functions/registerCommands.js"; export class CommandHandler { constructor(client) { + /** + * @type {import("../../structures/client.js").ExtendedClient} client + */ this.client = client; this.commands = []; } @@ -17,6 +20,8 @@ export class CommandHandler { client: this.client, commands: this.commands, }); + + this.handleCommands(); } async #buildCommands() { @@ -39,4 +44,29 @@ export class CommandHandler { this.commands.push({ data, run }); } } + + handleCommands() { + this.client.on("interactionCreate", async interaction => { + if (!interaction.isChatInputCommand() && !interaction.isAutocomplete()) return; + + const isAutocomplete = interaction.isAutocomplete(); + + const targetCommand = this.commands.find(cmd => cmd.data.name === interaction.commandName); + + if (!targetCommand) return; + // Skip if autocomplete handler is not defined + if (isAutocomplete && !targetCommand.autocompleteRun) return; + + const command = targetCommand[isAutocomplete ? "autocompleteRun" : "run"]; + + try { + await command({ + client: this.client, + interaction, + }); + } catch (error) { + logger.error(error); + } + }); + } }