Discord.js members of all guilds in status - discord.js

Ive been wondering how to put the total amount of users with my bot in there server, IN the bots status,ive tried client.guilds.cache and client.guilds.cache.size
but both just return undefined in the status, any help would he appreciated

To get all the members you can use:
client.users.cache.size
If you want only users (without bots) use:
client.users.cache.filter(user => !user.bot).size

Related

Discord.js How to add roles when a user joins if the bot is in multiple servers

My bot is currently part of multiple server and Im trying to get it to add a role to a user when they join one of the servers, I have done stuff like
if(bot.guild.id === [SERVER ID]) and I've tried different forms of the bot.on command to no avail.
the current code is
bot.on('guildMemberAdd', guildMember => {id
let welcomerole = guildMember.guild.roles.find(role => role.name === 'Nomad');
guildMember.roles.add(welcomerole);
guildMember.guild.channels.get([SERVER_ID]).send("Welcome to the server")
})
But as per usual it does not work.
I'm not sure if there is a function im missing in the bot.on section of the block, or if the issue is something else. Note I am running Discord.js 11.6, so these functions do or should work with the version. (there is a reason).
Is there is a way to have it so the bot only adds the role to one of the servers that its a part of.
Just update to Discord.js V13, v11 is many years out of date and new docs solutions won't work with it.

How do you count all users in all guilds in discord.js

I want to count how many users are in all of the guilds my discord.js bot is in, but the only help I can find is v12 based and does not work in v13. My code and error message is below. How do I upgrade it to v13?
throw new DiscordAPIError(data, res.status, request);
^
DiscordAPIError: 404: Not Found
client.users.fetch()
const users = client.users.cache.filter(user => !user.bot);
console.log(users.size);
Client.users is a manager for all Discord users, which is why you can't just cache all. The way you see how many users the bot is in is by looping through the guilds and adding the member count. Use Collection#reduce() for this
const userCount = client.guilds.cache.reduce((a, g) => a+g.memberCount, 0)
However, there is no way to filter out all users by bots unless you are using GuildMembers
Ok the answer is:
interaction.client.guilds.cache.reduce((acc, guild) => acc + guild.memberCount, 0)

How can I get roles and channels of a guild

I am fairly new to making discord bots and I am making a !serverinfo command. The problem I have is that I cant get the channels and the roles, and the roles cannot be clickable(colored)
You can use <Guild>.roles.cache or <Guild>.channels.cache to return the respective manager. Replacing <Guild> with your guild object, most likely you will use <Message>.guild
Example
message.guild.channels.cache
// Will return Collection [Map] of all the guild's channels
message.guild.roles.cache
// Will return Collection [Map] of all the guild's roles

Retrieve all members from specific channel in Discord

I have been trying different ways to retrieve the current list of members in a specific channel (e.g. 'networking'). But I am only get one, the actual bot user inside the channel, but nothing else.
This is the latest version of the code I am trying.
client.channels.cache.filter((c) => c.name === 'networking').forEach(channel => {
channel.fetch().then((channel) => {
console.log(channel.name);
for (let [snowflake, guildMember] of channel.members) {
console.log(`${guildMember.displayName} (${guildMember.id})`);
}
});
});
It's probably something to do with caching but I'm just not able to find the right sequence. Any help would be appreciated.
Thank you Tyler2P for the answer. You were absolutely right. Membership intents were disabled by default. I have lost so much time on this :) Thanks a million.
For everyone who gets to this question. Solution is so simple as enabling the "Server Members Intent".
Bot configuration in Discord Application page

Discord.js User DM from id

Im trying to dm a user but I only have there id. This is the code im using:
const member = bot.users.cache.find(ID)
member.send('TEXT')
it isn't working. Does anyone know why?
If the user is not cached you can do this.
const member = bot.users.fetch('user-id',false,true)
That helps to fetch member data directly from API. more at here
Like somebody else has stated, the user probably isn't cached. There isn't anything you can do about this as far as I can tell.
If you want to avoid the undefined error your having, and to catch any errors that may occur when sending the message:
const member = bot.users.cache.find(ID);
if (member) member.send('TEXT').catch(console.error);

Resources