Axkkzy
Axkkzy

Reputation: 11

discord.py sort roles from highest

@commands.command(aliases=['ui'])
    async def userinfo(self, ctx, member : discord.Member = None):
        if member == None:
            member = ctx.message.author
        else:
            member = member
    
        roles = [role for role in sorted(member.roles, key=lambda role: ctx.guild.roles)]

        embed = discord.Embed(colour=member.colour.purple(), timestamp=ctx.message.created_at)
        embed.set_author(name=f"User Info - {member}")
        embed.set_thumbnail(url=member.avatar_url)
        embed.set_footer(text=f"Requested by {ctx.author}", icon_url=ctx.author.avatar_url)
        embed.add_field(name="ID:", value=member.id)
        embed.add_field(name="Guild name:", value=member.display_name)
        embed.add_field(name="Created at:", value=member.created_at.strftime("%a, %#d %B %Y, %I:%M %p"))
        embed.add_field(name="Joined at:", value=member.joined_at.strftime("%a, %#d %B %Y, %I:%M %p"))
        embed.add_field(name=f"Roles ({len(roles)})", value=" ".join([role.mention for role in roles][1:]))
        embed.add_field(name="Top role:", value=member.top_role.mention)
        embed.add_field(name="Bot?", value=member.bot)

        await ctx.send(embed=embed)

it doesnt give me any errors but it doesnt work as i wanted it to. I want it to sort the roles according to the server's role hierarchy. All I should have to change for it to work is roles but I can't think of what

Upvotes: 1

Views: 448

Answers (1)

Łukasz Kwieciński
Łukasz Kwieciński

Reputation: 15698

The roles should already be sorted by their position in the role hierarchy. Nonetheless you can use the role.position attribute

roles = list(sorted(member.roles, key=lambda role: role.position))

Upvotes: 1

Related Questions