Skip to content

Instantly share code, notes, and snippets.

@eslachance
Last active January 18, 2021 00:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eslachance/500fdab100e3625ca428a588e47138d8 to your computer and use it in GitHub Desktop.
Save eslachance/500fdab100e3625ca428a588e47138d8 to your computer and use it in GitHub Desktop.
Getting Total/Online/Bots in a guild without privileged intents

Getting Online and Bot user counts without any privileged intents

Requirements

  • A bot with MANAGE_WEBHOOKS permissions on the guild.

Concepts

This code fetches the guild manually in order to get all of its values, including the approximateMemberCount (which is the semi-up-to-date value indicating how many members are on your guild) and the approximatePresenceCount which is the semi-up-to-date value indicating how many ONLINE members are on your guild. Now, they aren't always precise to the member, but they're close enough and are updated regularly. They're usually just a bit behind (a day or two, maybe?) in terms of data.

Once the guild is fetched, you now have the "Total" and "Online" members, but what about bots? For this we use the Integrations endpoint. Integrations counts webhooks and such, so it needs the MANAGE_WEBHOOKS permission - make sure yourt bot has it!

Limitation

Guild Integrations are limited to 50, so you will never see a bot count higher than 50 using this method. Still, guilds with more than 50 bots are fairly rare, and this is better than nothing ^_^

Using it

As the code shows, you can easily create a single function that takes in a guild and returns the appropriate values. This function must be promise-based as it need to wait for guild.fetch() and fetchIntegrations() to work. Make sure to await the response!

Please contribute other library code!

This gist currently only has a method to use with discord.js 12.4.x. If you know how to do this with other libraries, in any language, please don't hesitate to let me know either by commenting below, and I'll add it to the list!

/*
Requirements:
- Node.js 12.x or higher (but you should be using 14 now)
- Discord.js 12.4.0 or higher (very important, wont' work on 12.3 and lower)
Credits go to [WilsontheWolf](https://github.com/WilsontheWolf) for this code
*/
const Discord = require("discord.js");
const client = new Discord.Client();
const getMembersCounts = async (guild) => {
await guild.fetch();
const total = guild.approximateMemberCount;
const online = guild.approximatePresenceCount;
let bots = false;
if (guild.me.hasPermission('MANAGE_WEBHOOKS')) {
const integrations = await guild.fetchIntegrations({ includeApplications: true });
bots = integrations.filter(i => i.application && i.application.bot).size;
};
return {
total,
online,
bots: bots || 'N/A',
users: bots ? total - bots : 'N/A',
}
}
client.on("message", async message => {
if(message.content === '.guildstats') {
const { total, online, bots, users } = await getMembersCounts(message.guild);
message.channel.send(`Members: ${total}, Users: ${users}, Bots: ${bots}, Online: ${online}`);
}
});
client.login("SuperSecretBotTokenHere");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment