0

I tried making a &bot-info command to display the amount of members / channels / servers the bot is in, but every time i use the command it displays "0" for each value

    const Discord = require('discord.js')
    const client = new Discord.Client();
    async function execute(message, args) {
        const servers = await client.guilds.cache.size
        const users = await client.users.cache.size
        const textchannels = await client.channels.cache.size
        const embed = new Discord.MessageEmbed()
            
            .setTitle('Bot Stats')
            .setColor('#000000')
            .addFields(
                {
                    name: ' Servers',
                    value: `Serving ${servers} servers.`,
                    inline: true
                },
                {
                    name: ' Channels',
                    value: `Serving ${textchannels} channels.`,
                    inline: true
                },
                {
                    name: ' Server Users',
                    value: `Serving ${users}`,
                    inline: true
                }
            )
            

        await message.channel.send(embed)
    }
module.exports = {
    name: 'bot-info',
    description: 'bot information',
    execute
}

Message that the bot displays

Message that the bot displays

Gaxyhs
  • 71
  • 6
  • https://stackoverflow.com/questions/61980179/why-does-client-guilds-cache-size-only-say-0-in-my-playing-status-even-if-its – Phix Dec 07 '20 at 18:48
  • @Phix that does not apply here, because this code is being run when the `message` event triggers and the bot would already be ready by the point that it is able to receive message events (and therefore, the `ready` event would already have triggered). – Cannicide Dec 09 '20 at 18:49

1 Answers1

0

The problem is that you are creating a new client. client is an object that essentially represents your bot. You've already created a client in your main js file (server.js or index.js), and in this file you are creating another client. The client you have created in this file is an entirely new bot, and it is a bot that is not logged in (hence why it show zeroes across the board).

Instead of creating a new Discord.Client object, you need to use the same one that was used in your main file. Luckily, the message object has a message.client property which is a reference to your main client object, the client that instantiated the message. Here's an example of how that would look in your code:

//Removed "const Discord" and "const client" from the top of this file

async function execute(message, args) {
    const client = message.client;

    const servers = await client.guilds.cache.size;
    const users = await client.users.cache.size;
    const textchannels = await client.channels.cache.size;
    const embed = new Discord.MessageEmbed()
        
        .setTitle('Bot Stats')
        .setColor('#000000')
        .addFields(
            {
                name: ' Servers',
                value: `Serving ${servers} servers.`,
                inline: true
            },
            {
                name: ' Channels',
                value: `Serving ${textchannels} channels.`,
                inline: true
            },
            {
                name: ' Server Users',
                value: `Serving ${users}`,
                inline: true
            }
        )
        

    await message.channel.send(embed)
}

Relevant resources:
https://discord.js.org/#/docs/main/stable/class/Message?scrollTo=client

Cannicide
  • 4,360
  • 3
  • 22
  • 42