在聊天中显示错误无法获得不和谐

问题描述 投票:1回答:1

我试图通过吐出You do not have the permissions {}让我的机器人告诉用户是否有错误但是当我尝试使用此代码时:

@client.command(pass_context = True)
async def ban(ctx, member : discord.Member, *, content: str):
    if ctx.message.author == client.user:
        return
    if ctx.message.author.server_permissions.administrator:
        msg = (str(member) + "has been banned for" + str(content)).format(ctx.message)
        await client.send_message(member, content)
        await client.ban(member)
        await client.send_message(ctx.message.channel, msg)
@ban.error
async def ban_error(error, ctx):
    if isinstance(error, CheckFailure):
        msg = "Sorry but you do not have the permissions {}".format(ctx.message.author.mention)  
        await client.send_message(ctx.message.channel, msg)

discord bot dms用户并且python控制台中没有错误,如果我删除@ban.error片段,我得到的权限错误太低。

python python-3.x discord discord.py
1个回答
1
投票

如果CheckFailure失败,check只会被提升。您的代码中没有检查,因此这种情况永远不会发生。您可以使用ifcommands.has_permission语句转换为非常简单的检查:

@client.command(pass_context = True)
@has_permissions(administrator=True)
async def ban(ctx, member : discord.Member, *, content: str):
    msg = "{} has been banned for {}".format(ctx.message.author.mention, content)
    await client.send_message(member, content)
    await client.ban(member)
    await client.send_message(ctx.message.channel, msg)

情况可能是错误处理程序正在抑制有价值的错误信息。我们可以调用内置错误处理的机器人来处理任何其他不是CheckFailures的错误

from discord.ext.commands import CheckFailure
from discord import Forbidden


@ban.error
async def ban_error(error, ctx):
    if isinstance(error, CheckFailure):
        msg = "Sorry but you do not have the permissions {}".format(ctx.message.author.mention)  
        await client.send_message(ctx.message.channel, msg)
    elif isinstance(error, Forbidden):
        await client.send_message(ctx.message.channel, "I do not have the correct permissions")
    else:
        print(error)
        await client.on_command_error(error, ctx)
© www.soinside.com 2019 - 2024. All rights reserved.