Files
dc-bot/src/handlers/registerCommands.ts
Space-Banane e1300a98b3
All checks were successful
CI / build (push) Successful in 10s
ran prettier lol
2026-02-22 15:31:37 +01:00

43 lines
1.4 KiB
TypeScript

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,
});
}
}
});
}