如何使用新的discord.py版本获取discord服务器中所有成员的列表? [英] How do I get a list of all members in a discord server using the new discord.py version?

查看:413
本文介绍了如何使用新的discord.py版本获取discord服务器中所有成员的列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近更新了discord.py,看来我的一些旧命令有误。我需要遍历不和谐服务器的所有成员,但是我以前使用它的旧方法不再起作用。这是我的旧代码。

I recently updated my discord.py and it seems some of my older commands are wrong. I need to loop through all the members of a discord server but the old way I did it does not work anymore. Heres my old code.

@bot.command(pass_context = True)
async def missing(ctx, channel : str = None, useDiscordID : bool = False):
    memberlist = []
    for member in message.server.members:
        toAppend = ''
        if "barcode" in [y.name.lower() for y in member.roles]:
            if member.nick is None:
                toAppend = member.name
           else:
                toAppend = member.nick
            if useDiscordID:
                toAppend = f'{str(member)} : {toAppend}'
            memberlist.append(toAppend)

这是无效的代码部分,我不知道通过message.server中的成员循环遍历服务器所有成员的新方法是什么.members:不再起作用。谢谢您的帮助!

this is the part of the code that doesnt work, I dont know what the new way to loop through all the members of the server is since for member in message.server.members: doesnt work anymore. Thank you for help!

推荐答案

以下代码段将返回一个生成器,其中包含客户端可以看到的每个成员,即您的机器人可以看到的机器人是其成员的所有服务器。

Below snippet will return a generator with every 'Member' the client i.e your bot can see, across all the servers the bot is a member of.

@client.event
async def on_message(message):
    if message.content.startswith('!member'):
        for guild in client.guilds:
            for member in guild.members:
                print(member) # or do whatever you wish with the member detail

这篇关于如何使用新的discord.py版本获取discord服务器中所有成员的列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆