-
Notifications
You must be signed in to change notification settings - Fork 1
/
webhook.js
71 lines (64 loc) · 2.33 KB
/
webhook.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
const DISCORD = require('discord.js');
var Main = require('./server');
const Logger = require('./logging');
const Client = require('./client');
class Webhook {
constructor(sWebhookID) {
this.Queue = [];
this.ID = sWebhookID;
this.Initialize();
}
Initialize() {
try {
this._webHook = new DISCORD.WebhookClient(Main.Config.webhooks[this.ID].credentials.id, Main.Config.webhooks[this.ID].credentials.token);
Logger.log(1, `Webhook '${Main.Config.webhooks[this.ID].name}' is ready.`);
} catch(ex) {
Logger.log(3, "Invalid credentials for webhook: " + this.ID);
Logger.error("Webhook error: " + ex);
return;
}
}
Send(tEmbed) {
if(this._webHook == null) {
this.Initialize();
this.Queue.push(tEmbed);
return;
}
if(Client.ws.status == 0 && !Main.IntentionalDisconnect) {
this._webHook.send('', { embeds: [tEmbed] })
.catch(Logger.noteError(`[sendData] Sending an embed via WebHook: ${Main.Config.webhooks[this.ID].name}`));
Logger.log(0, `Webhook '${Main.Config.webhooks[this.ID].name}' has send an embed.`);
}
else
{
if(!Main.IntentionalDisconnect)
Logger.log(2, `Webhook '${Main.Config.webhooks[this.ID].name}' isn't ready for sending messages, queuing...`);
this.Queue.push(tEmbed);
}
}
SendQueue(bDestroy)
{
if(Client.ws.status == 0) {
this._webHook.send('', { embeds: this.Queue })
.then(() => {
this.Queue = [];
if(bDestroy) {
this.destroy();
}
})
.catch(Logger.noteError(`[sendData] Sending an embed via WebHook: ${Main.Config.webhooks[this.ID].name}`));
Logger.log(1, `Webhook '${Main.Config.webhooks[this.ID].name}' has send all queued embeds`);
}
else
{
if(!Main.IntentionalDisconnect)
Logger.log(3, `Webhook '${Main.Config.webhooks[this.ID].name}' isn't ready for sending messages, couldn't send queued embeds.`);
this.destroy();
}
}
destroy()
{
this._webHook.destroy();
}
}
module.exports = Webhook;