首页 > 解决方案 > Bot 说它在 0 个服务器中,有 0 个频道,有 0 个用户

问题描述

我做了一个&bot-info命令,但每次我使用它时,它都说它在 0 个服务器上,有 0 个频道和 0 个用户,我该如何解决这个问题?一开始我有类似的代码行,当机器人打开时告诉我他所在的服务器、频道和用户的数量,它工作正常

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

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

标签: javascriptdiscord.js

解决方案


您可以尝试为您的代码调整这个简单的解决方案,这对我有用,或者提供更多详细信息,为什么要使用异步功能。

const Discord = require('discord.js')
const client = new Discord.Client()

client.on("message", async message => {
    if(message.content === "&bot-info") {
        const embed = new Discord.MessageEmbed() 
            .setTitle('Bot Stats')
            .setColor('#000000')
            .addFields(
                {
                    name: ' Servers',
                    value: `Serving ${client.guilds.cache.size} servers.`,
                    inline: true
                },
                {
                    name: ' Channels',
                    value: `Serving ${client.channels.cache.size} channels.`,
                    inline: true
                },
                {
                    name: ' Server Users',
                    value: `Serving ${client.users.cache.size}`,
                    inline: true
                }
            )
        message.channel.send(embed)
    }
})

推荐阅读