feat: track entitlements and users

This commit is contained in:
Naomi Carrigan 2024-08-13 08:38:42 -07:00
parent 23fba3d95d
commit ebb549953f
Signed by: naomi
SSH Key Fingerprint: SHA256:rca1iUI2OhAM6n4FIUaFcZcicmri0jgocqKiTTAfrt8
4 changed files with 28 additions and 2 deletions

View File

@ -16,6 +16,7 @@ export const onReady = async (bot: ExtendedClient) => {
await sendDebugMessage(bot, `Logged in as ${bot.user?.tag}`);
await registerCommands(bot);
bot.analytics = new Prometheus(bot);
await bot.analytics.updateEntitlements(bot);
// Daily at 9am PST
scheduleJob("birthdays", "0 9 * * *", async () => await postBirthdays(bot));

View File

@ -16,4 +16,5 @@ export const onGuildCreate = async function (
content: `JOINED GUILD: ${guild.name} (${guild.id}) - owned by ${owner?.displayName} (${owner.id})`
});
bot.analytics.updateGuilds(bot);
await bot.analytics.updateEntitlements(bot);
};

View File

@ -37,6 +37,7 @@ export const onGuildDelete = async function (
.deleteMany({ where: { serverId: guild.id } })
.catch(() => null);
bot.analytics.updateGuilds(bot);
await bot.analytics.updateEntitlements(bot);
} catch (err) {
await errorHandler(bot, "on guild delete", err);
}

View File

@ -1,21 +1,33 @@
import client, { Counter, Gauge } from "prom-client";
import type { ExtendedClient } from "../interfaces/ExtendedClient.js";
import type { ExtendedClient } from "../interfaces/ExtendedClient";
import { checkEntitledGuild } from "../utils/checkEntitledGuild";
export class Prometheus {
private client = client;
private guilds: Gauge;
private entitled: Gauge;
private commands: Counter;
private users: Gauge;
constructor(bot: ExtendedClient) {
this.guilds = new Gauge({
name: "guilds",
help: "The number of guilds the bot is in."
});
this.guilds.set(bot.guilds.cache.size);
this.entitled = new Gauge({
name: "guilds",
help: "The number of guilds the bot is in."
});
this.updateGuilds(bot);
this.commands = new Counter({
name: "commands",
help: "The number of commands that have been used since last boot."
});
this.users = new Gauge({
name: "users",
help: "The number of users the bot knows."
});
this.users.set(bot.users.cache.size);
this.client.collectDefaultMetrics();
}
@ -26,4 +38,15 @@ export class Prometheus {
public updateGuilds(bot: ExtendedClient) {
this.guilds.set(bot.guilds.cache.size);
}
public updateUsers(bot: ExtendedClient) {
this.users.set(bot.users.cache.size);
}
public async updateEntitlements(bot: ExtendedClient) {
const entitled = bot.guilds.cache.filter(
async (g) => await checkEntitledGuild(bot, g)
);
this.entitled.set(entitled.size);
}
}