因此,我正在执行whois命令,而我正在执行的if语句将其执行,以便如果用户没有角色,则value = None。但是,我有麻烦。这是代码
@commands.command(aliases=["whois"])
async def userinfo(self, ctx, member: discord.Member = None):
if not member:
member = ctx.message.author
embed = discord.Embed(colour=discord.Colour(random.randint(1, 16777215)), timestamp=ctx.message.created_at,title=f"User Info - {member}")
embed.set_thumbnail(url=member.avatar_url)
embed.add_field(name="Name", value=member.name)
embed.add_field(name="ID:", value=member.id)
embed.add_field(name="Nickname:", value=member.display_name)
embed.add_field(name="Status", value=member.status)
embed.add_field(name="Created Account On:", value=member.created_at.strftime("%a, %#d %B %Y, %I:%M UTC"))
embed.add_field(name="Joined Server On:", value=(member.joined_at.strftime("%a, %#d %B %Y, %I:%M %p UTC")))
roles = [role.mention for role in member.roles[1:]]
embed.add_field(name=f"Roles({len(roles)}):",value=",".join(roles),
inline=False)
embed.add_field(name="Highest Role:", value=member.top_role.mention)
await ctx.send(embed=embed)
if roles == None:
embed.add_field(name="Roles", value="None", inline=False)
所以我问我的朋友,谁是python的佼佼者,他就把这个发给我:
@commands.command(aliases=["whois"])
async def userinfo(self, ctx, member: discord.Member = None):
if not member:
member = ctx.message.author
embed = discord.Embed(colour=discord.Colour(random.randint(1, 16777215)), timestamp=ctx.message.created_at,title=f"User Info - {member}")
embed.set_thumbnail(url=member.avatar_url)
embed.add_field(name="Name", value=member.name)
embed.add_field(name="ID:", value=member.id)
embed.add_field(name="Nickname:", value=member.display_name)
embed.add_field(name="Status", value=member.status)
embed.add_field(name="Created Account On:", value=member.created_at.strftime("%a, %#d %B %Y, %I:%M UTC"))
embed.add_field(name="Joined Server On:", value=(member.joined_at.strftime("%a, %#d %B %Y, %I:%M %p UTC")))
roles = [role.mention for role in member.roles[1:]]
if roles == None: embed.add_field(name=f"Roles({len(roles)}):",value=",".join(roles), inline=False)
embed.add_field(name="Highest Role:", value=member.top_role.mention)
await ctx.send(embed=embed)
这是错误
In embed.fields.6.value: This field is required
它可以工作,但是问题在于它显示了@@ everyone角色,我如何摆脱@@ everyone角色?
答案 0 :(得分:0)
这是固定的代码,@everyone
角色始终是成员的第一个角色,因此只需尝试访问第二个元素member.roles[1:]
中的列表即可将其排除,嵌入,但用户除@everyone
角色外没有其他任何角色,因为roles
列表为空,因此会吐出一个错误,因此您必须进行检查以防止插入空值。
commands.command(aliases=["whois"])
async def userinfo(self, ctx, member: discord.Member = None):
if not member:
member = ctx.message.author
embed = discord.Embed(colour=discord.Colour(random.randint(1, 16777215)), timestamp=ctx.message.created_at,title=f"User Info - {member}")
embed.set_thumbnail(url=member.avatar_url)
embed.add_field(name="Name", value=member.name)
embed.add_field(name="ID:", value=member.id)
embed.add_field(name="Nickname:", value=member.display_name)
embed.add_field(name="Status", value=member.status)
embed.add_field(name="Created Account On:", value=member.created_at.strftime("%a, %#d %B %Y, %I:%M UTC"))
embed.add_field(name="Joined Server On:", value=(member.joined_at.strftime("%a, %#d %B %Y, %I:%M %p UTC")))
# Fixed part
roles = [role.mention for role in member.roles[1:]]
if len(member.roles[1:]) < 1:
embed.add_field(name=f"Roles:",value="None", inline=False)
embed.add_field(name="Highest Role:", value="None")
elif roles != None:
embed.add_field(name=f"Roles({len(roles)}):",value=",".join(roles), inline=False)
embed.add_field(name="Highest Role:", value=member.top_role.mention)
# End of fix
await ctx.send(embed=embed)