summaryrefslogtreecommitdiff
path: root/index.ts
blob: 913cf55f8a5fa234f79126a17bce3dbd7d6d783e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
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);
}