Discord channel members only returns one member?
Question:
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>>]
Answers:
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)
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.
I ran into this issue right now,
I’m not using discord.js btw I’m making HTTP Request by hand.
For those using http request like me, the GUILD_MEMBERS intent can be your issue but you’ll probably run into this one too.
It seems that the list members API endpoint has a default limitation of 1 on the number of result. In my case (HTTP requests) I needed to add a limit=1000
query parameter to my request’s URL
so https://discord.com/api/guilds/<guild-id>/members
becomes https://discord.com/api/guilds/<guild-id>/members?limit=1000
https://discord.com/developers/docs/resources/guild#list-guild-members
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>>]
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)
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.
I ran into this issue right now,
I’m not using discord.js btw I’m making HTTP Request by hand.
For those using http request like me, the GUILD_MEMBERS intent can be your issue but you’ll probably run into this one too.
It seems that the list members API endpoint has a default limitation of 1 on the number of result. In my case (HTTP requests) I needed to add a limit=1000
query parameter to my request’s URL
so https://discord.com/api/guilds/<guild-id>/members
becomes https://discord.com/api/guilds/<guild-id>/members?limit=1000
https://discord.com/developers/docs/resources/guild#list-guild-members