![](/img/trans.png)
[英]Why does it show 0 when i use ${client.guilds.cache.size} in Discord.js?
[英]Is client.guilds.cache.size broken?
我试图让我的 discord 机器人显示它在多少台服务器上作为其状态,但是当我执行 client.guilds.cache.size 时它只显示 0。这也发生在我的朋友身上。
这是我的代码,如果它有任何用处...
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!');
它说:
问题是您试图在client
准备好之前获取guilds
集合。 您需要将获取client.guilds.cache.size
的代码移动到ready
事件处理程序中。
client.once('ready', () => {
console.log('Ready!');
client.user.setActivity(`on ${client.guilds.cache.size}`);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.