feat: added handle command func

This commit is contained in:
Slincnik 2025-01-11 13:50:49 +03:00
parent d4dfec9789
commit 31e75bb08b
No known key found for this signature in database

View file

@ -6,6 +6,9 @@ import registerCommands from "./functions/registerCommands.js";
export class CommandHandler { export class CommandHandler {
constructor(client) { constructor(client) {
/**
* @type {import("../../structures/client.js").ExtendedClient} client
*/
this.client = client; this.client = client;
this.commands = []; this.commands = [];
} }
@ -17,6 +20,8 @@ export class CommandHandler {
client: this.client, client: this.client,
commands: this.commands, commands: this.commands,
}); });
this.handleCommands();
} }
async #buildCommands() { async #buildCommands() {
@ -39,4 +44,29 @@ export class CommandHandler {
this.commands.push({ data, run }); 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);
}
});
}
} }