import { ChannelType, Client, Events, GatewayIntentBits, REST, Routes, SlashCommandBuilder, SlashCommandStringOption, type ChatInputCommandInteraction, } from "discord.js"; function requireEnv(key: string): string { const value = process.env[key]; if (!value) { throw new Error(`Missing ${key} environment variable.`); } return value; } const token = requireEnv("DISCORD_TOKEN"); const clientId = requireEnv("DISCORD_CLIENT_ID"); const guildId = requireEnv("DISCORD_GUILD_ID"); const client = new Client({ intents: [GatewayIntentBits.Guilds] }); const commands = [ ]; const rest = new REST({ version: "10" }).setToken(token); async function registerSlashCommands() { await rest.put(Routes.applicationGuildCommands(clientId, guildId), { body: commands.map(command => command.slashCommand.toJSON()), }); } client.once(Events.ClientReady, (readyClient) => { console.log(`Logged in as ${readyClient.user.tag}`); }); client.on(Events.InteractionCreate, async (interaction) => { if (!interaction.isChatInputCommand()) return; for (const command of commands) { if (interaction.commandName === command.slashCommand.name) { await command.handler(interaction); break; } } }); try { await registerSlashCommands(); await client.login(token); } catch (error) { console.error("Discord bot failed to start", error); process.exit(1); }