kezz
kezz

Reputation: 107

How can I get it all in one embed?

I want to make a command to see all members with a specific role in that server. But with what I have now I get an embed for every single member that has that role. But I want it all in one embed, but how?

@bot.command(name='members', aliases=['Members'])
@commands.has_permissions(manage_messages=True)
async def members(ctx,*args):
    server = ctx.message.guild
    role_name = (' '.join(args))
    role_id = server.roles[0]
    for role in server.roles:
        if role_name == role.name:
            role_id = role
            break
    else:
        await ctx.send("Role doesn't exist")
        return
    for member in server.members:
        if role_id in member.roles:
            embed=discord.Embed(title=f"Members of {role}",description=f"{member.display_name} - {member.id}", color=0xfcf8f8)
            await ctx.send(embed=embed)

Upvotes: 0

Views: 120

Answers (2)

Just for fun
Just for fun

Reputation: 4235

Here are few things to keep in mind

  1. Instead of looping through Guild.roles you can use discord.utils.get.
  2. Instead of looping through Guild.members and checking if member has the role, we have Role.members which returns a list of members having that role.
  3. Instead of a for loop, join() can be used and code can be minimized.
@bot.command(name='members', aliases=['Members'])
@commands.has_permissions(manage_messages=True)
async def members(ctx,*args):
    name = " ".join(args)
    role = discord.utils.get(ctx.guild.roles, name=name)
    if not role:
        return await ctx.send("Role not found")
    member_string = "\n".join([i.display_name + " - " + str(i.id) for i in role.members])
    e = discord.Embed(title=f"Members of {role.name}", description=member_string, color=0xfcf8f8)
    await ctx.send(embed=e)

Upvotes: 2

jreiss1923
jreiss1923

Reputation: 474

What you're doing now is looping through each member and sending an embed if they have the role you want. As a result you're sending one embed for each member, rather than sending one embed containing all the information you want. One possible solution I wrote is to create a string that contains all the members and their information, then send that in one embed.

member_string = ""

for member in server.members:
    if role_id in member.roles:
        member_string += "\nmember.display_name - member.id"
embed = discord.Embed(title=f"Members of {role}",description=member_string, color=0xfcf8f8)
await ctx.send(embed=embed)

Upvotes: 1

Related Questions