diff options
| author | Mathias Magnusson <mathias@magnusson.space> | 2025-12-12 16:50:09 +0100 |
|---|---|---|
| committer | Mathias Magnusson <mathias@magnusson.space> | 2025-12-12 16:52:06 +0100 |
| commit | e1f4ffd8b6b4fa8b396a421d470ad51fb3c58236 (patch) | |
| tree | 1f06c619169d1c68399dd2d0bf1d971fa428341f /index.ts | |
| download | challe-anka-e1f4ffd8b6b4fa8b396a421d470ad51fb3c58236.tar.gz | |
Initial commit
Diffstat (limited to 'index.ts')
| -rw-r--r-- | index.ts | 58 |
1 files changed, 58 insertions, 0 deletions
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); +} |
