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