如何检查用户是否在不和谐中扮演特定角色

时间:2019-02-23 20:27:28

标签: python python-3.x discord.py

这应该检查特定人员是否具有静音角色

    @bot.command(pass_context=True)
    @commands.has_role("Admin")
    async def unmute(ctx, user: discord.Member):
        role = discord.utils.find(lambda r: r.name == 'Member', 
    ctx.message.server.roles)
        if user.has_role(role):
            await bot.say("{} is not muted".format(user))
        else:
            await bot.add_roles(user, role)

抛出此错误

  

命令引发异常:AttributeError:“成员”对象没有属性“ has_role”

我不知道该怎么做,所以我将非常感谢我所能提供的一切帮助

2 个答案:

答案 0 :(得分:1)

成员没有.has_role()方法,但是您可以使用.roles获取其所有角色的列表。

要查看用户是否具有给定角色,我们可以使用role in user.roles

    @bot.command(pass_context=True)
    @commands.has_role("Admin")
    async def unmute(ctx, user: discord.Member):
        role = discord.utils.find(lambda r: r.name == 'Member', ctx.message.server.roles)
        if role in user.roles:
            await bot.say("{} is not muted".format(user))
        else:
            await bot.add_roles(user, role)

供参考的文档:https://discordpy.readthedocs.io/en/latest/api.html#member

答案 1 :(得分:1)

如果有人在重写后看到这个,语法已经改变了一点,这是更新的代码。

要查看成员是否具有指定的角色,我们可以使用 role in member.roles

@bot.command()
@commands.has_role("Admin")
async def unmute(ctx, member: discord.Member):
    role = discord.utils.get(ctx.guild.roles, name="Member")
    if role in member.roles:
        await ctx.send(f"{member} is not muted")
    else:
        await member.add_roles(role)

参考文档:

成员:discord。成员:https://discordpy.readthedocs.io/en/latest/ext/commands/commands.html#converters

has_role:

https://discordpy.readthedocs.io/en/latest/ext/commands/api.html#discord.ext.commands.has_role

用户角色:https://discordpy.readthedocs.io/en/latest/api.html#discord.Member.roles

member.add_roles:https://discordpy.readthedocs.io/en/latest/api.html#discord.Member.add_roles

相关问题