Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 27 additions & 11 deletions deploy-commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}
}
})();
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": "",
Expand Down