如何在消息中发送成员拥有的所有角色

时间:2021-02-12 17:52:55

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

我正在尝试发送一条消息,其中机器人会显示用户拥有的所有角色。我只想让机器人在消息中提及角色。这是我正在使用的代码:

    @commands.command()
    async def whois(self, ctx, user: discord.Member=None):
        if user:
            roles = user.roles
            embed = discord.Embed(
                description = user.mention
            )
            embed.add_field(name='Roles', value=roles)
            embed.set_thumbnail(url=user.avatar_url)
            embed.set_author(name=user.name, icon_url=user.avatar_url)
            await ctx.send(embed = embed)

这就是我在邮件中得到的信息:

[<Role id=785571867279753238 name='@everyone'>, <Role id=787317902906490940 name='admin'>]

1 个答案:

答案 0 :(得分:1)

你可以使用一点list comprehension来获得提及,然后你可以使用str.join(list)方法来获得一个字符串

mentions_list = [role.mention for role in roles]
mentions = "\n".join(mentions_list) # Using newline as the delimiter

或者如果你想要一个单线

mentions = "\n".join([role.mention for role in roles]) # Using newline as the delimiter

编辑

mentions = "\n".join([role.mention if role.name != "@everyone" else role.name for role in roles])