const request = require('request');

/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

/**
 * Gets the current guild and UNIQUE member counts.
 *
 * @param {CommandoClient} client - The Discord.JS-Commando Client object
 * @returns {object} - An object containing two properties, guilds and members, each representing the associated counts.
 */
exports.getGuildsAndMembers = async (client) => {
	return new Promise((resolve, reject) => {
		// Algorithm provided by CyberPon3
		// https://github.com/CyberPon3
		let members = client.guilds.reduce(
			(acc, guild) => {
				guild.members
					.filter(member => !member.user.bot)
					.forEach(member => acc.add(member.id));

				return acc;
			},
			new Set()
		).size;

		let guilds = client.guilds.array().length;

		resolve({ members: members, guilds: guilds });
	});
};

exports.setGame = async (client) => {
	let { members, guilds } = await this.getGuildsAndMembers(client);

	await Promise.all(client.config.statusApi.sites.map(async (site) => {
		return new Promise((resolve, reject) => {
			request.post(
				{
					uri: `https://${site.url}/api/bots/${client.user.id}/stats`,
					headers: {
						Authorization: site.token
					},
					json: {
						server_count: guilds
					}
				},
				(err, res, body) => {
					if (err) return reject(err);

					const status = res.statusCode;
					if (![200, 204, 301].includes(status)) return reject(new Error(`Received unexpected status code: ${status} at URL ${site.url}`));

					return resolve();
				}
			)
		});
	}));

	return client.user.setPresence({
		game: {
			name:`for ${client.commandPrefix || client.options.commandPrefix}help | ${members.toLocaleString()} members across ${guilds.toLocaleString()} guilds`,
			type: 'WATCHING'
		}
	});
};

@@ -1078,16 +1078,49 @@
 ient);%0A%0A
+%09if (client.config.statusApi) %7B%0A%09
 %09await P
@@ -1184,16 +1184,17 @@
  =%3E %7B%0A%09%09
+%09
 return n
@@ -1230,16 +1230,17 @@
 =%3E %7B%0A%09%09%09
+%09
 request.
@@ -1245,16 +1245,17 @@
 t.post(%0A
+%09
 %09%09%09%09%7B%0A%09%09
@@ -1257,16 +1257,17 @@
 %09%7B%0A%09%09%09%09%09
+%09
 uri: %60ht
@@ -1324,16 +1324,17 @@
 %60,%0A%09%09%09%09%09
+%09
 headers:
@@ -1332,24 +1332,25 @@
 %09headers: %7B%0A
+%09
 %09%09%09%09%09%09Author
@@ -1369,16 +1369,17 @@
 e.token%0A
+%09
 %09%09%09%09%09%7D,%0A
@@ -1387,16 +1387,17 @@
 %09%09%09%09
+%09
 json: %7B%0A
 %09%09%09%09
@@ -1388,24 +1388,25 @@
 %09%09%09%09json: %7B%0A
+%09
 %09%09%09%09%09%09server
@@ -1425,18 +1425,20 @@
 ds%0A%09%09%09%09%09
+%09
 %7D%0A
+%09
 %09%09%09%09%7D,%0A%09
@@ -1440,16 +1440,17 @@
 %09%7D,%0A%09%09%09%09
+%09
 (err, re
@@ -1454,14 +1454,8 @@
  res
-, body
 ) =%3E
@@ -1458,24 +1458,25 @@
 ) =%3E %7B%0A%09%09%09%09%09
+%09
 if (err) ret
@@ -1489,24 +1489,25 @@
 ject(err);%0A%0A
+%09
 %09%09%09%09%09const s
@@ -1530,16 +1530,17 @@
 usCode;%0A
+%09
 %09%09%09%09%09if 
@@ -1668,24 +1668,25 @@
 %7D%60));%0A%0A%09%09%09%09%09
+%09
 return resol
@@ -1699,26 +1699,34 @@
 %09%09%09%09
+%09
 %7D%0A
+%09
 %09%09%09)
-%0A
+;%0A%09
 %09%09%7D);%0A
+%09
 %09%7D));
+%0A%09%7D
 %0A%0A%09r

Fix error on selfhosted instances