Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 872
Jonathan Avatar asked Jan 20 '21 10:01

Jonathan


2 Answers

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

like image 126
stijndcl Avatar answered Oct 11 '22 02:10

stijndcl


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)
like image 25
FluxedScript Avatar answered Oct 11 '22 02:10

FluxedScript