From e1f4ffd8b6b4fa8b396a421d470ad51fb3c58236 Mon Sep 17 00:00:00 2001 From: Mathias Magnusson Date: Fri, 12 Dec 2025 16:50:09 +0100 Subject: Initial commit --- index.ts | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 index.ts (limited to 'index.ts') diff --git a/index.ts b/index.ts new file mode 100644 index 0000000..913cf55 --- /dev/null +++ b/index.ts @@ -0,0 +1,58 @@ +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); +} -- cgit v1.2.3