diff --git a/deploy-commands.js b/deploy-commands.js index 7907121..968d77b 100644 --- a/deploy-commands.js +++ b/deploy-commands.js @@ -4,6 +4,7 @@ import path from 'node:path'; import config from './config.json' with { type: "json" }; const { clientId, guildId, token } = config; import { fileURLToPath } from 'url'; +import util from 'node:util'; const filename = fileURLToPath(import.meta.url); const dirname = path.dirname(filename); @@ -31,20 +32,35 @@ for (const folder of commandFolders) { // Construct and prepare an instance of the REST module const rest = new REST().setToken(token); +const lastDeployCommands = path.join(dirname, ".last-deploy-commands"); +async function shouldRedeployCommands () { + try { + const last = await fs.promises.readFile(lastDeployCommands, "utf-8"); + const current = util.inspect(commands) + JSON.stringify(config.token); + return last !== current; + } catch { + // If we failed to open ".last-deploy-commands", then we have never deployed before. + return true; + } +} + // and deploy your commands! (async () => { - try { - console.log('Started refreshing ${commands.length} application (/) commands.'); + if (await shouldRedeployCommands()) { + try { + console.log('Started refreshing ${commands.length} application (/) commands.'); - // The put method is used to fully refresh all commands in the guild with the current set - const data = await rest.put( - Routes.applicationGuildCommands(clientId, guildId), - { body: commands }, - ); + // The put method is used to fully refresh all commands in the guild with the current set + const data = await rest.put( + Routes.applicationGuildCommands(clientId, guildId), + { body: commands }, + ); - console.log('Successfully reloaded ${data.length} application (/) commands.'); - } catch (error) { - // And of course, make sure you catch and log any errors! - console.error(error); + console.log('Successfully reloaded ${data.length} application (/) commands.'); + await fs.promises.writeFile(lastDeployCommands, util.inspect(commands) + JSON.stringify(config.token)); + } catch (error) { + // And of course, make sure you catch and log any errors! + console.error(error); + } } })(); \ No newline at end of file diff --git a/package.json b/package.json index f601b0f..762fcc9 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,7 @@ "main": "index.js", "type": "module", "scripts": { - "start": "nodemon --no-colours index.js" + "start": "nodemon --no-colours -x 'node deploy-commands && node index.js'" }, "keywords": [], "author": "",