我最近使用discord.py制作了一个discord机器人。我尝试为某些命令授予权限。我有这个测试命令功能:
@client.command()
@commands.has_role('Moderator')
async def cool(ctx):
await ctx.send("You are cool indeed!")
当用户没有“主持人”角色时,如何返回消息(错误)?
我已经尝试过这个:
async def on_command_error(ctx, error):
if isinstance(error, commands.NoPrivateMessage):
await ctx.send("*Private messages.* ")
elif isinstance(error, commands.MissingRequiredArgument):
await ctx.send("*Command is missing an argument:* ")
elif isinstance(error, commands.DisabledCommand):
await ctx.send("*This command is currenlty disabled. Please try again later.* ")
elif isinstance(error, commands.CheckFailure):
await ctx.send("*You do not have the permissions to do this.* ")
elif isinstance(error, commands.CommandNotFound):
await ctx.send("*This command is not listed in my dictionary.*")
但我没有从中得到任何返回。
最佳答案
在 on_command_error
函数中,检查 commands.MissingRole
或commands.MissingAnyRole
.
if isinstance(error, (commands.MissingRole, commands.MissingAnyRole)):
...
关于python - 如何从@commands.has_role()获取 "error"消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56580601/