Ad

I Want To Get Members Count In Discord Bot But It Gives Error

- 1 answer

I want to get members count in discord bot but it gives error. I search for it through internet for this and i don't find it! Here is code:

const Commando = require('discord.js-commando');
const bot = new Commando.Client({commandPrefix: '$'});
const TOKEN = 'here is token';
const MIN_INTERVAL = 3 * 1000;
const guild = bot.guilds.get("394805546450026496");

bot.registry.registerGroup('connectc', 'Connectc');
bot.registry.registerGroup('defaultc', 'Defaultc');
bot.registry.registerDefaults();
bot.registry.registerCommandsIn(__dirname + "/commands")

bot.on('ready', function(){
    console.log("Ready");
    setInterval(function(){
        var memberCount = guild.members.filter(member => !member.user.bot).size;
        var memberCountChannel = bot.channels.get("547805078787194891");
        memberCountChannel.setName("👤Osoby: "+ memberCount +" 👤");
    }, MIN_INTERVAL);
});
bot.login(TOKEN);

And here error:

C:\Users\User\Documents\Visual Studio Code\Discord Bots\VblacqeBot\index.js:18
    var memberCount = guild.members.filter(member => !member.user.bot).size;
                            ^

TypeError: Cannot read property 'members' of undefined
    at CommandoClient.<anonymous> (C:\Users\User\Documents\Visual Studio Code\Discord Bots\VblacqeBot\index.js:18:29)    
    at CommandoClient.emit (events.js:194:15)
    at WebSocketConnection.triggerReady (C:\Users\User\Documents\Visual Studio Code\Discord Bots\VblacqeBot\node_modules\discord.js\src\client\websocket\WebSocketConnection.js:125:17)
    at WebSocketConnection.checkIfReady (C:\Users\User\Documents\Visual Studio Code\Discord Bots\VblacqeBot\node_modules\discord.js\src\client\websocket\WebSocketConnection.js:141:61)
    at GuildCreateHandler.handle (C:\Users\User\Documents\Visual Studio Code\Discord Bots\VblacqeBot\node_modules\discord.js\src\client\websocket\packets\handlers\GuildCreate.js:13:31)
    at WebSocket.onMessage (C:\Users\User\Documents\Visual Studio Code\Discord Bots\VblacqeBot\node_modules\ws\lib\event-target.js:120:16)
    at WebSocket.emit (events.js:189:13)

Please help me!

Ad

Answer

From your error results that the guild is undefined. I ran the code and it's working as expected.

module.exports.run = async (client, message, arguments) => {
   const guild = client.guilds.get("566596189827629066");
   setInterval(function () {
      var memberCount = guild.members.filter(member => !member.user.bot).size;  
      var memberCountChannel = client.channels.get("626462657817477131");
      memberCountChannel.setName(`${guild.name} has ${memberCount} members!`);
   }, 1000);
};

Image

Please, double-check that 394805546450026496 is a valid guild-id and not a channel-id/user-id. If it is, check if the bot is in the mentioned guild.

Another thing, it's recommended to see if a guild is available before performing operations or reading data from it. You can check this with guild.available.

Ad
source: stackoverflow.com
Ad