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



我此代码没有任何错误,但机器人仍然不会告诉非管理员用户他们没有权限,它只是留在终端中,我做错了什么,机器人不会这么说,或者我需要更改什么来解决这个问题,也使用 discord.py 重写

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

在调用回调之前,命令调用期间引发的错误(如CheckFailureUserInputError及其派生(需要由单独的错误处理程序处理,因为失败的代码实际上并不在您的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

相关内容

  • 没有找到相关文章

最新更新