添加异常以告知频道中的用户他们没有权限

时间:2019-07-27 04:33:00

标签: python-3.x discord.py-rewrite

此代码我没有收到任何错误,但是bot仍然不会告诉非管理员用户他们没有权限,它只会停留在终端上,这是我在做什么错了,bot不会这么说或者我需要进行更改以解决此问题,也使用discord.py rewrite

bans a user with a reason
@client.command()
@commands.has_any_role("Keyblade Master","Foretellers")
async def ban (ctx, member:discord.User=None, reason =None):

    adminroles = ("Keyblade Master","Foretellers")

    try:
        if member == None or member == ctx.message.author:
            await ctx.channel.send("You cannot ban yourself")
            return

        elif reason == None:
            reason = "being a jerk!"
            message = f"You have been banned from {ctx.guild.name} for  {reason}"
            await member.send(message)
            # await ctx.guild.ban(member)
            await ctx.channel.send(f"{member} is banned!") 
    except commands.errors.MissingAnyRole(adminroles): 
        await ctx.channel.send("You do not have permission to do that!")
        return

1 个答案:

答案 0 :(得分:0)

在调用回调期间,在调用回调之前引发的错误(如CheckFailureUserInputError及其派生类)需要由单独的error handler处理,因为失败实际上并没有在您的try区块中。

@client.command(name="ban")
@commands.has_any_role("Keyblade Master","Foretellers")
async def ban_command(ctx, member:discord.User=None, reason =None):

    adminroles = ("Keyblade Master","Foretellers")
    if member == None or member == ctx.message.author:
        await ctx.channel.send("You cannot ban yourself")
        return

    elif reason == None:
        reason = "being a jerk!"

    message = f"You have been banned from {ctx.guild.name} for  {reason}"
    await member.send(message)
    # await ctx.guild.ban(member)
    await ctx.send(f"{member} is banned!") 

@ban_command.error
async def ban_error(ctx, error):
    if isinstance(error, commands.MissingAnyRole):
        await ctx.send("You do not have permission to do that!")
    else:
        raise error