Y
Y
yura_20212021-06-20 12:57:51
Node.js
yura_2021, 2021-06-20 12:57:51

How to count server members with discord.js bot?

I wrote a code that counts the participants on the discord server, as well as the number of people in the voice channels. However, the bot returns the value of the number of participants along with the bots. How to change the code so that the number of participants is without bots, and also add separate statistics by status - online, not active, do not disturb. Here is the code
robot.on('message', async (msg) => {//Number of server members
if (msg.partial) {
try {
await msg.fetch();
} catch (error) {
console.error('Something went wrong when fetching the message: ', error);
return;
}
}
let voiceChannels = msg.guild.channels.cache.filter(c => c.type === 'voice');
let count = 0;
for (let [id, voiceChannel] of voiceChannels) count += voiceChannel.members.size;
robot.channels.fetch('813054508015288360').then(channel => {
channel.setName("Cats:"+ msg.guild.memberCount + ' ' + count)});
});

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexey, 2021-06-20
@Azperin

https://discord.js.org/#/docs/main/stable/class/Gu...

N
not important ., 2021-06-20
@26DiDi12

I hope you know where to enter)
and to distinguish bots from members simply where "members" subtract the number of bots, for example:
${members - 1}

async function timemembercount() {
  let members = bot.guilds.cache.get("GuildID").memberCount 
  bot.channels.cache.get("ChannelID").setName(`Котиков: ${members}`); 
}

var interval = setInterval(function () { timemembercount(); }, 20000  );

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question