如何在discord.py中获取实际用户并排除漫游器?

时间:2020-09-25 15:30:27

标签: python discord discord.py

我正在尝试创建一个向公会中的每个实际用户赋予特定角色的命令,机器人除外。 我有的代码:

    @commands.command()
    @commands.has_permissions(manage_roles=True)
    async def maddrole(self,ctx,role: discord.Role):
        guild = ctx.guild
        for member in guild.members:
            await member.add_roles(role)
        embed= discord.Embed(description=f'{role} have been given to all users.',color=discord.Color.red())
        await ctx.channel.send(embed=embed)

这会获取行会中的每个人(机器人也是如此)。如何排除漫游器并仅获取真实用户?

2 个答案:

答案 0 :(得分:3)

Member对象具有bot属性,如果用户是机器人,则该属性为true,因此

for member in guild.members:
    if member.bot:
        continue
    await member.add_roles(role)

答案 1 :(得分:2)

discord.Member对象具有bot属性,该属性返回TrueFalse,具体取决于成员是否是漫游器:

@commands.command()
@commands.has_permissions(manage_roles=True)
async def maddrole(self,ctx,role: discord.Role):
    guild = ctx.guild
    for member in guild.members:
        if member.bot:
            continue
        await member.add_roles(role)
    embed= discord.Embed(description=f'{role} have been given to all users.',color=discord.Color.red())
    await ctx.channel.send(embed=embed)