Andryxa
Andryxa

Reputation: 113

How to make a filter from bots? Discord.js

I can't make a filter.

The bot shows 6 members of the server , with the bot.

How can I make the bot not be included in this number

const guild = client.guilds.cache.get("74721732.........");
setInterval(function () {
   var memberCount = guild.members.filter(member => !member.user.bot).size;
   var memberCountChannel = client.channels.cache.get("81358139.........");
  memberCountChannel.setName(`Members: ${memberCount}`);
}, 1000);

Upvotes: 0

Views: 1791

Answers (1)

Zsolt Meszaros
Zsolt Meszaros

Reputation: 23161

Your filter's callback function is correct, it removes every bot from the collection.

The problem is that guild.members returns a manager, not a collection. It means it won't have a filter() method. You should use guild.members.cache as it returns a collection of guild members and collections do have a .filter() method:

const guild = client.guilds.cache.get('74721732.........');
setInterval(function () {
  const memberCount = guild.members.cache.filter((member) => !member.user.bot).size;
  const memberCountChannel = client.channels.cache.get('81358139.........');
  memberCountChannel.setName(`Members: ${memberCount}`);
}, 1000);

Upvotes: 1

Related Questions