const fs = require("node:fs"); const { REST, Routes } = require("discord.js"); const { token } = require('./config.json'); const rest = new REST({ version: "9" }).setToken(token); const isDev = process.argv.includes('--dev'); rest .get(Routes.currentApplication()) .then(application => { console.log(`Operating on application ${application.name}`); if (isDev) console.log('Dev mode on - this may produce unwanted results in prod'); const appID = application.id; const commands = []; const commandFiles = fs.readdirSync("./dist/commands").filter((file) => file.endsWith(".js") && !file.startsWith('.')); for (const file of commandFiles) { const command = require(`./dist/commands/${file}`).default; commands.push(command); } if (!isDev) { const knownServers = require('./dist/lib/knownServers').knownServers; const servers = [...new Set(Object.values(knownServers).reduce((a, b) => a.concat(b), []))]; for (const id of servers) { const serverCommands = commands.filter(command => command.serverWhitelist && command.serverWhitelist.includes(id)); rest .put(Routes.applicationGuildCommands(appID, id), { body: serverCommands.map(cmd => cmd.data.toJSON()) }) .then(() => console.log(`${serverCommands.length} commands added to ${id}`)) .catch(console.error); } const globalCommands = commands.filter(command => !command.serverWhitelist); rest .put(Routes.applicationCommands(appID), { body: globalCommands.map(cmd => cmd.data.toJSON()) }) .then(() => console.log(`${globalCommands.length} commands added globally, might take a bit to refresh`)) .catch(console.error); } else { const guildID = '741963936689160282'; // TODO rest .put(Routes.applicationGuildCommands(appID, guildID), { body: commands.map(cmd => cmd.data.toJSON()) }) .then(() => console.log(`${commands.length} commands added to ${guildID}`)) .catch(console.error); } });