简体   繁体   English

显示角色成员 discord.py

[英]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.我正在尝试为 discord 机器人(python)编写代码,它将显示属于特定角色的成员列表。

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.机器人会在单独的消息中发送该角色中每个成员的姓名,但速度非常慢,甚至每次在列出 4-5 个成员后都会“停止”。 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.我不执行 ctx.send(tkm) 的原因是因为它包含太多信息,对于每个成员。 This it can post in the chat in one go.这个可以发在一个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.它不会让我做 tkm.name(这是一个列表,列表没有属性“名称”)我只对名称感兴趣,因此“a = row.name”确实只给我名称成员。 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')我被卡住的地方是:我似乎做不到 tkm = tk.members.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.我在这里想要的是让“a”中的成员回到列表中,然后在聊天中发布新列表。 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.或者,如果有一种方法可以将“tkm”操作为仅以成员名称开头,那也可以。

Thank you:)谢谢:)

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.有关更多信息,请参阅MCVE文章。

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.所以我猜你有一个dicord.member.Member对象列表。 It is likely that each such "Member" has the attribute name .很可能每个这样的“成员”都有属性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".在 Python 中有一种非常好的形式,称为“列表理解”。 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两个forms是一样的

Again, this is a wild guess, but with luck it will work:)同样,这是一个大胆的猜测,但幸运的话它会起作用:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM