首页 > 解决方案 > 检查用户是否有角色

问题描述

我正在尝试检查用户是否具有特定角色。我知道@commands.has_role(),但这不适用于我正在制作的内容。我在下面有示例代码,我不确定如何获取它来检查用户是否具有角色。

    @commands.command()
    async def hasthisroletest(self, ctx):
        if ctx.author.role.id == 746831268683186268:
            await ctx.send("You did it!")
        else:
            await ctx.send("Nope :(")

标签: pythondiscorddiscord.py

解决方案


如果想要的角色在那里,您可以检查Member.roles 。

使用discord.utils.get获取角色本身。

@commands.command()
async def hasthisroletest(ctx):
    # you can also use name = 'Test_role'
    role = discord.utils.get(ctx.guild.roles, id=746831268683186268)
    if role in ctx.author.roles:
        await ctx.send("You did it!")
    else:
        await ctx.send("Nope :(")

推荐阅读