diff --git a/package.json b/package.json index 856d4a1..3216b42 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/scripts/deploy-commands.js b/scripts/deploy-commands.js new file mode 100644 index 0000000..23baab5 --- /dev/null +++ b/scripts/deploy-commands.js @@ -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, + }; +} \ No newline at end of file