generated from nhcarrigan/template
Compare commits
8 Commits
v0.0.1
...
a6ab06eac4
| Author | SHA1 | Date | |
|---|---|---|---|
| a6ab06eac4 | |||
| db4d125613 | |||
| 113afe9d11 | |||
| ebf60041ad | |||
| 351810856a | |||
| 6c08d431b7 | |||
| 7ee03a413d | |||
| f838a89663 |
+4
-3
@@ -1,11 +1,12 @@
|
|||||||
{
|
{
|
||||||
"name": "@nhcarrigan/discord-analytics",
|
"name": "@nhcarrigan/discord-analytics",
|
||||||
"version": "0.0.1",
|
"version": "0.0.5",
|
||||||
"description": "Package that pairs with our logging tool to provide analytics for our Discord bots.",
|
"description": "Package that pairs with our logging tool to provide analytics for our Discord bots.",
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"private": false,
|
"private": false,
|
||||||
"main": "prod/index.js",
|
"main": "prod/index.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
|
"prepublish": "pnpm run lint && pnpm run build",
|
||||||
"lint": "eslint src --max-warnings 0",
|
"lint": "eslint src --max-warnings 0",
|
||||||
"build": "rm -rf prod && tsc",
|
"build": "rm -rf prod && tsc",
|
||||||
"test": "echo \"Error: no test specified\" && exit 0"
|
"test": "echo \"Error: no test specified\" && exit 0"
|
||||||
@@ -23,8 +24,8 @@
|
|||||||
"typescript": "5.9.3"
|
"typescript": "5.9.3"
|
||||||
},
|
},
|
||||||
"peerDependencies": {
|
"peerDependencies": {
|
||||||
"discord.js": "^14.0.0",
|
"@nhcarrigan/logger": ">=1.1.0-hotfix",
|
||||||
"@nhcarrigan/logger": ">=1.1.0-hotfix"
|
"discord.js": "^14.0.0"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"node-schedule": "2.1.1"
|
"node-schedule": "2.1.1"
|
||||||
|
|||||||
+10
-69
@@ -4,89 +4,25 @@
|
|||||||
* @author Naomi Carrigan
|
* @author Naomi Carrigan
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import { Logger } from "@nhcarrigan/logger";
|
|
||||||
import { scheduleJob, type Job } from "node-schedule";
|
import { scheduleJob, type Job } from "node-schedule";
|
||||||
|
import type { Logger } from "@nhcarrigan/logger";
|
||||||
import type { Events, Client } from "discord.js";
|
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.
|
* A class for logging Discord bot analytics.
|
||||||
*/
|
*/
|
||||||
export class DiscordAnalytics {
|
export class DiscordAnalytics {
|
||||||
private readonly logger: Logger;
|
|
||||||
private job: Job | null = null;
|
private job: Job | null = null;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a new instance of the DiscordAnalytics class.
|
* Creates a new instance of the DiscordAnalytics class.
|
||||||
* @param client -- The Discord client to monitor.
|
* @param client -- The Discord client to monitor.
|
||||||
* @param name -- The name of the application.
|
* @param logger -- Instance of @nhcarrigan/logger to use for logging.
|
||||||
* @param logToken -- Auth token for our logging service.
|
|
||||||
*/
|
*/
|
||||||
public constructor(
|
public constructor(
|
||||||
private readonly client: Client,
|
private readonly client: Client,
|
||||||
name: string,
|
private readonly logger: Logger,
|
||||||
logToken: string,
|
) {}
|
||||||
) {
|
|
||||||
this.logger = new Logger(name, logToken);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Starts a CRON job to run at midnight (system time) daily.
|
* Starts a CRON job to run at midnight (system time) daily.
|
||||||
@@ -150,6 +86,11 @@ export class DiscordAnalytics {
|
|||||||
event: Events,
|
event: Events,
|
||||||
payload: Record<string, unknown>,
|
payload: Record<string, unknown>,
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
await this.logger.metric(event, 1, flatten(payload));
|
await this.logger.metric(
|
||||||
|
event,
|
||||||
|
1,
|
||||||
|
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions -- We want to cast this to a specific type.
|
||||||
|
payload as Record<string, string | number>,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user