繁体   English   中英

为什么当我在 Discord.js 中使用 ${client.guilds.cache.size} 时它显示 0?

[英]Why does it show 0 when i use ${client.guilds.cache.size} in Discord.js?

在制作 Discord Bot 时,在 info 命令中使用客户端行会缓存大小时显示为 0。 在聊天中它看起来像这样

const { DiscordAPIError, MessageEmbed } = require("discord.js");
const config = require("../config.json");
const Discord = require('discord.js');
const client = new Discord.Client();
const prefix = config.prefix;

module.exports = {
    name: 'info',
    description: "info",
    execute(message, args, Discord) {
        const newEmbed = new MessageEmbed()
        .setColor('RANDOM')
        .setFooter('Info for Laughing Horse 🤖')
        .addFields(
            {name: 'Bot tag', value: '`Laughing Horse#6722`'},
            {name: 'Version', value: '`1.0.4`'},
            {name: 'Prefix', value: prefix},
            {name: 'Bot Dev', value: '`Skep#2721`'},
            {name: 'server count', value: `the bot is currently in ${client.guilds.cache.size} servers`}
        )
        message.channel.send(newEmbed);
    }
}

正如评论中所说, client.guilds.cache.size返回 0,因为您在此文件夹中创建了一个新客户端,但此客户端从未登录到 discord,因此加载了 0 个公会

const client = require(your index file); 不要作为您不从 index.js 或类似的东西导出客户端工作,

因为这是一个命令,您似乎正在从消息事件传递消息

execute(message, args, Discord)

你可以做message.client.guilds.cache.size

希望这会有所帮助

ps:您在执行Discord中执行 Discord 并且还需要它在顶部, const Discord =...您应该删除其中之一

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM