const { REST, Routes } = require("discord.js"); const { clientId, guildId, token } = require("./config.json"); const fs = require("node:fs"); const path = require("node:path"); const commands = []; const commandsPath = path.join(__dirname, "commands"); function getCommandFiles(dir) { const entries = fs.readdirSync(dir, { withFileTypes: true }); for (const entry of entries) { const fullPath = path.join(dir, entry.name); if (entry.isDirectory()) { getCommandFiles(fullPath); } else if (entry.isFile() && entry.name.endsWith(".js")) { try { delete require.cache[require.resolve(fullPath)]; const command = require(fullPath); if ("data" in command && "execute" in command) { commands.push(command.data.toJSON()); console.log(`[LOAD] Loaded command: ${command.data.name} (${fullPath})`); } else { console.warn(`[WARNING] The command at ${fullPath} is missing a required data or execute property.`); } } catch (err) { console.error(`[ERROR] Failed to load command at ${fullPath}:`, err); } } } } getCommandFiles(commandsPath); const rest = new REST({ version: "10" }).setToken(token); (async () => { try { console.log(`Started refreshing ${commands.length} application (/) commands.`); const data = await rest.put( Routes.applicationGuildCommands(clientId, guildId), { body: commands } ); console.log(`Successfully reloaded ${data.length} application (/) commands.`); } catch (error) { console.error(error); } })();