Reputation: 145
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!
Upvotes: 5
Views: 34577
Reputation: 190
They made a security update to this.
And I quote
Currently, this requires opting in explicitly via the developer portal as well. Bots in over 100 guilds will need to apply to Discord for verification.
So firstly you need to go into your developer account and check the Servers Member Intent option.
Then call the following:
m2 = await g.fetch_members(limit=None).flatten()
Where g is the retrieved guild object as above. The above method only returns one user.
I also to be on the safe side did the following at the beginning of my code:
# these are the modules imported
from discord.ext.commands.bot import Bot
import discord
# just for reference
discord.MemberCacheFlags.all()
bot = Bot("$")
# the bots flags and the global intents flags set to True
# explicitly.
bot.intents.members=True
Intents.members=True
Of interest all of the following will not work, only the api call returns more than 1 member for me.
guild.members
bot.get_all_members():
bot.users
Upvotes: 0
Reputation: 1092
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
Upvotes: 6
Reputation: 6134
The migration guide mentions that server
has been renamed to guild
. The correct code should be message.guild.members
.
Upvotes: 2