How do I get a list of all members in a discord server using the new discord.py version?

泄露秘密 提交于 2020-01-15 12:14:11

问题


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)

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!


回答1:


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



回答2:


The migration guide mentions that server has been renamed to guild. The correct code should be message.guild.members.



来源:https://stackoverflow.com/questions/56519760/how-do-i-get-a-list-of-all-members-in-a-discord-server-using-the-new-discord-py

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!