簡體   English   中英

client.guilds.cache.size 壞了嗎?

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

它說:

它在我的機器人狀態上顯示“在 0 上播放”

問題是您試圖在client准備好之前獲取guilds集合。 您需要將獲取client.guilds.cache.size的代碼移動到ready事件處理程序中。

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

相關資源:
為什么client.guilds.cache.size在我的播放狀態中只顯示“0”,即使它在2個服務器中?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM