This repository has been archived by the owner on Sep 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
93 lines (68 loc) · 2.51 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/* eslint prefer-rest-params: "off" */
'use strict';
const Https = require('https');
module.exports = function (webhook) {
if (!webhook) {
throw new Error('No webhook provided.');
}
const self = this;
this.levels = webhook.levels || ['debug', 'trace', 'info', 'warn', 'error', 'fatal'];
this.colors = webhook.colors || { trace: 65535, info: 52224, warn: 14535680, error: 16733440, fatal: 16711680 };
this.username = webhook.username;
this.avatar_url = webhook.avatar_url;
this.webhook = (webhook.url || webhook)
.replace('https://discordapp.com/api/webhooks/', '')
.replace('https://discordapp.com/', '')
.replace('https://discordapp.com', '')
.replace('/webhooks', '')
.replace('webhooks/', '')
.replace('/api', '')
.replace('api/', '');
this.post = function (payload) {
const postData = JSON.stringify(payload);
const request = Https.request({
port: 443,
method: 'POST',
hostname: 'discordapp.com',
path: `/api/webhooks/${ self.webhook }`,
headers: {
'Content-Type': 'application/json',
'Content-Length': postData.length
}
});
request.on('error', (e) => {
throw new Error(e);
});
request.write(postData);
request.end();
};
this.compose = function (color, title, message, usermeme, timestemp) {
const embed = { title: title.title };
const content = message || title.message || title;
const username = usermeme || title.username || this.username;
const timestamp = timestemp || title.timestamp || new Date().toLocaleString();
embed.description = content;
embed.footer = { text: timestamp };
if (title.color || color) {
embed.color = title.color || color;
}
if (message) {
embed.title = title;
}
const payload = { embeds: [embed] };
if (username) {
payload.username = username;
}
if (this.avatar_url) {
payload.avatar_url = this.avatar_url;
}
return payload;
};
this.compose.username = this.username;
for (let i = 0; i < this.levels.length; ++i) {
const level = this.levels[i];
this[level] = function (title, message, username, timestamp) {
self.post(self.compose(self.colors[level] || '', title, message, username, timestamp));
};
}
};