-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwebsocket.ts
610 lines (534 loc) · 15.8 KB
/
websocket.ts
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
import {
Button,
ContextMenuCommand,
Language,
RawEventListener,
SlashCommand,
SlashCommandCategory,
} from "./index";
import {
ComponentType,
GatewayDispatchEvents,
GatewayDispatchPayload,
GatewayHelloData,
GatewayInteractionCreateDispatchData,
GatewayReadyDispatchData,
GatewayReceivePayload,
InteractionType,
} from "discord-api-types/v9";
import {
GatewayIdentifyData,
GatewaySendOpcodes,
LanguageID,
OutgoingGatewayDispatchPayload,
Presence,
Snowflake,
} from "../typings/index";
import Collection from "@discordjs/collection";
import { REST as DiscordRestAPIClient } from "@discordjs/rest";
import { Interaction } from "../internals/index";
import { UniqueID } from "nodejs-snowflake";
import WebSocket from "ws";
import ZLib from "zlib-sync";
import chalk from "chalk";
import erlpack from "erlpack";
interface BotOptions {
token: string;
presence?: Presence;
intents: number;
debug?: boolean;
machineID: number;
}
export class Bot {
readonly debug?: boolean;
public idGenerator: UniqueID;
private __top_secret_TOKEN_dont_expose_this_please: string;
private _inital_presence?: Presence;
private _languages: Collection<LanguageID, Language> = new Collection();
private _defaultLanguage?: LanguageID;
private _slashCommandCategories: Collection<string, SlashCommandCategory> =
new Collection();
private _slashCommands: Collection<string, SlashCommand> = new Collection();
private _contextMenuCommands: Collection<string, ContextMenuCommand> =
new Collection();
private _rawEventListeners: Collection<
GatewayDispatchEvents,
RawEventListener[]
> = new Collection();
private _intents;
protected _ws?: WebSocket;
private webSocketHeartbeatInterval?: number;
private webSocketHeartbeatTimeout?: NodeJS.Timeout;
private lastWebSocketHeartbeatReceivedAcknolegement?: boolean;
private webSocketSequenceNumber = -1;
private webSocketSessionID?: string;
private resumingWebSocketConnection = false;
private webSocketReconnectTimeout?: NodeJS.Timeout;
private inflate = new ZLib.Inflate({
chunkSize: 65535,
// flush: zlib.Z_SYNC_FLUSH,
});
api: DiscordRestAPIClient;
readonly apiVersion = 9;
readonly gatewayEncoding: "etf" | "json" = "etf";
readonly gatewayCompression: "zlib-stream" | null = null;
private gatewayRoot?: string;
private get gatewayAddress() {
if (!this.gatewayRoot)
throw new Error(
"No gateway address set! Call Bot.start, not Bot.connect."
);
return `${this.gatewayRoot}/?v=${this.apiVersion}&encoding=${
this.gatewayEncoding
}${
this.gatewayCompression ? `compress=${this.gatewayCompression}` : ""
}`;
}
public buttonMap = new Collection<Snowflake, Button>();
constructor(options: BotOptions) {
this.idGenerator = new UniqueID({
machineID: options.machineID,
customEpoch: 1420070400000, // Discord's custom epoch
});
options.debug && (this.debug = options.debug);
this.__top_secret_TOKEN_dont_expose_this_please = options.token;
this._inital_presence = options.presence;
this._intents = options.intents;
// Category for commands that aren't in any other ones
this._slashCommandCategories.set(
"__senutila_default_other",
new SlashCommandCategory(
"__senutila_default_other",
"__SENUTILA_DEFAULT_CATEGORY_OTHER",
[]
)
);
this.api = new DiscordRestAPIClient({
version: this.apiVersion.toString(),
});
this.api.setToken(this.__top_secret_TOKEN_dont_expose_this_please);
// console.log("Set api token");
}
public checkThatEverythingHasBeenSetProperly(): boolean | Error {
// Check languages
if (this._languages.size < 1) {
throw new Error("No languages have been registered yet.");
}
if (!this._defaultLanguage) {
throw new Error("You need to set a defualt language.");
}
return true;
}
public setDefaultLanguage(language: LanguageID): Bot {
if (!this._languages.has(language))
throw new Error(
"You have to register the language before you can set it as the defualt."
);
this._defaultLanguage = language;
return this;
}
public registerLanguages(languages: Language[]): Bot {
// Add languages to Collection
for (const language of languages) {
if (this._languages.has(language.id))
throw new Error(
`Multiple languages with the same ID: ${language.id}`
);
language.load(this);
this._languages.set(language.id, language);
}
return this;
}
public addSlashCommands(
thingsToRegister: (SlashCommandCategory | SlashCommand)[]
): Bot {
this.checkThatEverythingHasBeenSetProperly();
for (const thing of thingsToRegister) {
if (thing instanceof SlashCommand) {
// Default category
const category = this._slashCommandCategories.get(
"__senutila_default_other"
);
// Stop people from being stupid
if (!category)
throw new Error(
"If you remove the defualt category, you'll need to put all your commands inside your own categories."
);
// If you want to update a command, then you need to update it with the update method
if (category.commands.has(thing.id))
throw new Error(
`Multiple commands with the same ID: ${thing.id} in the default category.`
);
thing.load(this);
// Add the command to the default slash commands collection
category.commands.set(thing.id, thing);
// Add it to the slash commands collection
this._slashCommands.set(thing.id, thing);
} else {
if (this._slashCommandCategories.has(thing.id))
throw new Error(
`Multiple categories with the same ID: ${thing.id}.`
);
thing.load(this);
// Add the categories' commands to the global commands collection
thing.commands.forEach((command) => {
this._slashCommands.set(command.id, command);
});
// Add the cateogory to the global categories collection
this._slashCommandCategories.set(thing.id, thing);
}
}
return this;
}
public addContextMenuCommands(toRegister: ContextMenuCommand[]): Bot {
this.checkThatEverythingHasBeenSetProperly();
for (const thing of toRegister) {
thing.load(this);
this._contextMenuCommands.set(thing.id, thing);
}
return this;
}
public addRawEventListeners(listeners: RawEventListener[]): Bot {
for (const listener of listeners) {
listener.load(this);
this._rawEventListeners.set(listener.event, [
listener,
...(this._rawEventListeners.get(listener.event) || []),
]);
}
return this;
}
private terminateWebSocketConnection(code?: number) {
if (!this._ws) return;
if (this.webSocketHeartbeatTimeout)
clearTimeout(this.webSocketHeartbeatTimeout);
if (code) {
this._ws.close(code);
} else {
this._ws.terminate();
}
}
private sendWebSocketMessage(
opcode: GatewaySendOpcodes,
payload?: any,
callback?: (err?: Error) => void
) {
if (!this._ws) return false;
let data: OutgoingGatewayDispatchPayload = { op: opcode };
if (payload !== undefined) data.d = payload;
let compressed;
if (this.gatewayEncoding === "etf") {
compressed = erlpack.pack(data);
} else {
compressed = JSON.stringify(data);
}
// console.log(compressed);
return this._ws.send(compressed, callback);
}
private resumeWebSocketConnection(): boolean {
if (this.resumingWebSocketConnection) return false;
if (!this.webSocketSessionID) {
console.warn(
chalk.red("Tried to resume but there is no session ID.")
);
return false;
}
this.resumingWebSocketConnection = true;
console.info(chalk.blue("Resuming connection..."));
this.sendWebSocketMessage(GatewaySendOpcodes.Resume, {
token: this.__top_secret_TOKEN_dont_expose_this_please,
session_id: this.webSocketSessionID,
seq: this.webSocketSequenceNumber,
});
return true;
}
private sendWebSocketHeartbeat(bot: Bot, schedule: boolean = true) {
/*
Becuase this method is sometimes called from a setTimeout,
'this' won't always refer to the Bot instance, so it needs
to be passed in as a parameter. Also remember to not use
'this' in this method, use 'bot' inststead.
*/
if (bot.lastWebSocketHeartbeatReceivedAcknolegement === false) {
bot.terminateWebSocketConnection(1012);
this.resumingWebSocketConnection = true;
bot.resumeWebSocketConnection();
return;
}
bot.lastWebSocketHeartbeatReceivedAcknolegement = false;
// console.log(GatewaySendOpcodes.Heartbeat);
bot.sendWebSocketMessage(GatewaySendOpcodes.Heartbeat, null);
// console.log("Sent heartbeat!");
if (schedule) {
if (!bot.webSocketHeartbeatInterval) return;
bot.webSocketHeartbeatTimeout = setTimeout(
() => bot.sendWebSocketHeartbeat(bot),
bot.webSocketHeartbeatInterval
);
}
}
private sendWebSocketIdentify() {
this.debug && console.info(chalk.blue("Sending identify message..."));
const data: GatewayIdentifyData = {
token: this.__top_secret_TOKEN_dont_expose_this_please,
intents: this._intents,
properties: {
$os: process.platform,
$browser: "Senutila",
$device: "Senutila",
},
...{ presence: this._inital_presence },
};
this.sendWebSocketMessage(GatewaySendOpcodes.Identify, data);
}
private handleWebSocketMessage({
data: rawData,
type,
target,
}: {
data: Buffer;
type: string;
target: WebSocket;
}) {
if (!rawData) return;
let data: any = rawData; // I need a mutable value here
if (this.gatewayCompression === "zlib-stream") {
const l = data.length;
const flush =
l >= 4 &&
data[l - 4] === 0x00 &&
data[l - 3] === 0x00 &&
data[l - 2] === 0xff &&
data[l - 1] === 0xff;
this.inflate.push(data, flush && ZLib.Z_SYNC_FLUSH);
if (!flush) return;
data = this.inflate.result;
}
if (this.gatewayEncoding === "etf") {
if (!Buffer.isBuffer(data))
data = Buffer.from(new Uint8Array(data));
try {
data = erlpack.unpack(data);
} catch (err) {
return console.warn(`Error decoding etf encoded data: ${err}`);
}
} else {
data = JSON.parse(data);
}
// console.log(data);
const messageData = data as GatewayReceivePayload;
const opcode = messageData.op;
const dataContent = messageData.d;
const sequence = messageData.s;
if (sequence) {
this.webSocketSequenceNumber = sequence;
}
// if (opcode !== 0)
this.debug &&
console.info(
chalk.cyan(`Received gateway payload with opcode: ${opcode}`)
);
switch (opcode) {
case 0:
const eventName = (messageData as GatewayDispatchPayload).t;
if (eventName) {
if (eventName === GatewayDispatchEvents.Ready) {
const readyData =
dataContent as GatewayReadyDispatchData;
this.webSocketSessionID = readyData.session_id;
if (this.resumingWebSocketConnection) {
this.resumingWebSocketConnection = false;
this.webSocketReconnectTimeout &&
clearTimeout(this.webSocketReconnectTimeout);
}
}
this.handleWebSocketEventDispatch(eventName, dataContent);
}
break;
case 1:
this.sendWebSocketHeartbeat(this, false);
break;
case 7:
this.terminateWebSocketConnection(1012);
this.connect(this, !!this.webSocketSessionID);
break;
case 9:
if (dataContent === true && !this.resumingWebSocketConnection) {
this.debug &&
console.info(
chalk.yellow(
`Resuming conenction to invalid session...`
)
);
this.resumingWebSocketConnection = true;
this.resumeWebSocketConnection();
} else {
const time = Math.random() * 5 * 1000;
this.debug &&
console.info(
chalk.yellow(
`Restarting bot in ${
time / 1000
} seconds due to invalid session...`
)
);
this.webSocketReconnectTimeout = setTimeout(() => {
this.connect(this, false);
}, time);
}
break;
case 10:
const helloData = dataContent as GatewayHelloData;
this.webSocketHeartbeatInterval = helloData.heartbeat_interval;
// console.log(
// `Heartbeat interval: ${this.webSocketHeartbeatInterval}`
// );
this.webSocketHeartbeatTimeout = setTimeout(() => {
// console.log("About to hearbeat!");
this.sendWebSocketHeartbeat(this);
}, helloData.heartbeat_interval * Math.random());
if (this.resumingWebSocketConnection) {
if (!this.resumeWebSocketConnection())
this.sendWebSocketIdentify();
} else {
this.sendWebSocketIdentify();
}
break;
case 11:
this.lastWebSocketHeartbeatReceivedAcknolegement = true;
break;
}
}
private handleWebSocketEventDispatch(
eventName: GatewayDispatchEvents,
data: any
) {
// if (eventName === GatewayDispatchEvents.Ready && this.debug)
// console.info(chalk.green("Ready!"));
const customHandlers = this._rawEventListeners.get(eventName);
if (customHandlers) {
customHandlers.forEach((handler) => {
handler.handle(data);
});
}
switch (eventName) {
case GatewayDispatchEvents.Resumed:
this.resumingWebSocketConnection = false;
break;
case GatewayDispatchEvents.InteractionCreate:
const interaction =
data as GatewayInteractionCreateDispatchData;
if (interaction.type === InteractionType.ApplicationCommand) {
const command = this._slashCommands.get(
interaction.data.name
);
if (command) command.handle(interaction);
} else if (
interaction.type === InteractionType.MessageComponent
) {
if (interaction.data) {
switch (interaction.data.component_type) {
case ComponentType.Button:
const button = this.buttonMap.get(
interaction.data.custom_id
);
const interactionObject = new Interaction(
this,
interaction
);
if (button) {
button.handle(this, interactionObject);
} else {
this.debug &&
console.warn(
chalk.red(
`Received a message component interaction with no matching ID in internal Map. ID: ${interaction.data.custom_id}`
)
);
interactionObject.reply({
ephemeral: true,
content:
"_COMPONENT_INTERACTION_HANDLER_NOT_FOUND",
});
}
break;
case ComponentType.SelectMenu:
break;
}
}
}
break;
}
}
private connect(bot: Bot, resume: boolean = false): Bot {
const ws = new WebSocket(bot.gatewayAddress);
ws.addEventListener("open", () => {
this.debug && console.info(chalk.green("WebSocket opened!"));
if (resume) {
bot.resumingWebSocketConnection = true;
bot.resumeWebSocketConnection();
}
});
ws.addEventListener("close", (e) => {
ws.terminate();
console.info(
chalk.red(
"WebSocket closed. Will try and reopen in 5 seconds..."
),
e.reason,
e.code
);
bot.webSocketReconnectTimeout = setTimeout(() => {
bot.connect(bot, true);
}, 5 * 1000);
});
ws.addEventListener("error", (event) => {
this.debug && console.warn("WebSocket error:", event.error);
});
ws.addEventListener("message", (event) => {
// console.log(event.data);
bot.handleWebSocketMessage(event);
});
bot._ws = ws;
return bot;
}
public async start(): Promise<Bot> {
const res = (await this.api.get("/gateway/bot")) as {
url: string;
shards: number;
session_start_limit: {
total: number;
remaining: number;
reset_after: number;
max_concurrency: number;
};
};
this.gatewayRoot = res.url;
this.debug &&
console.info(
`Session start limits:
${chalk.blue("Total:")} ${chalk.cyan(res.session_start_limit.total)}
${chalk.blue("Remaining:")} ${chalk.cyan(res.session_start_limit.remaining)}
${chalk.blue("Resets in:")} ${chalk.cyan(
res.session_start_limit.reset_after + "ms"
)}
${chalk.blue("Per 5 seconds:")} ${chalk.cyan(
res.session_start_limit.max_concurrency
)}`
);
this.connect(this);
return this;
}
public get slashCommandCategories() {
return this._slashCommandCategories;
}
public get languages() {
return this._languages;
}
public get defaultLanguage() {
return this._defaultLanguage;
}
}
export function bot(...args: ConstructorParameters<typeof Bot>): Bot {
return new Bot(...args);
}