Skip to content

Commit

Permalink
Add discord alert
Browse files Browse the repository at this point in the history
  • Loading branch information
MathyouMB committed Sep 7, 2024
1 parent e275dff commit 6f2df74
Show file tree
Hide file tree
Showing 4 changed files with 140 additions and 4 deletions.
91 changes: 91 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"@astrojs/mdx": "^3.1.2",
"@astrojs/svelte": "^5.6.0",
"astro": "^4.11.1",
"axios": "^1.7.7",
"fs": "^0.0.1-security",
"katex": "^0.16.10",
"mathjax": "^3.2.2",
Expand Down
33 changes: 33 additions & 0 deletions src/utilities/discord.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import axios from "axios";

export enum DiscordMessageType {
SUCCESS = "SUCCESS",
ERROR = "ERROR",
INFO = "INFO",
}

interface DiscordMessage {
type: DiscordMessageType;
data: any;
}

const TYPE_TO_EMOJI = {
[DiscordMessageType.SUCCESS]: "✅",
[DiscordMessageType.ERROR]: "🚨",
[DiscordMessageType.INFO]: "ℹ️",
};

export const sendAlert = async (message: DiscordMessage) => {
const alertWebhook =
"https://discord.com/api/webhooks/1282054916797501520/nP9e25-C92uG5O58aLOdZHMNAJBU8kVb2GQJFRf4NUHqLmoEkLJJjNEn1NhbHHiiOwbV";
const emoji = TYPE_TO_EMOJI[message.type];
try {
const jsonFormatted = JSON.stringify(message, null, 2);
const discordFormatted = "```json\n" + jsonFormatted + "\n```";
await axios.post(alertWebhook, {
content: `${emoji} **Questions Repository Alert: ${message.type === DiscordMessageType.ERROR ? "<@147881865548791808>" : ""}** ${discordFormatted}`,
});
} catch (error) {
console.error("Failed to send Discord message", error);
}
};
19 changes: 15 additions & 4 deletions src/utilities/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import "mathjax/es5/tex-svg-full.js";
import { DiscordMessageType, sendAlert } from "./discord";

const mathjaxLoad = () => {
if (!(window as any).MathJax.config.tex.inlineMath) {
Expand All @@ -19,10 +20,20 @@ const mathjaxTypeset = () => {
};

const dynamicImport = async (path: string, meta: string) => {
const pathString = decodeURIComponent(new URL(path, meta).pathname);

const module = await import(pathString);
return module;
try {
const pathString = decodeURIComponent(new URL(path, meta).pathname);
const module = await import(pathString);
return module;
} catch (error) {
sendAlert({
type: DiscordMessageType.ERROR,
data: {
message: `Failed to dynamically import: ${path}`,
error,
},
});
throw error;
}
};

export { dynamicImport, mathjaxLoad, mathjaxTypeset };

0 comments on commit 6f2df74

Please sign in to comment.