获取机器人正在服务的成员和服务器数量 [英] Getting number of members and servers a bot is serving

查看:83
本文介绍了获取机器人正在服务的成员和服务器数量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我浏览了discord.js指南,发现 client.guilds.size client.users.size 用于查找机器人所在的用户和服务器。但是当我实现它时,我会得到未定义。

So I went through the discord.js guide, and found that client.guilds.size and client.users.size is for finding no of users and servers a bot is on. But when I implement it I get "undefined". Any reason why?

推荐答案

您可以使用 client.guilds.cache.size 这将返回您的机器人所在的行会数量让我们假设您将为 const

const servers = client.guilds.cache.size

console.log(`Bot is now online and serving in ${servers} servers`)

但是您需要输入 await 在您的 const 中,并将您的函数用作异步函数,以便Bot可以返回并

But you need to put await in your const and make your function as an async function so the Bot will able to return and display it.

const servers = await client.guilds.cache.size

不执行此操作,您的机器人会将其显示为 0台服务器

Without doing this your bot will display it as 0 servers

在这里,我将举一个例子

Here i will give an example.

client.on('ready', async () => {
    //This will get the amount of servers and then return it.
    const servers = await client.guilds.cache.size
    const users = await client.users.cache.size
    
    console.log(`Bot is now online and serving in ${servers} servers`)
    //This will display "Playing in <servers> servers!"
    client.user.setActivity(`in ${servers} servers and serving ${users}`, {
        type: 'PLAYING',
    })
})

这将显示正在11台服务器上播放并为500位用户提供服务 (例如。)在机器人的状态中。

This will display "Playing in 11 Servers and serving 500 users" (for example.) in the Bot's Status.

适用于新手。
常量服务器=等待client.guilds.cache.size 可以用于您要使功能生效的所有事情异步功能
让我知道它是否对我有用!

For those who are new. const servers = await client.guilds.cache.size works for all things you want to make as long you made your function as an async function Let me know if this does work, it works for me!

这篇关于获取机器人正在服务的成员和服务器数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆