问题
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