summaryrefslogtreecommitdiff
path: root/index.ts
diff options
context:
space:
mode:
Diffstat (limited to 'index.ts')
-rw-r--r--index.ts58
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);
+}