【问题标题】:Bot says that it is in 0 servers, with 0 channels, with 0 usersBot 说它在 0 个服务器中,有 0 个频道,有 0 个用户
【发布时间】:2021-03-05 15:32:27
【问题描述】:

我做了一个&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
}

【问题讨论】:

标签: javascript discord.js


【解决方案1】:

这里的问题是 Guilds.cache 仅适用于 index.js 出于某种原因我尝试在我的命令处理程序上做同样的事情但它仍然对我说 0 所以如果你想让它工作把这个命令放在你的index.js 或 bot.js

【讨论】:

    【解决方案2】:

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

    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)
        }
    })
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-08-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多