Getting number of members and servers a bot is serving

若如初见. 提交于 2020-11-29 19:21:36

问题


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?


回答1:


Try client.guilds.cache.size and client.users.cache.size. This changed in discord.js v12.

client.users has been changed from a Collection to a Manager.

client.guilds has been changed from a Collection to a Manager.




回答2:


You can use client.guilds.cache.size which will return the number of guilds your bot is in, Let's assume you will be making a const for it and then put it as your bot's activity.

const servers = client.guilds.cache.size

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

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

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',
    })
})

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

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!



来源:https://stackoverflow.com/questions/62231788/getting-number-of-members-and-servers-a-bot-is-serving

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!