diff --git a/prod.env b/prod.env index 40d9791..8287cbe 100644 --- a/prod.env +++ b/prod.env @@ -1,4 +1,5 @@ MATRIX_ACCESS_TOKEN="op://Environment Variables - Naomi/Alert Server/matrix_access_token" MATRIX_ROOM_ID="op://Environment Variables - Naomi/Alert Server/matrix_room_id" API_AUTH="op://Environment Variables - Naomi/Alert Server/api_auth" -EMAIL_PASSWORD="op://Environment Variables - Naomi/Alert Server/email_pass" \ No newline at end of file +EMAIL_PASSWORD="op://Environment Variables - Naomi/Alert Server/email_pass" +DISCORD_WEBHOOK_URL="op://Environment Variables - Naomi/Alert Server/discord_hook" \ No newline at end of file diff --git a/src/modules/discord.ts b/src/modules/discord.ts new file mode 100644 index 0000000..e303e21 --- /dev/null +++ b/src/modules/discord.ts @@ -0,0 +1,47 @@ +/** + * @copyright nhcarrigan + * @license Naomi's Public License + * @author Naomi Carrigan + */ + +/** + * Sends a Discord webhook notification. + * @param subject - The subject of the email. + * @param body - The text content of the email. + */ +export const sendDiscord = async( + subject: string, + body: string, +): Promise => { + await fetch(`${process.env.DISCORD_WEBHOOK_URL ?? ""}?with_components=true`, { + body: JSON.stringify({ + components: [ + { + accent_color: 15_418_782, + components: [ + { + content: `# ${subject}`, + type: 10, + }, + { + divider: true, + spacing: 1, + type: 14, + }, + { + content: body, + type: 10, + }, + ], + spoiler: false, + type: 17, + }, + ], + flags: 32_768, + }), + headers: { + "Content-Type": "application/json", + }, + method: "POST", + }); +}; diff --git a/src/server/serve.ts b/src/server/serve.ts index 7902343..1c733e2 100644 --- a/src/server/serve.ts +++ b/src/server/serve.ts @@ -6,6 +6,7 @@ import fastify from "fastify"; import { auth } from "../modules/auth.js"; +import { sendDiscord } from "../modules/discord.js"; import { sendMail } from "../modules/sendMail.js"; import { errorSchema } from "../schemas/errorSchema.js"; import { logSchema } from "../schemas/logSchema.js"; @@ -89,6 +90,10 @@ export const instantiateServer = (): void => { } const { application, context, stack, message } = request.body; await sendMail(`[ERROR]: ${context} - ${application}`, `${message}\n\n${stack}`); + await sendDiscord( + `[ERROR]: ${context} - ${application}`, + `${message}\n\n\`\`\`\n${stack}\n\`\`\``, + ); await response.status(200).send({ success: true }); }, );