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 toolkit12mo 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
OriOP12mo ago
[email protected] and node v20.3.0
Kinect3000
Kinect300012mo 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
OriOP12mo ago
Wdym? Set?
Kinect3000
Kinect300012mo ago
Put instead of post
Ori
OriOP12mo ago
Oh, what's the difference? It's temporary maybe?
Kinect3000
Kinect300012mo ago
put sets alls commands
Ori
OriOP12mo ago
Oh yeah
Kinect3000
Kinect300012mo 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
OriOP12mo ago
I already use put lol
No description
Ori
OriOP12mo ago
Wait maybe i'm getting old commands
Kinect3000
Kinect300012mo ago
Try running the old command It shouldn’t go to the bot
Ori
OriOP12mo ago
Well it says the message wait
Ori
OriOP12mo ago
No description
Kinect3000
Kinect300012mo ago
Yea
Ori
OriOP12mo ago
I'm maybe guessing that i'm getting old commands before pushing
Kinect3000
Kinect300012mo ago
Is that from ur bot?
Ori
OriOP12mo ago
Yes (dont mind the name it's not the actual bot lol)
Kinect3000
Kinect300012mo ago
Are you sure the command got removed from the array?
Ori
OriOP12mo 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
OriOP12mo ago
Yeah now it works
No description
Ori
OriOP12mo ago
When I add a guild command
Kinect3000
Kinect300012mo ago
You don’t manually run the deploy script while testing?
Ori
OriOP12mo ago
what do you mean?
Kinect3000
Kinect300012mo ago
Usually, application command deployment is its own script
Ori
OriOP12mo 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
Kinect3000
Kinect300012mo 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
OriOP12mo 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
Kinect3000
Kinect300012mo ago
I would just have it always deploy Even if there are no commands
Ori
OriOP12mo 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
Want results from more Discord servers?
Add your server