如何获取在 discord.py 中引发错误的命令的名称?

How do I get the name of command that raised error in discord.py?

我希望我的机器人生成某种命令日志,我希望它不仅发送“某些命令刚刚执行”,而且如果用户在尝试使用此命令时出现任何错误(缺少 argument/permission 等等)。我想使用 on_command_error() 来实现它,但我仍然需要知道哪个命令引发了错误。有什么办法吗?

因为您从失败的命令中获取了 ctx,所以如果您想要完整的命令对象,可以使用 ctx.invoked_withctx.command 访问命令名称。

...
@bot.event
async def on_command_error(ctx, error):
    command = ctx.invoked_with

参见:https://discordpy.readthedocs.io/en/stable/ext/commands/api.html?highlight=notfound#discord.ext.commands.Bot.on_command_error

https://discordpy.readthedocs.io/en/stable/ext/commands/api.html?highlight=context#discord.ext.commands.Context.command