Delete slash command if it is "forgotten"

I have a problem with my guilds slash commands where If I delete a command in my code, it stays in the guild. Now I'm trying to code something to delete the slash command if it doesn't "exist" anymore in the code, but I'm getting an error. Here's my output:
❯ npm start

> node scripts/start.js && npm run build && cd dist && node src/index.js


> rimraf dist/* && tsc && tsc-alias && tscp

Non-typescript files copied
discord-dev-env
✓ Created logs folder at: /Users/marlon/Javascript/discord-dev-env/dist/logs
✓ Logged in as xxx#0000!
ℹ Initalizing Events...
✓ Events have been loaded! [interactionCreate.js]
ℹ Started deploying slash commands...
✓ Cleared global commands cache!
ℹ Scanning commands in "/Users/marlon/Javascript/discord-dev-env/dist/src/commands"
✓ (/) Reloaded guild-specific commands for 0 guilds.
✓ (/) Reloaded 3 global commands
✓ All slash commands have been deployed. The bot is ready to run!
✖ An error occured while deleting left-over command:
DiscordAPIError[10063]: Unknown application command
❯ npm start

> node scripts/start.js && npm run build && cd dist && node src/index.js


> rimraf dist/* && tsc && tsc-alias && tscp

Non-typescript files copied
discord-dev-env
✓ Created logs folder at: /Users/marlon/Javascript/discord-dev-env/dist/logs
✓ Logged in as xxx#0000!
ℹ Initalizing Events...
✓ Events have been loaded! [interactionCreate.js]
ℹ Started deploying slash commands...
✓ Cleared global commands cache!
ℹ Scanning commands in "/Users/marlon/Javascript/discord-dev-env/dist/src/commands"
✓ (/) Reloaded guild-specific commands for 0 guilds.
✓ (/) Reloaded 3 global commands
✓ All slash commands have been deployed. The bot is ready to run!
✖ An error occured while deleting left-over command:
DiscordAPIError[10063]: Unknown application command
The code to delete the slash command is the following:
const command = interaction.client.commands.get(interaction.commandName)
if (!command) {
const rest = new REST().setToken(process.env.token)
try {
rest.delete(Routes.applicationGuildCommand(process.env.clientId,interaction.guild.id,interaction.id))
.then(()=> logging(`Deleted leftover command ${interaction.commandName} in guild "${interaction.guild.id}`,"minimal"))
.catch(err => logging(`An error occured while deleting left-over command:\n${err}`,"error"))
} catch (error) {
logging("An error occured while deleting left-over command:\n" + error,"error")
}
interaction.reply({content: "Command is outdated. It cannot be used anymore."})

return
}
const command = interaction.client.commands.get(interaction.commandName)
if (!command) {
const rest = new REST().setToken(process.env.token)
try {
rest.delete(Routes.applicationGuildCommand(process.env.clientId,interaction.guild.id,interaction.id))
.then(()=> logging(`Deleted leftover command ${interaction.commandName} in guild "${interaction.guild.id}`,"minimal"))
.catch(err => logging(`An error occured while deleting left-over command:\n${err}`,"error"))
} catch (error) {
logging("An error occured while deleting left-over command:\n" + error,"error")
}
interaction.reply({content: "Command is outdated. It cannot be used anymore."})

return
}
Can't I use the command interaction ID to delete it?
30 Replies
d.js toolkit
d.js toolkit2y 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 staff
Ori
OriOP2y ago
[email protected] and node v20.3.0
Inky
Inky2y ago
I just use set as opposed to create when deploying commands to avoid the need for deleting commands Well, put instead of post if using rest
Ori
OriOP2y ago
Wdym? Set?
Inky
Inky2y ago
Put instead of post
Ori
OriOP2y ago
Oh, what's the difference? It's temporary maybe?
Inky
Inky2y ago
put sets alls commands
Ori
OriOP2y ago
Oh yeah
Inky
Inky2y ago
Whatever array of commands you provide will become the entire set of commands (new ones are created, removed ones are deleted, others are updated if needed) The creates are still counted against the creation rate limit, but the rest isn’t Heh, rest
Ori
OriOP2y ago
I already use put lol
No description
Ori
OriOP2y ago
Wait maybe i'm getting old commands
Inky
Inky2y ago
Try running the old command It shouldn’t go to the bot
Ori
OriOP2y ago
Well it says the message wait
Ori
OriOP2y ago
No description
Inky
Inky2y ago
Yea
Ori
OriOP2y ago
I'm maybe guessing that i'm getting old commands before pushing
Inky
Inky2y ago
Is that from ur bot?
Ori
OriOP2y ago
Yes (dont mind the name it's not the actual bot lol)
Inky
Inky2y ago
Are you sure the command got removed from the array?
Ori
OriOP2y ago
I'll try logging it Oh yeah also If I remove every guild commands then the guild deploy part will not run and therefor not overwrite the old guild commands that's why
Ori
OriOP2y ago
Yeah now it works
No description
Ori
OriOP2y ago
When I add a guild command
Inky
Inky2y ago
You don’t manually run the deploy script while testing?
Ori
OriOP2y ago
what do you mean?
Inky
Inky2y ago
Usually, application command deployment is its own script
Ori
OriOP2y ago
Oh yes it is its own script What I mean is that If I delete all guild commands, the part where it deploys guild commands will not run, so it will not overwrite the old commands That's why I wanted to delete the command if it was old
Inky
Inky2y ago
I’m not rly sure what your mean My deploy script just reads the commands directory to see available commands, then sets the commands to match that
Ori
OriOP2y ago
Yes but I'm talking about guild-specifc commands My script is halfed into global, and guild commands. The global deploy always run. The guilds one only runs if one command in the code is guild-specific I could send it If you want
Inky
Inky2y ago
I would just have it always deploy Even if there are no commands
Ori
OriOP2y ago
I have to go but I'll try to explain it better later If there is no guilds in which you need to reload commands, then the code doesn't know which guilds you need to push commands to

Did you find this page helpful?