Skip to content

Commit

Permalink
feat: add button to metrics msg n stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
Sv443 committed Jan 1, 2025
1 parent 4653a78 commit 6176f17
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 14 deletions.
4 changes: 4 additions & 0 deletions src/lib/misc.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { access, constants as fsconst } from "node:fs/promises";
import { exec } from "node:child_process";
import packageJson from "@root/package.json" with { type: "json" };

/** The base URL for the GitHub repository, without trailing slash */
export const ghBaseUrl = packageJson.repository.url.replace(/(git\+)|(\.git)/g, "");

/** Checks if a file exists */
export async function exists(path: string) {
Expand Down
43 changes: 29 additions & 14 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
import { EmbedBuilder, Events, type Client, type Message } from "discord.js";
import { readFile, writeFile } from "node:fs/promises";
import { ActionRowBuilder, ButtonBuilder, ButtonStyle, EmbedBuilder, Events, type Client, type Message, type MessageCreateOptions } from "discord.js";
import k from "kleur";
import "dotenv/config";
import { client, botToken } from "@lib/client.ts";
import { em, initDatabase } from "@lib/db.ts";
import { cmdInstances, evtInstances, initRegistry, registerCommandsForGuild } from "@lib/registry.ts";
import { autoPlural } from "@lib/text.ts";
import { envVarEquals, getEnvVar } from "@lib/env.ts";
import { initTranslations } from "@lib/translate.ts";
import { GuildConfig } from "@models/GuildConfig.model.ts";
import { readFile, writeFile } from "node:fs/promises";
import { getHash } from "@lib/crypto.ts";
import { getCommitHash } from "@lib/misc.ts";
import { getCommitHash, ghBaseUrl } from "@lib/misc.ts";
import { Col } from "@lib/embedify.ts";
import { GuildConfig } from "@models/GuildConfig.model.ts";
import packageJson from "@root/package.json" with { type: "json" };

//#region validate env

const requiredEnvVars = ["BOT_TOKEN", "APPLICATION_ID", "DB_URL", "BOT_INVITE_URL", "SUPPORT_SERVER_INVITE_URL"];

/** Called before the client is ready to check for environment variables and to initialize the client and database */
Expand All @@ -25,6 +28,8 @@ async function init() {
return;
}

//#region init bot

console.log(k.gray("\nInitializing and logging in..."));

await Promise.all([
Expand Down Expand Up @@ -57,7 +62,7 @@ async function init() {
}, 1000);
}

//#region metrics
//#region metrics:vars

const metGuildId = getEnvVar("METRICS_GUILD", "stringNoEmpty");
const metChanId = getEnvVar("METRICS_CHANNEL", "stringNoEmpty");
Expand All @@ -67,7 +72,7 @@ const metricsManifFile = ".metrics.json";
let metricsData: MetricsManifest | undefined;
let firstMetricRun = true;

//#region m:types
//#region metrics:types

type MetricsManifest = {
msgId: string | null;
Expand Down Expand Up @@ -150,7 +155,7 @@ async function updateMetrics(client: Client) {

const recreateMsg = async () => {
await metricsMsg?.delete();
metricsMsg = await metricsChan?.send(await useMetricsEmbed(latestMetrics));
metricsMsg = await metricsChan?.send(await useMetricsMsg(latestMetrics));
metricsData!.msgId = metricsMsg?.id;
await writeFile(metricsManifFile, JSON.stringify(metricsData));
};
Expand All @@ -159,14 +164,14 @@ async function updateMetrics(client: Client) {
if(!metricsMsg)
recreateMsg();
else
await metricsMsg?.edit(await useMetricsEmbed(latestMetrics));
await metricsMsg?.edit(await useMetricsMsg(latestMetrics));
}
catch {
recreateMsg();
}
}
else if(!metricsData.msgId || metricsData.msgId.length === 0) {
metricsMsg = await metricsChan?.send(await useMetricsEmbed(latestMetrics));
metricsMsg = await metricsChan?.send(await useMetricsMsg(latestMetrics));
metricsData.msgId = metricsMsg?.id;
await writeFile(metricsManifFile, JSON.stringify(metricsData));
}
Expand All @@ -181,13 +186,13 @@ async function updateMetrics(client: Client) {

//#region m:metrEmbed

/** Get the metrics / stats embed */
async function useMetricsEmbed(metrics: MetricsData) {
/** Get the metrics / stats embed and buttons */
async function useMetricsMsg(metrics: MetricsData) {
const ebd = new EmbedBuilder()
.setTitle("Bot metrics:")
.setFields([
{ name: "Joined guilds", value: String(metrics.guildsAmt), inline: true },
{ name: "Uptime", value: String(metrics.uptimeStr), inline: false },
{ name: "Guilds", value: String(metrics.guildsAmt), inline: true },
{ name: "Commands", value: String(metrics.commandsAmt), inline: true },
{ name: "Events", value: String(metrics.eventsAmt), inline: true },
])
Expand All @@ -196,7 +201,17 @@ async function useMetricsEmbed(metrics: MetricsData) {

return {
embeds: [ebd],
};
components: [
new ActionRowBuilder()
.addComponents(
new ButtonBuilder()
.setStyle(ButtonStyle.Link)
.setLabel("Browse current commit")
.setURL(`${ghBaseUrl}/tree/${await getCommitHash()}`)
)
.toJSON(),
],
} as Pick<MessageCreateOptions, "embeds" | "components">;
}

/** Returns the uptime in a human-readable format */
Expand All @@ -207,7 +222,7 @@ function getUptime() {
[(1000 * 60 * 60 * 24), `${Math.floor(upt / (1000 * 60 * 60 * 24))}d`],
[(1000 * 60 * 60), `${Math.floor(upt / (1000 * 60 * 60)) % 24}h`],
[(1000 * 60), `${Math.floor(upt / (1000 * 60)) % 60}m`],
[1000, `${Math.floor(upt / 1000) % 60}s`],
[0, `${Math.floor(upt / 1000) % 60}s`],
] as const)
.filter(([d]) => upt >= d)
.map(([, s]) => s)
Expand Down

0 comments on commit 6176f17

Please # to comment.