尝试在出现错误时将不和谐的重命名权限错误转换为默认消息,我该怎么做?

Trying To Convert a discord rename permission error to a default message when that error comes up, How Do I do That?

@rename.error
async def rename_error(ctx, error):
    if isinstance(error, PermissionError):
        await ctx.send("I don't have the permission to do that!¯\_(ツ)_/¯")
    else:
        raise error

这是我当前的代码。我想做的是让我的机器人给出默认响应,而不是在它没有重命名某个管理员的权限时告诉它在我的控制台中引发错误。我对这个错误转换完全陌生,我实际上复制并粘贴了它,但它仍然对我不起作用。有人可以帮我吗?

discord.ext.commands.errors.CommandInvokeError: Command raised an exception: Forbidden: 403 Forbidden (error code: 50013): Missing Permissions

这是我试图转换为默认消息的错误。

您应该使用 on_command_error 事件,它有两个参数:


使用方法:

from discord.ext import commands

@bot.event #or client.event
async def on_command_error(ctx, error):
    if isinstance(error, commands.CommandInvokeError) and "Missing Permissions" in str(error):
        await ctx.send("I don't have the permission to do that!¯\_(ツ)_/¯")
    else:
        raise error

您会发现每个 API 个错误 here