muthera 1.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,226 @@
1
+ const Websocket = require("ws");
2
+ const { Rest } = require("./mutheraRest");
3
+
4
+ class Node {
5
+ constructor(muthera, node, options) {
6
+ this.muthera = muthera;
7
+ this.name = node.name || node.host;
8
+ this.host = node.host || "localhost";
9
+ this.port = node.port || 2333;
10
+ this.password = node.password || "youshallnotpass";
11
+ this.secure = node.secure || false;
12
+ this.sessionId = node.sessionId || null;
13
+ this.rest = new Rest(muthera, this);
14
+ this.wsUrl = `ws${this.secure ? "s" : ""}://${this.host}:${
15
+ this.port
16
+ }/v4/websocket`;
17
+ this.restUrl = `http${this.secure ? "s" : ""}://${this.host}:${this.port}`;
18
+ this.ws = null;
19
+ this.send = options.send;
20
+ this.regions = node.regions;
21
+ this.stats = {
22
+ players: 0,
23
+ playingPlayers: 0,
24
+ uptime: 0,
25
+ memory: {
26
+ free: 0,
27
+ used: 0,
28
+ allocated: 0,
29
+ reservable: 0,
30
+ },
31
+ cpu: {
32
+ cores: 0,
33
+ systemLoad: 0,
34
+ lavalinkLoad: 0,
35
+ },
36
+ frameStats: {
37
+ sent: 0,
38
+ nulled: 0,
39
+ deficit: 0,
40
+ },
41
+ };
42
+ this.connected = false;
43
+ this.resumeKey = options.resumeKey || null;
44
+ this.resumeTimeout = options.resumeTimeout || 60;
45
+ this.reconnectTimeout = options.reconnectTimeout || 5000;
46
+ this.reconnectTries = options.reconnectTries || 3;
47
+ this.reconnectAttempt = null;
48
+ this.reconnectAttempted = 1;
49
+ }
50
+
51
+ connect() {
52
+ if (this.ws) this.ws.close();
53
+ const headers = {
54
+ Authorization: this.password,
55
+ "User-Id": this.muthera.clientId,
56
+ "Client-Name": `Muthera@${require("../../package.json").version}`,
57
+ };
58
+
59
+ if (this.sessionId) headers["Session-Id"] = this.sessionId;
60
+
61
+ this.ws = new Websocket(this.wsUrl, { headers });
62
+ this.ws.on("open", this.open.bind(this));
63
+ this.ws.on("error", this.error.bind(this));
64
+ this.ws.on("message", this.message.bind(this));
65
+ this.ws.on("close", this.close.bind(this));
66
+ }
67
+
68
+ open() {
69
+ if (this.reconnectTimeout) clearTimeout(this.reconnectTimeout);
70
+
71
+ this.muthera.emit("nodeConnect", this);
72
+ this.connected = true;
73
+ this.muthera.emit(
74
+ "debug",
75
+ this.name,
76
+ `Lavalink connected on ${this.wsUrl}`
77
+ );
78
+ }
79
+
80
+ error(event) {
81
+ if (!event) return;
82
+ this.muthera.emit("nodeError", this, event);
83
+ }
84
+
85
+ message(msg) {
86
+ if (Array.isArray(msg)) msg = Buffer.concat(msg);
87
+ else if (msg instanceof ArrayBuffer) msg = Buffer.from(msg);
88
+
89
+ const payload = JSON.parse(msg.toString());
90
+ if (!payload.op) return;
91
+
92
+ this.muthera.emit("mutheraRaw", payload);
93
+ this.muthera.emit(
94
+ "debug",
95
+ this.name,
96
+ `Node Update : ${JSON.stringify(payload)}`
97
+ );
98
+
99
+ if (payload.op === "stats") {
100
+ this.stats = { ...payload };
101
+ }
102
+
103
+ if (payload.op === "ready") {
104
+ if (this.sessionId !== payload.sessionId) {
105
+ this.rest.setSessionId(payload.sessionId);
106
+ this.sessionId = payload.sessionId;
107
+ }
108
+
109
+ this.muthera.emit(
110
+ "debug",
111
+ this.name,
112
+ `Payload received ${JSON.stringify(payload)}`
113
+ );
114
+
115
+ if (this.sessionId) {
116
+ this.rest.makeRequest(
117
+ `PATCH`,
118
+ `/${this.rest.version}/sessions/${this.sessionId}`,
119
+ { resuming: true, timeout: this.resumeTimeout }
120
+ );
121
+ this.muthera.emit("debug", this.name, `Node resuming.`);
122
+ }
123
+ }
124
+
125
+ const player = this.muthera.players.get(payload.guildId);
126
+ if (payload.guildId && player) player.emit(payload.op, payload);
127
+ }
128
+
129
+ close(event, reason) {
130
+ this.muthera.emit("nodeDisconnect", this, { event, reason });
131
+ this.muthera.emit(
132
+ "debug",
133
+ `Lavalink connection closed with Error code : ${event || "Unknown code"}`
134
+ );
135
+ this.connected = false;
136
+ this.reconnect();
137
+ }
138
+
139
+ reconnect() {
140
+ this.reconnectAttempt = setTimeout(() => {
141
+ if (this.reconnectAttempted >= this.reconnectTries) {
142
+ const error = new Error(
143
+ `Unable to connect node: ${this.name} after ${this.reconnectTries} attempts.`
144
+ );
145
+
146
+ this.muthera.emit("nodeError", this, error);
147
+ return this.destroy();
148
+ }
149
+
150
+ this.ws.removeAllListeners();
151
+ this.ws = null;
152
+ this.muthera.emit("nodeReconnect", this);
153
+ this.connect();
154
+ this.reconnectAttempted++;
155
+ }, this.reconnectTimeout);
156
+ }
157
+
158
+ destroy() {
159
+ if (!this.connected) return;
160
+
161
+ const player = this.muthera.players.filter(
162
+ (player) => player.node === this
163
+ );
164
+ if (player.size) player.forEach((player) => player.destroy());
165
+
166
+ if (this.ws) this.ws.close(1000, "destroy");
167
+ this.ws.removeAllListeners();
168
+ this.ws = null;
169
+
170
+ this.reconnectAttempted = 1;
171
+ clearTimeout(this.reconnectAttempt);
172
+
173
+ this.muthera.emit("nodeDestroy", this);
174
+ this.muthera.destroyPlayer(player.guildId);
175
+
176
+ this.muthera.nodeMap.delete(this.name);
177
+ this.connected = false;
178
+ }
179
+
180
+ send(payload) {
181
+ const data = JSON.stringify(payload);
182
+ this.ws.send(data, (error) => {
183
+ if (error) return error;
184
+ return null;
185
+ });
186
+ }
187
+
188
+ disconnect() {
189
+ if (!this.connected) return;
190
+ this.muthera.players.forEach((player) => {
191
+ if (player.node == this) {
192
+ player.move();
193
+ }
194
+ });
195
+ this.ws.close(1000, "destroy");
196
+ this.ws?.removeAllListeners();
197
+ this.ws = null;
198
+ this.muthera.nodes.delete(this.name);
199
+ this.muthera.emit("nodeDisconnect", this);
200
+ this.connected = false;
201
+ }
202
+
203
+ get penalties() {
204
+ let penalties = 0;
205
+ if (!this.connected) return penalties;
206
+ if (this.stats.players) {
207
+ penalties += this.stats.players;
208
+ }
209
+ if (this.stats.cpu && this.stats.cpu.systemLoad) {
210
+ penalties += Math.round(
211
+ Math.pow(1.05, 100 * this.stats.cpu.systemLoad) * 10 - 10
212
+ );
213
+ }
214
+ if (this.stats.frameStats) {
215
+ if (this.stats.frameStats.deficit) {
216
+ penalties += this.stats.frameStats.deficit;
217
+ }
218
+ if (this.stats.frameStats.nulled) {
219
+ penalties += this.stats.frameStats.nulled * 2;
220
+ }
221
+ }
222
+ return penalties;
223
+ }
224
+ }
225
+
226
+ module.exports = { Node };