guild.members not working correctly discord.py

Viewed 5889

I am trying to get a list of members in a server using discord.py

My code is:

@client.command()
async def members(ctx):
    
    members = ctx.guild.members

    for member in members:

        await ctx.send(member.name)

    await ctx.send("done")

But the problem is it's sending the name of the bot only. If you figure out what's the problem in the code then can you pls help me, I want the name of all members.

2 Answers

i'll give you a brief answer, so that you can understand very well.

intents are as same as permissions, in a way

so what is their use? intents are required if you wanna get certain events happening in server. some can be enabled just by the script, and some intents like presence and members require going to discord developer portal and manually enabling + enabling in script

so you just open your script and go to top, add these lines:

from discord.ext import commands
intents = discord.Intents(messages=True, members=True)
client = commands.Bot(command_prefix=commands.when_mentioned_or('prefix_here'), intents=intents)

it is using commands, but you can also use this with the discord.client method too, which can be done by..

import discord

intents = discord.Intents.all()
client = discord.Client(intents=intents)

now you try to run your script with these :)

You need to use Intents for your bot, modify your code to use this and it should solve the issue.

import discord

intents = discord.Intents.all()
client = discord.Client(intents=intents)

More information on intents.

Related