将Discord.py中的角色列入黑名单

时间:2020-06-22 23:20:33

标签: python discord discord.py

尝试了很多事情之后,我想出了一个主意,问你那里有聪明的人!我想将3个角色列入单个命令的黑名单。但是我尝试的一切都失败了。

这是我写的代码:

member = ctx.message.author
roleA = get(member.guild.roles, name="Rote Birdys")
roleB = get(member.guild.roles, name="Grüne Birdys")
roleC = get(member.guild.roles, name="Blaue Birdys")

if commands.has_any_role(roleA, roleB, roleC):
    return

什么都没用,我尝试了更多尝试,但它们也没有用。可能我真的很蠢,但是我需要在几周内完成这个项目,而我被困在这里。

我希望你能帮助我:)

1 个答案:

答案 0 :(得分:0)

这只是has_any_role检查,但倒退。我还创建了一个类似MissingAnyRole的新错误:

from discord.ext.commands import check, CheckFailure
import discord.utils

class HasForbiddenRole(CheckFailure):
    def __init__(self, forbidden_roles):
        self.forbidden_roles = forbidden_roles

        forbidden= ["'{}'".format(role) for role in forbidden_roles]

        if len(missing) > 2:
            fmt = '{}, or {}'.format(", ".join(forbidden[:-1]), forbidden[-1])
        else:
            fmt = ' or '.join(forbidden)

        message = "You have at least one of the forbidden roles: {}".format(fmt)
        super().__init__(message)

def lacks_every_role(*items):
    def predicate(ctx):
        if not isinstance(ctx.channel, discord.abc.GuildChannel):
            raise NoPrivateMessage()

        getter = functools.partial(discord.utils.get, ctx.author.roles)
        if not any(getter(id=item) is not None if isinstance(item, int) else getter(name=item) is not None for item in items):
            return True
        raise HasForbiddenRole(items)

    return check(predicate)

用法与其他任何检查一样:

@lacks_every_role("Rote Birdys", "Grüne Birdys", "Blaue Birdys")  # You could also use role ids
@bot.command()
async def comm(ctx):
    ...