-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy patherror.js
More file actions
66 lines (60 loc) · 2.12 KB
/
error.js
File metadata and controls
66 lines (60 loc) · 2.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import fetch from 'node-fetch';
/**
* Escape Discord markdown formatting characters so messages render as plain text.
* Escapes: \ * _ ~ | ` > # - [ ] ( )
* @param {string} text - Text to escape
* @returns {string} Escaped text
*/
function escapeDiscordMarkdown(text) {
if (!text) return text;
return text.replace(/[\\*_~|`>#\-\[\]()]/g, '\\$&');
}
/**
* Send a notification to Discord webhook
* @param {string} webhookURL - Discord webhook URL
* @param {string} message - Message to send
* @param {string} level - Error level for logging (e.g., 'error', 'low', 'info')
*/
async function sendWebhookNotification(webhookURL, message, level = 'error') {
if (!webhookURL) {
console.warn(`No webhook URL configured for ${level} level notifications`);
return;
}
try {
await fetch(webhookURL, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ content: escapeDiscordMarkdown(message) }),
});
console.log(`Sent ${level} level notification to Discord`);
} catch (err) {
console.error(`Failed to send ${level} notification to Discord:`, err.message);
}
}
/**
* Send high-priority error notification to Discord
* @param {string} errorMessage - Error message to send
*/
export async function sendError(errorMessage) {
const webhookURL = process.env.WEBHOOK_URL;
await sendWebhookNotification(webhookURL, errorMessage, 'error');
}
/**
* Send low-priority error notification to Discord
* @param {string} lowErrorMessage - Low-priority error message to send
*/
export async function sendLowError(lowErrorMessage) {
const webhookURL = process.env.WEBHOOK_URL_LOW;
await sendWebhookNotification(webhookURL, lowErrorMessage, 'low');
}
/**
* Send informational notification to Discord (startup, reconnect success, etc.)
* Uses the low-priority webhook to avoid competing with real error alerts
* @param {string} infoMessage - Info message to send
*/
export async function sendInfo(infoMessage) {
const webhookURL = process.env.WEBHOOK_URL_LOW || process.env.WEBHOOK_URL;
await sendWebhookNotification(webhookURL, infoMessage, 'info');
}