From f022dcbba98c1b74a66e2e9966b9aa4cfee81226 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Gro=C3=9F?= Date: Tue, 7 Nov 2023 14:19:09 +0100 Subject: [PATCH] Split global and guild commands --- commands/utility/ping.js | 1 + deploy-commands.js | 29 +++++++++++++++++++++-------- 2 files changed, 22 insertions(+), 8 deletions(-) diff --git a/commands/utility/ping.js b/commands/utility/ping.js index 50f85b5..dc57a9f 100644 --- a/commands/utility/ping.js +++ b/commands/utility/ping.js @@ -2,6 +2,7 @@ const { SlashCommandBuilder } = require('discord.js'); module.exports = { category: 'utility', + global: true, data: new SlashCommandBuilder() .setName('ping') .setDescription('Replies with Pong!'), diff --git a/deploy-commands.js b/deploy-commands.js index bb331f7..2a06395 100644 --- a/deploy-commands.js +++ b/deploy-commands.js @@ -3,7 +3,9 @@ const { clientId, guildId, token } = require('./config.json'); const fs = require('node:fs'); const path = require('node:path'); -const commands = []; +const guildCommands = []; +const globalCommands = []; + // Grab all the command files from the commands directory you created earlier const foldersPath = path.join(__dirname, 'commands'); const commandFolders = fs.readdirSync(foldersPath); @@ -17,7 +19,11 @@ for (const folder of commandFolders) { const filePath = path.join(commandsPath, file); const command = require(filePath); if ('data' in command && 'execute' in command) { - commands.push(command.data.toJSON()); + if (command.global) + globalCommands.push(command.data.toJSON()); + else { + guildCommands.push(command.data.toJSON()); + } } else { console.log(`[WARNING] The command at ${filePath} is missing a required "data" or "execute" property.`); } @@ -30,17 +36,24 @@ const rest = new REST().setToken(token); // and deploy your commands! (async () => { try { - console.log(`Started refreshing ${commands.length} application (/) commands.`); + console.log(`[GUILD] Started refreshing ${guildCommands.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( + let data = await rest.put( Routes.applicationGuildCommands(clientId, guildId), - { body: commands }, + { body: guildCommands }, ); - console.log(`Successfully reloaded ${data.length} application (/) commands.`); + console.log(`[GUILD] Successfully reloaded ${data.length} application (/) commands.`); + + console.log(`[GLOBAL] Started refreshing ${globalCommands.length} application (/) commands.`); + + data = await rest.put( + Routes.applicationCommands(clientId), + { body: globalCommands }, + ); + + console.log(`[GLOBAL] Successfully reloaded ${data.length} application (/) commands.`); } catch (error) { - // And of course, make sure you catch and log any errors! console.error(error); } })(); \ No newline at end of file