Simplezes
Simplezes

Reputation: 106

Multiple results in the same Embed | Discord.py

How do i include multiple results in the same embed?

Heres the code btw

@client.command()
async def list(ctx):
    role = discord.utils.get(ctx.guild.roles, name="mute")
    for member in ctx.guild.members:
        if role in member.roles:
            embed = discord.Embed(title="Mute members")
            embed.add_field(name="Name", value=f"**{member.name}**",inline=False)
            embed.add_field(name="ID", value=f"{member.id}",inline=True)
            await ctx.send(embed=embed)
            empty = False
    if empty:
        await ctx.send("Nobody has the role {}".format(role.mention))

The thing now is. When there are multiples mute members, the bot sends different embeds. and i want all the results in the same embeds

Upvotes: 1

Views: 591

Answers (1)

Diggy.
Diggy.

Reputation: 6944

Here's an example using a fair amount of list comprehension:

@client.command()
async def list(ctx):
    role = discord.utils.get(ctx.guild.roles, name="mute")
    muted = [(m.name, m.id) for m in ctx.guild.members if "mute" in [r.name for r in m.roles]]
    if len(muted) > 0:
        embed = discord.Embed(title="Muted members")
        embed.add_field(name="Names", value=f"**{', '.join([i[0] for i in muted])}**",inline=False)
        embed.add_field(name="ID", value=f"{', '.join([str(i[1]) for i in muted])}",inline=True)
        await ctx.send(embed=embed)
    else:
        await ctx.send(f"Nobody has the role {role.mention}")

It makes a list of tuples, in the format:
[("name", 112233445566778899), ....
Which are then retrieved later through another comprehension, getting the first element of each tuple as the name, and the second element as the ID.

The ID has to be converted to a string for .join() to work, hence str(i[1]).


References:

Upvotes: 2

Related Questions