Reputation: 107
Goal:
Send a custom message to the user as an interaction response when an error occurs while running the slash command notifying them of the error and why it may have happened.
Current status:
I used to use text commands for my bots, so the error handling was simple as listening for the on_command_error()
event and sending a context.reply to the text command as so:
@bot.event
async def on_command_error(ctx,error):
if isinstance(error, discord.ext.commands.errors.MissingPermissions):
await ctx.reply("You don't have the perms to do that.")
This same event didn't work for the slash commands I made.
The slash commands themselves work as intended.
Here's a barebones example of what I'm doing as a command:
@bot.tree.command(name="test_command")
async def test(interaction: discord.Interaction):
await interaction.response.defer(ephemeral=True)
await asyncio.sleep(5)
await interaction.followup.send("Command works.")
But in the case of, for example if the response fails without getting deferred, the command simply fails and the Console shows a 404 error.
Purpose of question:
I want a way to do Goal globally. How do I modify on_command_error()
in a way that will work with slash commands or is there a separate handler for slash command exceptions?
Upvotes: 0
Views: 617
Reputation: 718
You can register the error handler for the command tree by using the bot.tree.error
decorator to register a coroutine to be the error handler, like with the on_command_error
but with discord.Interaction
instead of discord.ext.commands.Context
. Demo:
@bot.tree.error
async def on_app_command_error(interaction: discord.Interaction, error):
# handle the errors here
...
Upvotes: 1