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