0
votes

I just tried adding a listrole command on my discord bot which lists every user with a certain role. However I can't seem to find a solution where my bot sends the whole list as one single message, it sends out every single member in the list as one message, which gets annoying with a large amount of members.

Here's my code so far:

@bot.command()
@commands.has_permissions(administrator=True)
async def listrole(ctx, role:discord.Role):
    members = role.members
    if len(members) > 100:
        await ctx.send("Too many members to list")
    else:
        for member in members:
            memberlist = ''.join(f"{member.display_name}#{member.discriminator}")
            await ctx.send(memberlist)

Help is greatly appreciated, I'm very new to Python in general

1
Also, str(member) will give the members username + discriminatorJoshua Nixon

1 Answers

0
votes

Define your memberlist before the loop, and append to it. Then, join all the items in that list into one long string separated by newlines, and send it after the loop completes:

memberlist = []
for member in members:
    memberlist.append(f"{member.display_name}#{member.discriminator}")
await ctx.send('\n'.join(memberlist))