2

I'm trying to make it so that my discord bot displays how many servers it's on as its status, but when i do client.guilds.cache.size it just says 0. This was happening with my friend aswell.

Here's my code if it's of any use...

const fs = require('fs');
const Discord = require('discord.js');
const { prefix, token } = require('./myconfig10.json');
const client = new Discord.Client();
const cheerio = require('cheerio');
const request = require('request');
client.commands = new Discord.Collection();
const commandFiles = fs.readdirSync('./commands').filter(file => file.endsWith('.js'));
const embedAuthor = ('This bot was made by <my discord name>')
const servers = client.guilds.cache.size


//Telling the bot where to look for the commands
for (const file of commandFiles) {
    const command = require(`./commands/${file}`);
    client.commands.set(command.name, command);
}

//Once the bot is up and running, display 'Ready' in the console
client.once('ready', () => {
    console.log('Ready!');
    client.user.setActivity(`on ${servers}`);
});

//Seeing if the message starts with the prefix
client.on('message', message => {
    if (!message.content.startsWith(prefix) || message.author.bot) return;

//Telling the bot what arguments are
    const args = message.content.slice(prefix.length).trim().split(/ +/)
    const commandName = args.shift().toLowerCase();

//Checking to see if the command you sent is one of the commands in the commands folder 
    if (!client.commands.has(commandName)) return;
    console.log(`Collected 1 Item, ${message}`)

    const command = client.commands.get(commandName);

//Try to execute the command.
    try {
        command.execute(message, args);
    
//If there's an error, don't crash the bot. 
    } catch (error) {
        console.error(error);
        
//Sends a message on discord telling you there was an error
        message.reply('there was an error trying to execute that command!');

and it says:

Its says 'Playing on 0' on my bot's status

Tomori Lol
  • 27
  • 5
  • Have you turned on intents from the [discord developer portal](https://discord.com/developers/applications)? – Tyler2P Dec 09 '20 at 17:37
  • @Tyler2P I'm not sure what that is? – Tomori Lol Dec 09 '20 at 17:39
  • Maybe [this answer](https://stackoverflow.com/questions/64559390/none-of-my-discord-js-guildmember-events-are-emitting-my-user-caches-are-basica/64559391#64559391) could help. – Tyler2P Dec 09 '20 at 17:42
  • @Tyler2P I've checked both of them but it still says 0, will it take a bit to take effect? – Tomori Lol Dec 09 '20 at 17:45
  • The issue is not intents. The problem is that you are trying to grab the `guilds` collection before the client is ready. You need to move the code that gets `client.guilds.cache.size` into your `ready` event handler. See the answer to this question [here](https://stackoverflow.com/questions/61980179/why-does-client-guilds-cache-size-only-say-0-in-my-playing-status-even-if-its) – Cannicide Dec 09 '20 at 17:48

1 Answers1

2

The problem is that you are trying to grab the guilds collection before the client is ready. You need to move the code that gets client.guilds.cache.size into your ready event handler.

client.once('ready', () => {
    console.log('Ready!');
    client.user.setActivity(`on ${client.guilds.cache.size}`);
});

Relevant resources:
Why does client.guilds.cache.size only say "0" in my playing status even if it's in 2 servers?

Cannicide
  • 4,360
  • 3
  • 22
  • 42