Implemented function to remove commands from a given bot and guild
This commit is contained in:
@@ -11,14 +11,14 @@ const path = require('node:path');
|
|||||||
const { DebugBuilder } = require("./debugBuilder");
|
const { DebugBuilder } = require("./debugBuilder");
|
||||||
const log = new DebugBuilder("server", "deployCommands");
|
const log = new DebugBuilder("server", "deployCommands");
|
||||||
|
|
||||||
const commands = [];
|
var commands = [];
|
||||||
// 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 commandsPath = path.resolve(__dirname, '../commands');
|
const commandsPath = path.resolve(__dirname, '../commands');
|
||||||
const commandFiles = fs.readdirSync(commandsPath).filter(file => file.endsWith('.js'));
|
const commandFiles = fs.readdirSync(commandsPath).filter(file => file.endsWith('.js'));
|
||||||
|
|
||||||
exports.deploy = (clientId, guildIDs) => {
|
exports.deploy = (clientId, guildIDs) => {
|
||||||
log.DEBUG("Deploying commands for: ", guildIDs);
|
log.DEBUG("Deploying commands for: ", guildIDs);
|
||||||
if (Array.isArray(guildIDs)) guildIDs = [guildIDs];
|
if (!Array.isArray(guildIDs)) guildIDs = [guildIDs];
|
||||||
// Grab the SlashCommandBuilder#toJSON() output of each command's data for deployment
|
// Grab the SlashCommandBuilder#toJSON() output of each command's data for deployment
|
||||||
for (const file of commandFiles) {
|
for (const file of commandFiles) {
|
||||||
const command = require(`${path.resolve(commandsPath, file)}`);
|
const command = require(`${path.resolve(commandsPath, file)}`);
|
||||||
@@ -48,3 +48,35 @@ exports.deploy = (clientId, guildIDs) => {
|
|||||||
})()
|
})()
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Remove all commands for a given bot in a given guild
|
||||||
|
*
|
||||||
|
* @param {*} clientId The client ID of the bot to remove commands from
|
||||||
|
* @param {*} guildId The ID of the guild to remove the bot commands from
|
||||||
|
*/
|
||||||
|
exports.removeAll = (clientId, guildId) => {
|
||||||
|
if (!Array.isArray(guildId)) guildIDs = [guildId];
|
||||||
|
log.DEBUG("Removing commands for: ", clientId, guildIDs);
|
||||||
|
|
||||||
|
commands = [];
|
||||||
|
|
||||||
|
const rest = new REST({ version: '10' }).setToken(token);
|
||||||
|
for (const guildId of guildIDs){
|
||||||
|
(async () => {
|
||||||
|
try {
|
||||||
|
log.DEBUG(`Started refreshing ${commands.length} application (/) commands for guild ID: ${guildId}.`);
|
||||||
|
// 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 },
|
||||||
|
);
|
||||||
|
|
||||||
|
log.DEBUG(`Successfully reloaded ${data.length} application (/) commands for guild ID: ${guildId}.`);
|
||||||
|
} catch (error) {
|
||||||
|
// And of course, make sure you catch and log any errors!
|
||||||
|
log.ERROR("ERROR Deploying commands: ", error, "Body from error: ", commands);
|
||||||
|
}
|
||||||
|
})()
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user