我正在尝试创建一个向公会中的每个实际用户赋予特定角色的命令,机器人除外。 我有的代码:
@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)
这会获取行会中的每个人(机器人也是如此)。如何排除漫游器并仅获取真实用户?
答案 0 :(得分:3)
Member对象具有bot
属性,如果用户是机器人,则该属性为true,因此
for member in guild.members:
if member.bot:
continue
await member.add_roles(role)
答案 1 :(得分:2)
discord.Member
对象具有bot
属性,该属性返回True
或False
,具体取决于成员是否是漫游器:
@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)