delete all commands

async ResetCommands(guildId) {
var guild = this.Client.guilds.cache.get(guildId);
try {
var commands = await guild.commands.fetch();

if (commands.size == 0) return;

for (const command of commands.values()) {
await guild.commands.delete(command.id);
console.log(`Deleted command in guild ${guild.name}: ${command.name}`);
}
} catch (err) {
console.log(err);
}

// this.Rest.put(Routes.applicationCommands(this.Client.user.id), { body: [] });
}
async ResetCommands(guildId) {
var guild = this.Client.guilds.cache.get(guildId);
try {
var commands = await guild.commands.fetch();

if (commands.size == 0) return;

for (const command of commands.values()) {
await guild.commands.delete(command.id);
console.log(`Deleted command in guild ${guild.name}: ${command.name}`);
}
} catch (err) {
console.log(err);
}

// this.Rest.put(Routes.applicationCommands(this.Client.user.id), { body: [] });
}
I'm trying to get a command, but the commands keep returning 0. Maybe I missed something?
6 Replies
d.js toolkit
d.js toolkit2w ago
- What's your exact discord.js npm list discord.js and node node -v version? - Not a discord.js issue? Check out #other-js-ts. - Consider reading #how-to-get-help to improve your question! - Explain what exactly your issue is. - Post the full error stack trace, not just the top part! - Show your code! - Issue solved? Press the button! - Marked as resolved by OP
Mark
Mark2w ago
What are you trying to achieve with your code
shaitan
shaitan2w ago
What I'm trying to do now is delete all guild commands once the bot is ready.
d.js docs
d.js docs2w ago
If you have duplicate commands on your server, you registered both global and guild commands. You can remove the duplicates by resetting either the global or guild commands - Resetting global commands: rest.put(Routes.applicationCommands(clientId), { body: [] }) - Resetting guild commands: rest.put(Routes.applicationGuildCommands(clientId, guildId), { body: [] })
Mark
Mark2w ago
Just use the second method, you don't need to fetch or do them one by one You shouldn't be doing this every time your bot starts, either
shaitan
shaitan2w ago
I already try that one but nothing happen I will try again now it's work with Global Command oh problem was being registered with Global Command.