如何将角色列入白名单以防止被禁止

How to whitelist roles from being banned

我之前问过这个问题,但由于我对堆栈溢出的经验几乎为零,所以把它搞砸了。所以我会用答案重写这个问题,这样寻找这个的人可以马上得到它。

所以我的问题是,我怎样才能将角色列入白名单以防止被禁止。现在,随着 discord.py 的更多经验,我可以告诉您下面列出的答案是做到这一点的方法。

工作代码:归功于 CaptAngryEyes

@bot.command()
async def ban(ctx, member : discord.Member):
    whitelisted_roles = [728345678923456, 923478569378456, 8923475627893456] # Put the role IDs here
    for role in member.roles:
        if role.id in whitelisted_roles:
            await ctx.send("You can't ban this user! He is a moderator!")
            return
        else:
            # Ban code goes here...
            pass

您可以遍历用户的角色并检查他是否在列表中有角色。我使用角色 ID,因为它无法更改。

@bot.command()
async def ban(ctx, member : discord.Member):
    whitelisted_roles = [728345678923456, 923478569378456, 8923475627893456] # Put the role IDs here
    for role in member.roles:
        if role.id in whitelisted_roles:
            await ctx.send("You can't ban this user! He is a moderator!")
            return
        else:
            # Ban code goes here...
            pass

如果他有“白名单”角色,我们 return 什么都不做,命令结束。