【问题标题】:How can you check if a user has a particular role in a particular server [closed]如何检查用户是否在特定服务器中具有特定角色 [关闭]
【发布时间】:2020-09-19 22:15:31
【问题描述】:

我想为我的机器人制作 Patreon Only 命令,但我不知道如何制作它,因此我可以根据 1 个服务器中的 1 个角色限制对命令的访问,我希望该命令在全局范围内工作,但是我希望我的支持服务器上的“Patreon”角色的人只能访问该命令。

【问题讨论】:

    标签: python discord discord.py discord.py-rewrite patreon


    【解决方案1】:

    您可以使用discord.utils.get 获取Patreon 角色,然后遍历成员的角色和一个简单的if-else

    @client.command()
    async def test(ctx):
        homeGuild = client.get_guild(7300646418576xxxxx)  # Home Guild/Support Server; where user has "Patreon" role.
        patreonRole = get(homeGuild.roles, id=7570088168301xxxxx)  # Patreon role ID that a user must have.
        member = []
    
        for pledger in homeGuild.members:
            if pledger == ctx.author:
                member = pledger
    
        if patreonRole in member.roles:
            await ctx.send("You are a Patreon.")
        else:
            await ctx.send("You are not a Patreon.")
    

    【讨论】:

    • 我猜你放的ID是角色ID,对吧?
    • 是的,代码里有注释。
    • 它只适用于支持服务器(角色来自哪里)而不是其他任何地方,有什么办法可以解决这个问题吗?
    • 当然,检查我的编辑。
    【解决方案2】:

    在您的命令中,使用get_guild 获取您的支持服务器公会,然后使用Guild.get_member 获取执行命令的任何人的成员对象(ctx.author.id)。然后您可以检查您的Patreon 角色是否在该成员的Member.roles 中(我会使用utils.get),如果不是则退出命令。

    【讨论】:

    • 大声笑,另一个人在我的解决方案中编辑的非常酷
    猜你喜欢
    • 1970-01-01
    • 2010-12-22
    • 2019-06-16
    • 2021-10-25
    • 2021-10-10
    • 1970-01-01
    • 2016-05-02
    • 2018-11-06
    相关资源
    最近更新 更多