Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
357 views
in Technique[技术] by (71.8m points)

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

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!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...