From 8bb74d9cd82147cca6164590610f2e883f639cd0 Mon Sep 17 00:00:00 2001 From: Michael Date: Fri, 16 Dec 2022 15:17:43 -0500 Subject: [PATCH] Report server count anonymously with PM2 --- index.js | 16 ++++++++++++++++ utils/pm2-metrics.js | 5 +++++ 2 files changed, 21 insertions(+) diff --git a/index.js b/index.js index c033300..cb4606f 100644 --- a/index.js +++ b/index.js @@ -52,9 +52,13 @@ client.once(Events.ClientReady, c => { metrics.websocketHeartbeatHist.update(latency); }, 1000); + // Report current server count with PM2 (Servers counted anonymously) + metrics.serverCount.set(c.guilds.cache.size); + }); // Client "on" Events +// Someone used an interaction client.on(Events.InteractionCreate, async interaction => { if (!interaction.isChatInputCommand()) return; @@ -99,5 +103,17 @@ client.on(Events.InteractionCreate, async interaction => { }); +// Joined a server +client.on(Events.guildCreate, guild => { + // Report current server count with PM2 (Servers counted anonymously) + metrics.serverCount.set(guild.client.guilds.cache.size); +}); + +// Removed from a server +client.on(Events.guildDelete, guild => { + // Report current server count with PM2 (Servers counted anonymously) + metrics.serverCount.set(guild.client.guilds.cache.size); +}); + // Log in to Discord with your client's token client.login(token); \ No newline at end of file diff --git a/utils/pm2-metrics.js b/utils/pm2-metrics.js index 7c1cf8d..5e5bdaf 100644 --- a/utils/pm2-metrics.js +++ b/utils/pm2-metrics.js @@ -22,6 +22,10 @@ const websocketHeartbeatHist = io.histogram({ measurement: 'mean', }); +const serverCount = io.metric({ + name : 'Server Count', +}); + const interactionSuccess = function() { interactionSuccessCounter.inc(); interactionSuccessMeter.mark(); @@ -32,4 +36,5 @@ module.exports = { interactionErrors, interactionSuccess, websocketHeartbeatHist, + serverCount, }; \ No newline at end of file