-
Notifications
You must be signed in to change notification settings - Fork 32
/
main.js
83 lines (70 loc) · 2.28 KB
/
main.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
const fs = require("fs");
const gifts = require("./gift");
const dclient = require("./client.js");
const config = require("./config");
const webserver = require("./webserver");
function handleEvent(packet) {
if (packet.t == "MESSAGE_CREATE") {
if (config["show_messages"])
console.log(`${packet.d.author.username} > ${packet.d.content}`);
gifts.checkForGift(packet);
}
}
let tokens = [];
let clients = [];
if (config["read_messages_on_redeem_account"])
tokens.push(config["d_token"]);
if (config["use_multiple_tokens"]) {
if (config["tokens_file"] != undefined) {
let t = fs.readFileSync(config["tokens_file"]).toString();
t.split("\n").forEach(v=>{
v = v.split(";")[0].trim();
if (v != "")
tokens.push(v);
})
} else {
config["tokens"].split(";").forEach(v=>{
v = v.trim();
if (v != "")
tokens.push(v);
})
}
}
if (process.env.PORT || config["force_webserver"]) {
webserver.startWebServer(process.env.PORT)
}
(async ()=>{
gifts.Init();
for (let i = 0; i < tokens.length; i++) {
console.log(`Creating dclient for account n${i}`)
var cl = new dclient(tokens[i], config.d_gateway, handleEvent);
cl.connect();
clients[i] = cl;
await new Promise(res=>setTimeout(res, 1000));
}
setInterval(() => {
const datenow = Date.now();
let msg = "";
for (idx in clients) {
let cl = clients[idx];
if (datenow - cl.last_heartbeat_timestamp > 100000) {
cl.disconnect();
msg += `Account n${idx} last heartbeat was ${~~(datenow - cl.last_heartbeat_timestamp)/1000}s ago\n`;
}
}
if (msg != "") {
msg = msg.trim();
console.log(msg);
gifts.sendWebhook(config.d_err_webhook ? config.d_err_webhook : config.d_webhook, JSON.stringify({
"embeds": [{
"color": 0xFF0000,
"description": msg,
"footer": {
"text": "All above were attempted to reconnect"
},
}
]
}));
}
}, 60000);
})();