-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
127 lines (105 loc) · 3.43 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
const WebSocket = require("ws");
const config = require("./config");
const { extractTopActivity } = require("./utils");
// supported non-browser (for Server-side) only.
let ws = new WebSocket(`wss://gateway.discord.gg/?v=${config.Identification.v}&encoding=json`);
ws.on("message", (raw) => {
// raw Websocket data is made out of Buffer. so you have to convert it into JSON. devhuman-readable.
if (!raw) return;
let data;
try {
data = JSON.parse(Buffer.from(raw).toString("utf-8"));
} catch (err) {
data = null;
return console.error(err);
};
// failed parsing the JSON, well just break it.
if (!data) return;
// console.log(data);
let identifyRequest = JSON.stringify({op: config.OP.identify, d: config.Identification});
let heartbeat = JSON.stringify({op: config.OP.heartbeat, d: config.Constants.seq});
switch(data.op) {
case config.OP.dispatch:
if (data.t === "READY") {
config.Constants.sessionID = data.d.session_id;
console.log("ready.");
};
break;
case config.OP.heartbeat:
ws.send(heartbeat);
console.log("heartbeat.");
break;
case config.OP.invalidSession:
config.Constants.seq = 0;
config.Constants.sessionID = null;
ws.send(identifyRequest);
console.warn("invalid session, identifying.");
break;
case config.OP.reconnect:
if (!ws) return;
clearInterval(config.Constants.heartbeatInterval);
config.Constants.heartbeatInterval = null;
if (ws.readyState !== ws.CLOSED /*&& ws.readyState !== ws.CLOSING*/) {
try {
if (config.Constants.sessionID) {
if (ws.readyState === ws.OPEN) {
console.log("reconnecting.");
ws.close(4901, "reconnect.");
}
else {
console.warn("terminated.");
ws.terminate();
}
} else {
console.log("session continued.");
ws.close(1000, "continue.");
};
} catch (error) {
console.error(error);
};
};
ws = null;
break;
case config.OP.HELLO:
if (data.d.heartbeat_interval > 0) {
if (config.Constants.heartbeatInterval) clearInterval(config.Constants.heartbeatInterval);
config.Constants.heartbeatInterval = setInterval(() => ws.send(heartbeat), data.d.heartbeat_interval);
};
if (config.Constants.sessionID) {
console.log("resuming the connection.");
ws.send(JSON.stringify({
op: config.OP.resume,
d: {
token: config.Identification.token,
session_id: config.Constants.sessionID,
seq: config.Constants.seq
}
}));
} else {
ws.send(identifyRequest);
ws.send(heartbeat);
};
break;
// you and i dont need to know this.
case config.OP.heartbeatACK:
break;
default:
console.log(data);
break;
};
if (
data.t === "PRESENCE_UPDATE" &&
data.d.user.id === config.Constants.userMonitoredID &&
data.op === config.OP.dispatch
) {
// for lurking
//console.log(require("util").inspect(data, false, null, false));
extractTopActivity(JSON.stringify(data.d))
// return redis.set("activity.ray1337", result)
// .catch(console.error);
};
});
ws.on("error", console.error);
ws.on("close", (code, reason) => {
console.log(code, Buffer.from(reason).toString());
});