Discord channel members only returns one member?

Viewed 1010

I am running a bot on a test server. There are currently three members in the channel (member_count of 3), but it only returns one member. This member is the Bot.

The code:


import discord
from discord.ext import commands

TOKEN = "<Token>"
CHANNEL_ID = 1234

@client.event
async def on_ready():
    channel = client.get_channel(CHANNEL_ID)
    print(channel.members)

client.run(TOKEN)

Output:

[<Member id=<> name='Bot_name' discriminator='Bot_discriminator' bot=True nick=None guild=<Guild id=<> name="Server_name" shard_id=None chunked=False member_count=3>>]
3 Answers

Discord recently changed their bot api, which may be responsible for what you're seeing.

The good news is that the fix is reasonably easy, you just need to enable the "Server Members Intent" in your bot's admin page. Follow the instructions here.

Try to looping through the channel.members
For example:

for i in channel.members:
 print(i.id)

You can make it into a command which will get the count of members in the channel the command used in.

Docs:

@bot.command()
async def count(ctx):
    print(len(ctx.channel.members)) # to show how many are there
    for member in ctx.channel.members:
        print(member.name)
Related