-
While coding a cog for handling exceptions inside my own bot, I stumbled upon some new stuff inside the documentation. If somebody could please explain the use of |
Beta Was this translation helpful? Give feedback.
Answered by
Soheab
Aug 28, 2022
Replies: 1 comment 1 reply
-
The
from discord import app_commands, Interaction
from discord.ext import commands
class MyCog(commands.Cog):
async def cog_app_command_error(
self,
interaction: Interaction,
error: app_commands.AppCommandError
):
# disclaimer: this is an example implementation.
print("An error occurred in the following command:", interaction.command, "error:", str(error))
raise error
from discord import app_commands, Interaction
from discord.ext import commands
class MyCog(commands.Cog):
def __init__(self, bot: commands.Bot):
...
# assign the handler
bot.tree.on_error = self.global_app_command_error
async def global_app_command_error(
self,
interaction: Interaction,
error: app_commands.AppCommandError
):
# disclaimer: this is an example implementation.
print("An error occurred in the following command:", interaction.command, "error:", str(error))
raise error
async def setup(bot: commands.Bot):
await bot.add_cog(MyCog(bot))
from discord import app_commands, Interaction
# subclassing CommandTree
class MyCommandTree(app_commands.CommandTree):
# overriding on_error
async def on_error(
self,
interaction: Interaction,
error: app_commands.AppCommandError
):
# disclaimer: this is an example implementation.
print("An error occurred in the following command:", interaction.command, "error:", str(error))
raise error
# using it
# discord.Client
# tree = MyCommandTree(client)
# -> commands.Bot
bot = commands.Bot(
command_prefix=...,
intents=...,
tree_cls=MyCommandTree # passing the class, not calling it.
) |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
hitblast
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The
@CommandTree.error
decorator cannot be used inside a cog but there are alternatives.Cog.cog_app_command_error
, this is a special method that is called for errors that occur for app commands that are defined in that cog.