试图使它成为has:权限使

2024-04-16 22:07:57 发布

您现在位置:Python中文网/ 问答频道 /正文

因此,我试图对我的discord.py重写机器人发出say命令。但是我不知道如何使它在用户没有正确权限时发送文本

@client.command()
@commands.has_permissions(manage_messages = True)
async def say (ctx, *, msg2:str):
        await ctx.send(msg2)

@say.error
async def say_error(error, ctx):
    if isinstance(error, CheckFailure):
        await ctx.send('You need the **Manage messages** permission to use this command')

如果用户具有管理邮件或管理员的权限。我想让它运行say命令。如果他们没有这些权限。我希望它发送:“您需要管理消息权限才能使用此命令”


Tags: 用户py命令send权限asyncdeferror
1条回答
网友
1楼 · 发布于 2024-04-16 22:07:57

您对say_error的参数是反向的。您还可以检查更具体的^{}错误

@say.error
async def say_error(ctx, error):
    if isinstance(error, MissingPermissions):
        await ctx.send('You need the **Manage messages** permission to use this command')
    else:
        raise error

你可以用这样的方法来概括

@say.error
async def say_error(ctx, error):
    if isinstance(error, MissingPermissions):
        await ctx.send(f'You need the **{", ".join(error.missing_perms)}** permission to use this command')
    else:
        raise error

相关问题 更多 >