Discord bot to open dong files
1import { REST, Routes } from "discord.js";
2import fs from "node:fs";
3import path from "node:path";
4
5const clientId = Deno.env.get("CLIENT");
6const token = Deno.env.get("TOKEN");
7if (!clientId) throw "CLIENT not defined";
8if (!token) throw "TOKEN not defined";
9
10const commands = [];
11// Grab all the command folders from the commands directory you created earlier
12const foldersPath = path.join(import.meta.dirname, "commands");
13const commandFolders = fs.readdirSync(foldersPath);
14
15for (const folder of commandFolders) {
16 // Grab all the command files from the commands directory you created earlier
17 const commandsPath = path.join(foldersPath, folder);
18 const commandFiles = fs
19 .readdirSync(commandsPath)
20 .filter((file) => file.endsWith(".ts") || file.endsWith(".js"));
21 // Grab the SlashCommandBuilder#toJSON() output of each command's data for deployment
22 for (const file of commandFiles) {
23 const filePath = path.join(commandsPath, file);
24 const command = await import(filePath);
25 if ("data" in command && "execute" in command) {
26 commands.push(command.data.toJSON());
27 } else {
28 console.log(
29 `[WARNING] The command at ${filePath} is missing a required "data" or "execute" property.`
30 );
31 }
32 }
33}
34
35// Construct and prepare an instance of the REST module
36const rest = new REST().setToken(token);
37
38// and deploy your commands!
39(async () => {
40 try {
41 console.log(
42 `Started refreshing ${commands.length} application (/) commands.`
43 );
44
45 // The put method is used to fully refresh all commands with the current set
46 const data = await rest.put(Routes.applicationCommands(clientId), {
47 body: commands,
48 });
49
50 console.log(
51 `Successfully reloaded ${data.length} application (/) commands.`
52 );
53 } catch (error) {
54 // And of course, make sure you catch and log any errors!
55 console.error(error);
56 process.stdout.write(JSON.stringify(error) + "\n");
57 }
58})();