我有自己的错误处理代码,我尝试当用户没有禁止/踢等人的权限时,它会发送错误并说明缺少什么权限。现在,它只说
Application did not respond
(我使用@client.tree.commands)。如何让它发送错误信息?这是我的错误处理代码:
@client.event
async def on_command_error(ctx, error):
if isinstance(error, commands.errors.CommandNotFound):
return
elif isinstance(error, commands.MissingRequiredArgument):
return
elif isinstance(error, commands.errors.MissingPermissions) or isinstance(error, discord.Forbidden):
required_permission = error.missing_perms[0] if isinstance(error, commands.MissingPermissions) else "Unknown Permission"
noperms = discord.Embed(title="<a:wrong:1199385940263706765> ERROR",
description=f"```\nNO PERMISSIONS TO USE THIS COMMAND\n```\n**REQUIRED PERMISSIONS:**\n```\n{required_permission}\n```",
colour=0xf50000)
await ctx.send(embed=noperms)
elif isinstance(error, commands.errors.CommandOnCooldown):
cooldown = discord.Embed(title="<a:wrong:1199385940263706765> ERROR",
description=f"```\nYOU NEED TO WAIT {error.retry_after:.2f} BEFORE TRYING THIS COMMAND AGAIN.\n```",
colour=0xf50000)
await interaction.response.send_message(embed=cooldown)
elif isinstance(error, commands.errors.MissingRole):
if ctx.command.name in [""]:
await ctx.send("You need premium to use this command")
else:
error = discord.Embed(title="<a:wrong:1199385940263706765> ERROR",
description=f"**SOMETHING WEIRD HAPPENED!?**\n__**SEND THIS ERROR CODE TO BOT DEVELOPERS:**__\n\n```\n{error}\n```",
colour=0xf50000)
await ctx.send(embed=error)
我的朋友尝试在没有权限的情况下使用它,它应该发送带有权限的错误消息朋友需要使用它
on_command_error
是正常命令的事件,而不是斜杠命令。
要为应用程序(斜杠)命令创建全局错误处理程序,请使用
tree.error
装饰器:
from discord import app_commands, Interaction
tree = client.tree
@tree.error
async def on_app_command_error(
interaction: Interaction,
error: app_commands.AppCommandError
):
if isinstance(error, app_commands.MissingPermissions):
... # missing perms
elif isinstance(error, app_commands.MissingRole):
... # missing role
elif isinstance(error, app_commands.CommandOnCooldown):
... # command on cooldown