diff --git a/deploy-commands.cjs b/deploy-commands.cjs index 2f37eca..f7e04a0 100644 --- a/deploy-commands.cjs +++ b/deploy-commands.cjs @@ -1,41 +1,51 @@ const fs = require("node:fs"); const { REST, Routes } = require("discord.js"); const { token } = require('./config.json'); -const { exec } = require('child_process'); const rest = new REST({ version: "9" }).setToken(token); -const appID = '898850107892596776'; -console.log('building...'); -exec('pnpm tsc', (err, stdout, stderr) => { - if (err) throw err; +const isDev = process.argv.includes('--dev'); - console.log(stdout); +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('.')); + 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}`); + commands.push(command); + } - for (const file of commandFiles) { - const command = require(`./dist/commands/${file}`); - 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 - 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); -}); \ No newline at end of file + 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); + } + }); \ No newline at end of file