Split global and guild commands

This commit is contained in:
2023-11-07 14:19:09 +01:00
parent 13ac74a764
commit f022dcbba9
2 changed files with 22 additions and 8 deletions

View File

@@ -2,6 +2,7 @@ const { SlashCommandBuilder } = require('discord.js');
module.exports = { module.exports = {
category: 'utility', category: 'utility',
global: true,
data: new SlashCommandBuilder() data: new SlashCommandBuilder()
.setName('ping') .setName('ping')
.setDescription('Replies with Pong!'), .setDescription('Replies with Pong!'),

View File

@@ -3,7 +3,9 @@ const { clientId, guildId, token } = require('./config.json');
const fs = require('node:fs'); const fs = require('node:fs');
const path = require('node:path'); const path = require('node:path');
const commands = []; const guildCommands = [];
const globalCommands = [];
// Grab all the command files from the commands directory you created earlier // Grab all the command files from the commands directory you created earlier
const foldersPath = path.join(__dirname, 'commands'); const foldersPath = path.join(__dirname, 'commands');
const commandFolders = fs.readdirSync(foldersPath); const commandFolders = fs.readdirSync(foldersPath);
@@ -17,7 +19,11 @@ for (const folder of commandFolders) {
const filePath = path.join(commandsPath, file); const filePath = path.join(commandsPath, file);
const command = require(filePath); const command = require(filePath);
if ('data' in command && 'execute' in command) { 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 { } else {
console.log(`[WARNING] The command at ${filePath} is missing a required "data" or "execute" property.`); 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! // and deploy your commands!
(async () => { (async () => {
try { 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 let data = await rest.put(
const data = await rest.put(
Routes.applicationGuildCommands(clientId, guildId), 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) { } catch (error) {
// And of course, make sure you catch and log any errors!
console.error(error); console.error(error);
} }
})(); })();