-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
49 lines (41 loc) · 1.13 KB
/
index.js
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
const axios = require('axios');
class DiscordNotification {
constructor(webhookUrl) {
if (!webhookUrl) {
throw new Error('Webhook URL is required');
}
this.webhookUrl = webhookUrl;
}
async send(content, options = {}, embed = null) {
if (!content && !embed) {
throw new Error('Content or embed is required');
}
const payload = {
content,
...options,
};
if (embed) {
payload.embeds = [embed];
}
try {
const response = await axios.post(this.webhookUrl, payload);
return response.data;
} catch (error) {
throw new Error('Failed to send notification: ' + error.message);
}
}
async sendFromApi(apiUrl, options = {}, embedOptions = {}) {
if (!apiUrl) {
throw new Error('API URL is required');
}
try {
const response = await axios.get(apiUrl);
const data = response.data;
const content = JSON.stringify(data, null, 2);
return this.send(content, options, null);
} catch (error) {
throw new Error('Failed to send notification from API: ' + error.message);
}
}
}
module.exports = DiscordNotification;