8 Commits

Author SHA1 Message Date
naomi 113afe9d11 release: v0.0.4
Node.js CI / Lint and Test (push) Successful in 35s
2025-10-07 18:06:50 -07:00
naomi ebf60041ad fix: use someone else's flatten package 2025-10-07 18:06:02 -07:00
naomi 351810856a release: v0.0.3 2025-10-07 17:43:18 -07:00
naomi 6c08d431b7 chore(tools): lint and build before publishing 2025-10-07 17:42:56 -07:00
naomi 7ee03a413d release: v0.0.2 2025-10-07 17:40:38 -07:00
naomi f838a89663 feat: user-provided logger 2025-10-07 17:40:19 -07:00
naomi 34e963250d release: v0.0.1
Node.js CI / Lint and Test (push) Successful in 34s
2025-10-07 17:37:32 -07:00
naomi 58b0ec20f2 fix: update tsconfig to include source maps and declarations 2025-10-07 17:37:17 -07:00
4 changed files with 24 additions and 72 deletions
+5 -3
View File
@@ -1,11 +1,12 @@
{
"name": "@nhcarrigan/discord-analytics",
"version": "0.0.0",
"version": "0.0.4",
"description": "Package that pairs with our logging tool to provide analytics for our Discord bots.",
"type": "module",
"private": false,
"main": "prod/index.js",
"scripts": {
"prepublish": "pnpm run lint && pnpm run build",
"lint": "eslint src --max-warnings 0",
"build": "rm -rf prod && tsc",
"test": "echo \"Error: no test specified\" && exit 0"
@@ -23,10 +24,11 @@
"typescript": "5.9.3"
},
"peerDependencies": {
"discord.js": "^14.0.0",
"@nhcarrigan/logger": ">=1.1.0-hotfix"
"@nhcarrigan/logger": ">=1.1.0-hotfix",
"discord.js": "^14.0.0"
},
"dependencies": {
"flat": "6.0.1",
"node-schedule": "2.1.1"
}
}
+10
View File
@@ -14,6 +14,9 @@ importers:
discord.js:
specifier: ^14.0.0
version: 14.22.1
flat:
specifier: 6.0.1
version: 6.0.1
node-schedule:
specifier: 2.1.1
version: 2.1.1
@@ -1127,6 +1130,11 @@ packages:
resolution: {integrity: sha512-f7ccFPK3SXFHpx15UIGyRJ/FJQctuKZ0zVuN3frBo4HnK3cay9VEW0R6yPYFHC0AgqhukPzKjq22t5DmAyqGyw==}
engines: {node: '>=16'}
flat@6.0.1:
resolution: {integrity: sha512-/3FfIa8mbrg3xE7+wAhWeV+bd7L2Mof+xtZb5dRDKZ+wDvYJK4WDYeIOuOhre5Yv5aQObZrlbRmk3RTSiuQBtw==}
engines: {node: '>=18'}
hasBin: true
flatted@3.3.3:
resolution: {integrity: sha512-GX+ysw4PBCz0PzosHDepZGANEuFCMLrnRTiEy9McGjmkCQYwRq4A/X786G/fjM/+OjsWSU1ZrY5qyARZmO/uwg==}
@@ -3368,6 +3376,8 @@ snapshots:
flatted: 3.3.3
keyv: 4.5.4
flat@6.0.1: {}
flatted@3.3.3: {}
for-each@0.3.5:
+5 -67
View File
@@ -4,88 +4,26 @@
* @author Naomi Carrigan
*/
import { Logger } from "@nhcarrigan/logger";
import { flatten } from "flat";
import { scheduleJob, type Job } from "node-schedule";
import type { Logger } from "@nhcarrigan/logger";
import type { Events, Client } from "discord.js";
// eslint-disable-next-line complexity, max-lines-per-function, max-statements -- Justified
const flatten = (
object: Record<string, unknown>,
): Record<string, string | number | boolean> => {
const result: Record<string, string | number | boolean> = {};
for (const key in object) {
const value = object[key];
if (value === null || value === undefined) {
continue;
}
if (
typeof value === "string"
|| typeof value === "number"
|| typeof value === "boolean"
) {
result[key] = value;
continue;
}
if (typeof value === "object" && !Array.isArray(value)) {
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions -- Justified
const nested = flatten(value as Record<string, unknown>);
for (const nestedKey in nested) {
const nestedValue = nested[nestedKey];
if (nestedValue === undefined) {
continue;
}
result[`${key}_${nestedKey}`] = nestedValue;
}
continue;
}
if (Array.isArray(value)) {
for (const [ index, arrayValue ] of value.entries()) {
if (
typeof arrayValue === "string"
|| typeof arrayValue === "number"
|| typeof arrayValue === "boolean"
) {
result[`${key}_${index.toString()}`] = arrayValue;
continue;
}
if (typeof arrayValue === "object" && arrayValue !== null) {
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions -- Justified
const nested = flatten(arrayValue as Record<string, unknown>);
for (const nestedKey in nested) {
const nestedValue = nested[nestedKey];
// eslint-disable-next-line max-depth -- Justified
if (nestedValue === undefined) {
continue;
}
result[`${key}_${index.toString()}_${nestedKey}`] = nestedValue;
}
}
}
continue;
}
}
return result;
};
/**
* A class for logging Discord bot analytics.
*/
export class DiscordAnalytics {
private readonly logger: Logger;
private job: Job | null = null;
/**
* Creates a new instance of the DiscordAnalytics class.
* @param client -- The Discord client to monitor.
* @param name -- The name of the application.
* @param logToken -- Auth token for our logging service.
* @param logger -- Instance of @nhcarrigan/logger to use for logging.
*/
public constructor(
private readonly client: Client,
name: string,
logToken: string,
private readonly logger: Logger,
) {
this.logger = new Logger(name, logToken);
}
/**
@@ -150,6 +88,6 @@ export class DiscordAnalytics {
event: Events,
payload: Record<string, unknown>,
): Promise<void> {
await this.logger.metric(event, 1, flatten(payload));
await this.logger.metric(event, 1, flatten(payload, { delimiter: "_" }));
}
}
+3 -1
View File
@@ -2,6 +2,8 @@
"extends": "@nhcarrigan/typescript-config",
"compilerOptions": {
"outDir": "./prod",
"rootDir": "./src"
"rootDir": "./src",
"sourceMap": true,
"declaration": true
},
}