feat: add analytics
Node.js CI / Lint and Test (push) Successful in 37s

This commit is contained in:
2025-10-08 15:23:27 -07:00
parent 12fac9fbd1
commit e779153d54
5 changed files with 66 additions and 42 deletions
+5 -2
View File
@@ -21,7 +21,7 @@ import type { MessageParam } from "@anthropic-ai/sdk/resources/index.js";
* Handles the Discord message event.
* @param message - The message payload from Discord.
*/
// eslint-disable-next-line max-lines-per-function, max-statements -- We're off by one bloody line.
// eslint-disable-next-line max-lines-per-function, max-statements, complexity -- We're off by one bloody line.
export const onMessage = async(message: Message): Promise<void> => {
try {
if (message.channel.type !== ChannelType.DM) {
@@ -79,8 +79,11 @@ export const onMessage = async(message: Message): Promise<void> => {
}
await calculateCost(messages.usage, message.author.username);
await logger.metric("messages_processed", 1, { user: message.author.id });
} catch (error) {
await logger.error("message event", error as Error);
await logger.error("message event", error instanceof Error
? error
: new Error(String(error)));
const button = new ButtonBuilder().
setLabel("Need help?").
setStyle(ButtonStyle.Link).
+5
View File
@@ -3,6 +3,7 @@
* @license Naomi's Public License
* @author Naomi Carrigan
*/
import { DiscordAnalytics } from "@nhcarrigan/discord-analytics";
import { Client, Events, GatewayIntentBits, Partials } from "discord.js";
import { onMessage } from "./events/message.js";
import { about } from "./modules/about.js";
@@ -33,7 +34,10 @@ const client = new Client({
partials: [ Partials.Channel ],
});
const analytics = new DiscordAnalytics(client, logger);
client.on(Events.InteractionCreate, (interaction) => {
void analytics.logGatewayEvent(Events.InteractionCreate, { ...interaction });
if (interaction.isChatInputCommand()) {
switch (interaction.commandName) {
case "about":
@@ -69,6 +73,7 @@ client.on(Events.EntitlementDelete, (entitlement) => {
client.on(Events.ClientReady, () => {
void logger.log("debug", "Bot is ready.");
analytics.startCron();
});
instantiateServer();