Files
Checkmate/server/src/service/infrastructure/notificationProviders/discord.ts
T

89 lines
2.4 KiB
TypeScript
Raw Normal View History

2026-01-19 22:16:33 +00:00
const SERVICE_NAME = "DiscordProvider";
2026-01-19 18:58:17 +00:00
import type { Monitor, Notification, MonitorStatusResponse } from "@/types/index.js";
import { INotificationProvider } from "@/service/index.js";
2026-02-15 18:35:54 +00:00
import type { MonitorActionDecision } from "@/service/infrastructure/SuperSimpleQueue/SuperSimpleQueueHelper.js";
2026-01-19 21:58:48 +00:00
import { buildHardwareAlerts, buildDiscordBody, getTestMessage } from "@/service/infrastructure/notificationProviders/utils.js";
2026-01-19 20:12:54 +00:00
import got from "got";
2026-01-19 19:58:15 +00:00
2026-01-19 18:58:17 +00:00
export class DiscordProvider implements INotificationProvider {
2026-01-19 22:16:33 +00:00
private logger: any;
constructor(logger: any) {
this.logger = logger;
}
private getHardwareContent = (
clientHost: string,
monitor: Monitor,
monitorStatusResponse: MonitorStatusResponse,
decision: MonitorActionDecision
) => {
2026-02-15 18:35:54 +00:00
// For status changes (recovery), use standard format
if (decision.notificationReason === "status_change") {
return buildDiscordBody(monitor, monitorStatusResponse);
}
// For threshold breaches, use hardware alert format
const { discordPayload } = buildHardwareAlerts(clientHost, monitor, monitorStatusResponse);
2026-01-19 19:58:15 +00:00
return discordPayload;
};
sendAlert = async (
notification: Notification,
monitor: Monitor,
monitorStatusResponse: MonitorStatusResponse,
decision: MonitorActionDecision,
clientHost: string
) => {
2026-01-19 19:58:15 +00:00
let body;
if (monitor.type === "hardware") {
body = this.getHardwareContent(clientHost, monitor, monitorStatusResponse, decision);
2026-01-19 19:58:15 +00:00
} else {
body = buildDiscordBody(monitor, monitorStatusResponse);
}
if (!notification.address) {
return false;
}
try {
await got.post(notification.address, {
json: body,
headers: {
"Content-Type": "application/json",
},
});
return true;
2026-01-19 22:16:33 +00:00
} catch (error) {
const err = error as Error;
this.logger.warn({
message: "Discord alert failed",
service: SERVICE_NAME,
method: "sendAlert",
stack: err?.stack,
});
2026-01-19 19:58:15 +00:00
return false;
}
2026-01-19 18:58:17 +00:00
};
sendTestAlert = async (notification: Notification) => {
2026-01-19 21:58:48 +00:00
if (!notification.address) {
return false;
}
try {
await got.post(notification.address, {
json: { content: getTestMessage() },
headers: {
"Content-Type": "application/json",
},
});
return true;
} catch (error) {
2026-01-19 22:16:33 +00:00
const err = error as Error;
this.logger.warn({
message: "Discord test alert failed",
service: SERVICE_NAME,
method: "sendTestAlert",
stack: err?.stack,
});
2026-01-19 21:58:48 +00:00
return false;
}
2026-01-19 18:58:17 +00:00
};
}