Discord Bot Not Getting All Users

Discord bot not getting all users

You have to use the method .fetchMembers() for every discord server the bot is in.

Try to use the following code:

function getUsers() {
let guilds = bot.guilds.array();

for (let i = 0; i < guilds.length; i++) {
bot.guilds.get(guilds[i].id).fetchMembers().then(r => {
r.members.array().forEach(r => {
let username = `${r.user.username}#${r.user.discriminator}`;
console.log(`${username}`);
});
});
}
}

Discord bot not getting all of the members

client.users is all of the User objects that have been cached at any point, mapped by their IDs.

I don't think there is a single method or property to get the total number of members of every server the bot is in. You could probably iterate over the guilds the client is currently handling and fetch the members instead.

The following should work:

async function getTotalNumberOfMembers(client) {
const guilds = client.guilds.cache;
let numberOfMembers = 0;

// resolves once members from every guild is fetched
await Promise.all(
guilds.map(async (guild) => {
const members = await guild.members.fetch();
numberOfMembers += members.size;
}),
);

return numberOfMembers;
}

// ...

const total = await getTotalNumberOfMembers(client);
console.log(total);

Edit: Yes, you can also use the .memberCount property:

function getTotalNumberOfMembers(client) {
const guilds = client.guilds.cache;
let numberOfMembers = 0;

guilds.each((guild) => {
numberOfMembers += guild.memberCount;
});

return numberOfMembers;
}

const total = getTotalNumberOfMembers(client);

Problem in getting the total users of my discord.js bot

You just can use a simple line in v12

${client.guilds.cache.reduce((a, g) => a + g.memberCount, 0)}

How to get all non bot users in discord js using a discord bot in nodejs

I think the problem is related to updating the bot policy for discord. Do you have this checkbox checked in the bot settings?
https://discord.com/developers/applications

Some info about client.users.cache:
Its a cache collection, so if you restart bot, or bot never handle user message or actions before, this collection will be empty. Better use guild.members.cache.get('')

Sample Image

Discord Bot can only see itself and no other users in guild

As of discord.py v1.5.0, you are required to use Intents for your bot, you can read more about them by clicking here
In other words, you need to do the following changes in your code -

import discord
from dotenv import load_dotenv

load_dotenv()
TOKEN = os.getenv('DISCORD_TOKEN')
GUILD = os.getenv('DISCORD_GUILD')

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

@client.event
async def on_ready():
for guild in client.guilds:
if guild.name == GUILD:
break

print(
f'{client.user} is connected to the following guild: \n'
f'{guild.name} (id: {guild.id})'
)

# just trying to debug here
for guild in client.guilds:
for member in guild.members:
print(member.name, ' ')

members = '\n - '.join([member.name for member in guild.members])
print(f'Guild Members:\n - {members}')

client.run(TOKEN)


Related Topics



Leave a reply



Submit