AbdurJ
AbdurJ

Reputation: 1033

Display role members discord.py

I'm trying to write a code for a discord bot (python) that will show a list of members belonging to a specific role.

Sorry for the long question, I'm new to coding and trying to be as thorough as possible, as I'm learning by doing.

@client.command()
async def team(ctx): # Always same role, no input needed
    guild = ctx.message.guild
    tk = guild.get_role(role_id)
    tkm = tk.members
    # print(type(tkm)) shows it as "list"  
    for row in tkm:   
        a = row.name  
        # print(type(a)) # shows "<class 'discord.member.Member'>" x amount of times
        await ctx.send(a)

This does sort of work. The bot sends the name of each member in that role in separate messages, but it's very slow and even "stops" every time after listing 4-5 members. I have searched around, and only found similar codes.

The reason I'm not doing ctx.send(tkm) is because it contains too much info, for every member. This it can post in the chat in one go.

[<Member id=_________ name='___' discriminator='__' bot=False nick='_____' guild=<Guild i
_______ name='________' shard_id=None chunked=True member_count=28>>,....]

It will not let me do tkm.name (It's a list, and lists don't have attribute 'name') I'm only interested in name, hence the "a = row.name" which does give me just the names of the members. but also results in the list splitting up and giving me these objects

<class 'discord.member.Member'>
<class 'discord.member.Member'>
...
...

Where I'm stuck is: I can't seem to do tkm = tk.members.name (again, because of no attribute 'name')

What I want from here, is to get the members from "a" back in to a list, and then post the new list in the chat. But I can't seem to figure out how. Or if there is a way of manipulating "tkm" to only have member names to begin with that will also works.

Thank you :)

Upvotes: 2

Views: 4362

Answers (1)

exhuma
exhuma

Reputation: 21687

First of all, thank you for going through the effort to write up such a detailed question. This is a bit tricky to answer because it contains a bit of guesswork as we cannot easily reproduce this with a runnable example. See the article for an MCVE for more on this.

BUT... as your question is so detailed it helps doing some "informed guessing". And my guess is this:

I noticed two things: First you have this in your code:

# print(type(a)) # shows "<class 'discord.member.Member'>" x amount of times

and also this:

... It's a list, and lists don't have attribute 'name' ...

So my guess is that you have a list of dicord.member.Member objects. It is likely that each such "Member" has the attribute name. But what you are stuck with is: "How do I get the name of each member from that list?".

You can do this with a "loop". And there is a very nice form of this in Python called a "list comprehension". It would look like this in your case:

names = [item.name for item in a]

You can also write a more "traditional" loop as:

names = []
for item in a:
    names.append(item.name)

The two forms are identical

Again, this is a wild guess, but with luck it will work :)

Upvotes: 2

Related Questions