Z
Z
znsxwq2022-02-18 00:06:15
Python
znsxwq, 2022-02-18 00:06:15

How to get only three user roles on a discord server?

Good evening everyone! I'm making a bot for a discord server and want to make a profile command. In the profile, I want to display the user's roles, but not all, but only three.

@client.command(aliases = ['me', 'profile'])
async def __profile(ctx, member: discord.Member = None):
    if member is None:
        mentions = [role.mention for role in ctx.author.roles[1:]]
        rolescount = len(mentions)
        embed = discord.Embed(title = f'Профиль - {ctx.author}', 
            description = f"**```fix\nО себе: {cursor.execute('SELECT bio FROM users WHERE id = ?', (ctx.author.id,)).fetchone()[0]}```**" , color = 0xb5bceb)

        t = ctx.message.author.status
        if t == discord.Status.online:
             d = "В сети"

        t = ctx.message.author.status
        if t == discord.Status.offline:
            d = "Не в сети"

        t = ctx.message.author.status
        if t == discord.Status.idle:
            d = "Не активен"

        t = ctx.message.author.status
        if t == discord.Status.dnd:
            d = "Не беспокоить"
        

        embed.set_thumbnail(url=f'{ctx.author.avatar_url}')
        embed.add_field(name=':cookie:Сообщений', value=f"```ini\n{cursor.execute('SELECT message_count FROM users WHERE id = ?', (ctx.author.id,)).fetchone()[0]}```", inline = True)
        embed.add_field(name=':sparkles:Репутация', value=f"```ini\n{cursor.execute('SELECT rep FROM users WHERE id = ?', (ctx.author.id,)).fetchone()[0]}```", inline = True)
        embed.add_field(name=":computer:Активность", value=f"**```{d}```**",inline=True)
        embed.add_field(name=f"Роли ({rolescount})", value=f"".join(mentions))
        embed.add_field(name=":calendar:Возраст аккаунта / Присоединился к серверу", value=f"**```{ctx.message.author.created_at.strftime('%d.%m.%Y')} / {ctx.message.author.joined_at.strftime('%d.%m.%Y')}```**", inline=False)
            

        await ctx.message.delete()
        await ctx.send(embed=embed)

The bot displays all user roles, can you tell me how to make only three?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Nevzorov, 2022-02-23
@fixator10

The easiest option is to add another slice.

mentions = [role.mention for role in ctx.author.roles[1:][:3]]   # "Первые" 3 роли (в интерфейсе Дискорда - нижние роли)
mentions = [role.mention for role in ctx.author.roles[1:][-3:]]  # "Последние" 3 роли (в интерфейсе Дискорда - верхние роли)

If it is necessary to show only three specific roles:
ROLES = [945967796849310659, 945967801044219854, 945967796847229780]
mentions = [role.mention for role in ctx.author.roles[1:] if role.id in ROLES]

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question