gpt4 book ai didi

python - 如何从@commands.has_role()获取 "error"消息

转载 作者:太空宇宙 更新时间:2023-11-03 20:46:16 25 4
gpt4 key购买 nike

我最近使用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.MissingRolecommands.MissingAnyRole .

if isinstance(error, (commands.MissingRole, commands.MissingAnyRole)):
...

关于python - 如何从@commands.has_role()获取 "error"消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56580601/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com