简体   繁体   English

client.guilds.cache.size 坏了吗?

[英]Is client.guilds.cache.size broken?

I'm trying to make it so that my discord bot displays how many servers it's on as its status, but when i do client.guilds.cache.size it just says 0. This was happening with my friend aswell.我试图让我的 discord 机器人显示它在多少台服务器上作为其状态,但是当我执行 client.guilds.cache.size 时它只显示 0。这也发生在我的朋友身上。

Here's my code if it's of any use...这是我的代码,如果它有任何用处...

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!');

and it says:它说:

Its says 'Playing on 0' on my bot's status它在我的机器人状态上显示“在 0 上播放”

The problem is that you are trying to grab the guilds collection before the client is ready.问题是您试图在client准备好之前获取guilds集合。 You need to move the code that gets client.guilds.cache.size into your ready event handler.您需要将获取client.guilds.cache.size的代码移动到ready事件处理程序中。

client.once('ready', () => {
    console.log('Ready!');
    client.user.setActivity(`on ${client.guilds.cache.size}`);
});

Relevant resources:相关资源:
Why does client.guilds.cache.size only say "0" in my playing status even if it's in 2 servers? 为什么client.guilds.cache.size在我的播放状态中只显示“0”,即使它在2个服务器中?

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

相关问题 为什么当我在 Discord.js 中使用 ${client.guilds.cache.size} 时它显示 0? - Why does it show 0 when i use ${client.guilds.cache.size} in Discord.js? 为什么client.guilds.cache.size在我的播放状态中只显示“0”,即使它在2个服务器中? - Why does client.guilds.cache.size only say “0” in my playing status even if it's in 2 servers? client.guilds.cache.get(...)。 members.get 不是函数 - client.guilds.cache.get(...). members.get It is not a function Discord JS - 当我加入更多公会时,使用 client.guilds 的 .cache 属性会导致错误吗? - Discord JS - Will using the .cache property of client.guilds cause an error further down the line when I am in more guilds? Discord.js - 正确使用:for (const guild of client.guilds.cache)? - Discord.js - Correct use of: for (const guild of client.guilds.cache)? discord.js client.guilds.cache.get(role).members | 创建一个包含公会所有成员的数组 - discord.js client.guilds.cache.get(role).members | Create a array with all members of a guild client.guilds.find 不是 function - client.guilds.find is not a function Client.guilds.get() 未按预期工作 - Client.guilds.get() not working as intended message.client.guilds.fetch() 不是函数 - message.client.guilds.fetch() is not a function client.guilds 仅在日志中有效,在聊天中无效 - client.guilds only works in the log and not chat
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM