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('')
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
Check If a String Is HTML or Not
Get Docx File Contents Using Javascript/Jquery
Button Enabled and Disabled Based on Checkbox Is Checked or Not
Javascript Generate Random Numbers Without Repeating
Keep Order of Objects Inside a Json String After They Are Parsed
Use Localstorage Across Subdomains
Reduce the Build Time in a Large Angular 8
Jquery Ui Tabs - How to Get Currently Selected Tab Index
Check If a Number Has Repeated Digits
Combine Multiple JavaScript Files into One Js File
Get Request Works With Postman But Why Doesn't It Work With Reactjs Fetch
How to Get the Id of a Button That Was Clicked - Reactjs
Angular [Innerhtml]: How to Render Additional Elements Inside an Element With [Innerhtml] Directive
How to Get Width of (Dom) Element in Angular2
For Loop With Two Array in JavaScript