12

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!

3 Answers 3

11

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

Sign up to request clarification or add additional context in comments.

Comments

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)

Comments

-1

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])

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.