-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
63 lines (44 loc) · 1.79 KB
/
index.ts
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
59
60
61
62
63
import assert from "assert";
import { Client, GatewayIntentBits, REST, Routes } from "discord.js";
import { commands } from "./load-commands.js";
import { loadFlow } from "@flyde/runtime";
const __filename = fileURLToPath(import.meta.url);
const rootDir = join(__filename, "../..");
import "dotenv/config";
import { fileURLToPath } from "url";
import { join } from "path";
const { DISCORD_BOT_TOKEN, DISCORD_CLIENT_ID } = process.env;
assert(DISCORD_BOT_TOKEN, "Missing DISCORD_BOT_TOKEN env var");
assert(DISCORD_CLIENT_ID, "Missing DISCORD_CLIENT_ID env var");
const client = new Client({ intents: [GatewayIntentBits.Guilds] });
const rest = new REST({ version: "10" }).setToken(DISCORD_BOT_TOKEN);
try {
console.log("Started refreshing application (/) commands.");
await rest.put(Routes.applicationCommands(DISCORD_CLIENT_ID), {
body: commands.map((command) => command.config),
});
console.log("Successfully reloaded application (/) commands.");
} catch (error) {
console.error(error);
}
client.on("ready", () => {
console.log(`Logged in as ${client?.user?.tag}!`);
});
client.on("interactionCreate", async (interaction) => {
if (!interaction.isChatInputCommand()) return;
const command = commands.find(
(command) => command.config.name === interaction.commandName
);
if (!command) {
console.error(`Command not found: ${interaction.commandName}`);
await interaction.reply(
"Oops, something went wrong. The command you tried to run doesn't exist."
);
return;
}
console.info(`Running command: ${interaction.commandName}`);
const execute = await loadFlow(command.handlerPath, rootDir);
await execute({}, { extraContext: { interaction } }).result;
console.info(`Finished running command: ${interaction.commandName}`);
});
client.login(DISCORD_BOT_TOKEN);