From 59bce88f3a0d796b9d0304d44b4394055b503f3f Mon Sep 17 00:00:00 2001 From: MapleLeaf <19603573+itsMapleLeaf@users.noreply.github.com> Date: Sat, 25 Dec 2021 00:51:43 -0600 Subject: [PATCH] playground: simple command handler --- playground/command-handler.ts | 39 +++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 playground/command-handler.ts diff --git a/playground/command-handler.ts b/playground/command-handler.ts new file mode 100644 index 0000000..791828e --- /dev/null +++ b/playground/command-handler.ts @@ -0,0 +1,39 @@ +import type { Client, CommandInteraction } from "discord.js" + +type Command = { + name: string + description: string + run: (interaction: CommandInteraction) => unknown +} + +export function createCommandHandler(client: Client, commands: Command[]) { + client.on("ready", async () => { + for (const command of commands) { + for (const guild of client.guilds.cache.values()) { + await client.application?.commands.create( + { + name: command.name, + description: command.description, + }, + guild.id, + ) + } + } + console.info("ready 💖") + }) + + client.on("interactionCreate", async (interaction) => { + if (!interaction.isCommand()) return + + const command = commands.find( + (command) => command.name === interaction.commandName, + ) + if (command) { + try { + await command.run(interaction) + } catch (error) { + console.error(error) + } + } + }) +}