'Get all members discord.py
I've got a question, I need to get a list from all members from all servers where the bot is online(I'm using discord.py rewrite), Right now I have this code-snipped:
@bot.command()
async def members(ctx):
for guild in bot.guilds:
for member in guild.members:
print(member)
The program outputs the Bots name 3 times because the bot is on 3 servers.
Thank you!
Solution 1:[1]
Sounds like an Intents
issue. You need to enable the members
intent when creating your commands.Bot
instance, and pass your intents into it.
intents = discord.Intents.default()
intents.members = True
client = commands.Bot(intents=intents, prefix_and_other_things_go_here)
You also have to enable it in your developer portal. More info on how & where: https://discordpy.readthedocs.io/en/latest/intents.html
Solution 2:[2]
Your code is correct. However you require the Members Intent
You will have to enable it here. Select the application you want it on -> Select Bot
-> SERVER MEMBERS INTENT and then make sure it shows blue next to it. Then click Save Changes. Since you are developing your bot you might want to enable Presence intent as well to save you time later.
You will then have to edit your bot variable like so:
intents = discord.Intents()
intents.all()
bot = commands.Bot(command_prefix=".", intents=intents)
Solution 3:[3]
here is a one-liner solution
@client.command()
async def getmember(ctx,member:str):
print([i.name+'#'+i.discriminator for i in ctx.message.guild.members if member in i.name+'#'+i.discriminator])
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | stijndcl |
Solution 2 | Åukasz KwieciÅ„ski |
Solution 3 | AssaultOPS |