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

> discord-dev-env@1.3.1 start
> node scripts/start.js && npm run build && cd dist && node src/index.js


> discord-dev-env@1.3.1 build
> 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

> discord-dev-env@1.3.1 start
> node scripts/start.js && npm run build && cd dist && node src/index.js


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