Home > Enterprise >  Everytime I run the discord bot, it says that the number of online members is undefined
Everytime I run the discord bot, it says that the number of online members is undefined

Time:12-24

This is the code

 module.exports = async (client) => {
        let myGuild = client.guilds.cache.get(guild.id)

            setInterval(() =>{

            var onlineCount = myGuild.members.cache.filter(member => member.presence?.status != "offline").size
    
            let memberCountChannel = myGuild.channels.cache.get(channel.id);
            console.log(onlineCount.name);
            memberCountChannel.setName(onlineCount   'people online')
            .then(result => console.log(result))
            .catch (error => console.log(error));
        }, 5000)
    }

And this is the part that counts the members

var onlineCount = myGuild.members.cache.filter(member => member.presence?.status != "offline").size

What the terminal shows

undefined
undefined
undefined
undefined
undefined

CodePudding user response:

Since you are restarting the bot the data is not cached, you firstly need to fetch the members with <Guild>.members.fetch method.

Else it means you didn't activate intents, refer here.

CodePudding user response:

filter method returns an array, and arrays do not have size property. Use length property and it will be ok.

Please check this answer also: https://stackoverflow.com/a/14202745/10500500

var onlineCount = myGuild.members.cache.filter(member => member.presence?.status != "offline").length
  • Related