import { Client, Events, REST, Routes } from "discord.js"; import { getAllCommands, getCommand } from "../lib/commandRegistry"; import { GUILD_ID } from "../config"; export default async function (client: Client) { const token = process.env.DISCORD_TOKEN!; const clientId = client.user!.id; // use the ready client's ID const rest = new REST().setToken(token); const commandData = getAllCommands().map((cmd) => cmd.data.toJSON()); await rest.put(Routes.applicationGuildCommands(clientId, GUILD_ID), { body: commandData, }); console.log(`Registered ${commandData.length} slash command(s).`); client.on(Events.InteractionCreate, async (interaction) => { if (!interaction.isChatInputCommand()) return; const command = getCommand(interaction.commandName); if (!command) return; try { await command.execute(interaction); } catch (err) { console.error(err); if (interaction.replied || interaction.deferred) { await interaction.followUp({ content: "An error occurred.", ephemeral: true, }); } else { await interaction.reply({ content: "An error occurred.", ephemeral: true, }); } } }); }