Skip to content

Commit

Permalink
feat(scripts): deploy commands
Browse files Browse the repository at this point in the history
  • Loading branch information
DaStormer committed Nov 19, 2023
1 parent 4324dee commit 0000bb9
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
"description": "HackRU Discord bot to facilitate a mentor queue system.",
"main": "./bot/index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node bot/index.js",
"deploy-cmds": "node scripts/deploy-commands.js"
},
"repository": {
"type": "git",
Expand Down
39 changes: 39 additions & 0 deletions scripts/deploy-commands.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
const { REST, Routes, ApplicationCommand, ApplicationCommandType } = require("discord.js");
const { readdirSync } = require("node:fs");
require("dotenv").config();

const rest = new REST().setToken(process.env.TOKEN);

const commands = [];

const cmdFiles = readdirSync("./bot/interactions/slashcommands/").filter(f => f.endsWith(".js"));

for (const file in cmdFiles) {
const cmd = new (require(`../bot/interactions/slashcommands/${file}`))();
commands.push(transformCommand(cmd.config.commandData));
}

(async () => {
try {
console.log("Started refreshing application commands...");

await rest.put(
Routes.applicationCommands(process.env.BOT_ID),
{ body: commands },
);

console.log("Successfully reloaded application commands.");
} catch (error) {
console.error(error);
}
})();

function transformCommand(command) {
return {
name: command.name,
description: command.description,
type: typeof command.type === "number" ? command.type : ApplicationCommandType[command.type],
options: command.options?.map(o => ApplicationCommand.transformOption(o)),
default_permission: command.defaultPermission ?? command.default_permission,
};
}

0 comments on commit 0000bb9

Please sign in to comment.