oleg2010
oleg2010

Reputation: 19

discord.py guilds.members not working correctly

It works, but for some reason not on all servers, but on certain ones. I understand that it can't get the server members because it is trying to create a DM for itself.

@bot.event
async def on_ready():
    for guild in bot.guilds:
        print(guild.members)
        for member in guild.members:
            guild.fetch_members()
            print(member)
            try:
                await member.create_dm()
                await member.send(text)
            except:
                print('direct messages are closed')
                await asyncio.sleep(25)
                continue

I tried Googling, I just found that I need intents, I tried nothing has changed. Here they are if anything:

intents = discord.Intents.default()
intents = discord.Intents(messages=True, guilds=True, members=True, typing = True)
bot = commands.Bot(command_prefix='!', intents=intents)

Upvotes: 1

Views: 390

Answers (1)

VintiumDust
VintiumDust

Reputation: 105

I've just spent some time looking through the discord.py docs and I think I may have found the issue. When you run guild.fetch_members() inside of that for, it seems like you're attempting to enable access to members in guild.members. Though this may work sometimes, the documentation recommends a different usage which is likely more reliable and which is possibly newer.

From the fetch_members() docs (a bit paraphrased):

Example Usage:

async for member in guild.fetch_members(limit=150):
    print(member.name)
    

# if a discord.ClientException is raised, the members intent is not enabled.

Let me know if this helps/works 👍

Upvotes: 2

Related Questions