简体   繁体   English

discord.py:循环通过成员列表不起作用

[英]discord.py: looping thru a list of members does not work

@bot.command()
async def start(ctx):
     CUSTOMER_IDS = []

     for member in ctx.guild.members:
          for role in member.roles: 
               if role.id == CUSTOMER_ROLE_ID: 
                    print(f'{member} has the customer role!')
                    CUSTOMER_IDS.append(str(member))

               else:
                    print(f'{member} does not have the customer role!') 

     print(f'\n{CUSTOMER_IDS}')
     await ctx.reply(f'List: `{CUSTOMER_IDS}`')

its supposed to go thru a loop of the guild's members and validate wether they have a role or not.它应该通过公会成员的循环来 go 并验证他们是否有角色。 I tested this on my server with about 500 members (300+ have the role) and it only returned 1 member and 2 checks of the bot itself.我在我的服务器上用大约 500 名成员(超过 300 名成员)对此进行了测试,它只返回了 1 名成员和 2 次对机器人本身的检查。

未知

I feel what is most likely happening in this scenario is that discord.py rightfully doesn't keep all of that data cached.我觉得在这种情况下最有可能发生的是 discord.py 正确地没有缓存所有这些数据。 Instead what you'll need to do is make the REST API requests for what you are trying to do.相反,您需要做的是让 REST API 请求您尝试执行的操作。 So in this case, you could try the following:因此,在这种情况下,您可以尝试以下操作:

guild = bot.get_guild(ctx.guild.id)

# It is far more efficient to get the members from the role rather
# the role from the members.
role = guild.get_role(CUSTOMER_ROLE_ID)
customers = role.members

# from here you have a list with all the member objects of people with the role
# so you can do as you wish with them from there, for example:

print(f"Customer ID's: {[m.id for m in customers]}")

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

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