簡體   English   中英

檢查用戶是否有角色

[英]Check if a user has a role

我正在嘗試檢查用戶是否具有特定角色。 我知道@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 :(")

如果想要的角色在那里,您可以檢查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 :(")

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM