@discordeno/gateway 19.0.0-next.a196e22 → 19.0.0-next.a26c7c0
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.
- package/dist/Shard.d.ts +9 -89
- package/dist/Shard.d.ts.map +1 -1
- package/dist/Shard.js +51 -184
- package/dist/Shard.js.map +1 -1
- package/dist/manager.d.ts +11 -5
- package/dist/manager.d.ts.map +1 -1
- package/dist/manager.js +89 -46
- package/dist/manager.js.map +1 -1
- package/package.json +3 -3
package/dist/Shard.d.ts
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
import {
|
|
3
|
-
import
|
|
4
|
-
import type {
|
|
5
|
-
import type { BotStatusUpdate, ShardEvents, ShardGatewayConfig, ShardHeart, ShardSocketRequest, StatusUpdate, UpdateVoiceState } from './types.js';
|
|
1
|
+
import type { DiscordGatewayPayload } from '@discordeno/types';
|
|
2
|
+
import { LeakyBucket } from '@discordeno/utils';
|
|
3
|
+
import NodeWebSocket from 'ws';
|
|
4
|
+
import type { BotStatusUpdate, ShardEvents, ShardGatewayConfig, ShardHeart, ShardSocketRequest } from './types.js';
|
|
6
5
|
import { ShardState } from './types.js';
|
|
7
6
|
export declare class DiscordenoShard {
|
|
8
7
|
/** The id of the shard */
|
|
@@ -20,7 +19,7 @@ export declare class DiscordenoShard {
|
|
|
20
19
|
/** Current session id of the shard if present. */
|
|
21
20
|
sessionId?: string;
|
|
22
21
|
/** This contains the WebSocket connection to Discord, if currently connected. */
|
|
23
|
-
socket?:
|
|
22
|
+
socket?: NodeWebSocket;
|
|
24
23
|
/** Current internal state of the this. */
|
|
25
24
|
state: ShardState;
|
|
26
25
|
/** The url provided by discord to use when resuming a connection for this this. */
|
|
@@ -33,22 +32,10 @@ export declare class DiscordenoShard {
|
|
|
33
32
|
resolves: Map<"READY" | "RESUMED" | "INVALID_SESSION", (payload: DiscordGatewayPayload) => void>;
|
|
34
33
|
/** Shard bucket. Only access this if you know what you are doing. Bucket for handling shard request rate limits. */
|
|
35
34
|
bucket: LeakyBucket;
|
|
36
|
-
/** This managers cache related settings. */
|
|
37
|
-
cache: {
|
|
38
|
-
requestMembers: {
|
|
39
|
-
/**
|
|
40
|
-
* Whether or not request member requests should be cached.
|
|
41
|
-
* @default false
|
|
42
|
-
*/
|
|
43
|
-
enabled: boolean;
|
|
44
|
-
/** The pending requests. */
|
|
45
|
-
pending: Collection<string, RequestMemberRequest>;
|
|
46
|
-
};
|
|
47
|
-
};
|
|
48
35
|
constructor(options: ShardCreateOptions);
|
|
49
36
|
/** The gateway configuration which is used to connect to Discord. */
|
|
50
37
|
get gatewayConfig(): ShardGatewayConfig;
|
|
51
|
-
/** The url to connect to.
|
|
38
|
+
/** The url to connect to. Initially this is the discord gateway url, and then is switched to resume gateway url once a READY is received. */
|
|
52
39
|
get connectionUrl(): string;
|
|
53
40
|
/** Calculate the amount of requests which can safely be made per rate limit interval, before the gateway gets disconnected due to an exceeded rate limit. */
|
|
54
41
|
calculateSafeRequests(): number;
|
|
@@ -70,11 +57,12 @@ export declare class DiscordenoShard {
|
|
|
70
57
|
/** Shutdown the this. Forcefully disconnect the shard from Discord. The shard may not attempt to reconnect with Discord. */
|
|
71
58
|
shutdown(): Promise<void>;
|
|
72
59
|
/** Handle a gateway connection close. */
|
|
73
|
-
handleClose(close:
|
|
60
|
+
handleClose(close: NodeWebSocket.CloseEvent): Promise<void>;
|
|
74
61
|
/** Handles a incoming gateway packet. */
|
|
75
62
|
handleDiscordPacket(packet: DiscordGatewayPayload): Promise<void>;
|
|
63
|
+
forwardToBot(packet: DiscordGatewayPayload): void;
|
|
76
64
|
/** Handle an incoming gateway message. */
|
|
77
|
-
handleMessage(message:
|
|
65
|
+
handleMessage(message: NodeWebSocket.MessageEvent): Promise<void>;
|
|
78
66
|
/**
|
|
79
67
|
* Override in order to make the shards presence.
|
|
80
68
|
* async in case devs create the presence based on eg. database values.
|
|
@@ -89,74 +77,6 @@ export declare class DiscordenoShard {
|
|
|
89
77
|
startHeartbeating(interval: number): void;
|
|
90
78
|
/** Stop the heartbeating process with discord. */
|
|
91
79
|
stopHeartbeating(): void;
|
|
92
|
-
/**
|
|
93
|
-
* Connects the bot user to a voice or stage channel.
|
|
94
|
-
*
|
|
95
|
-
* This function sends the _Update Voice State_ gateway command over the gateway behind the scenes.
|
|
96
|
-
*
|
|
97
|
-
* @param guildId - The ID of the guild the voice channel to leave is in.
|
|
98
|
-
* @param channelId - The ID of the channel you want to join.
|
|
99
|
-
*
|
|
100
|
-
* @remarks
|
|
101
|
-
* Requires the `CONNECT` permission.
|
|
102
|
-
*
|
|
103
|
-
* Fires a _Voice State Update_ gateway event.
|
|
104
|
-
*
|
|
105
|
-
* @see {@link https://discord.com/developers/docs/topics/gateway#update-voice-state}
|
|
106
|
-
*/
|
|
107
|
-
joinVoiceChannel(guildId: BigString, channelId: BigString, options?: AtLeastOne<Omit<UpdateVoiceState, 'guildId' | 'channelId'>>): Promise<void>;
|
|
108
|
-
/**
|
|
109
|
-
* Edits the bot status in all shards that this gateway manages.
|
|
110
|
-
*
|
|
111
|
-
* @param data The status data to set the bots status to.
|
|
112
|
-
* @returns Promise<void>
|
|
113
|
-
*/
|
|
114
|
-
editBotStatus(data: StatusUpdate): Promise<void>;
|
|
115
|
-
/**
|
|
116
|
-
* Edits the bot's status on one shard.
|
|
117
|
-
*
|
|
118
|
-
* @param shardId The shard id to edit the status for.
|
|
119
|
-
* @param data The status data to set the bots status to.
|
|
120
|
-
* @returns Promise<void>
|
|
121
|
-
*/
|
|
122
|
-
editShardStatus(data: StatusUpdate): Promise<void>;
|
|
123
|
-
/**
|
|
124
|
-
* Fetches the list of members for a guild over the gateway.
|
|
125
|
-
*
|
|
126
|
-
* @param guildId - The ID of the guild to get the list of members for.
|
|
127
|
-
* @param options - The parameters for the fetching of the members.
|
|
128
|
-
*
|
|
129
|
-
* @remarks
|
|
130
|
-
* If requesting the entire member list:
|
|
131
|
-
* - Requires the `GUILD_MEMBERS` intent.
|
|
132
|
-
*
|
|
133
|
-
* If requesting presences ({@link RequestGuildMembers.presences | presences} set to `true`):
|
|
134
|
-
* - Requires the `GUILD_PRESENCES` intent.
|
|
135
|
-
*
|
|
136
|
-
* If requesting a prefix ({@link RequestGuildMembers.query | query} non-`undefined`):
|
|
137
|
-
* - Returns a maximum of 100 members.
|
|
138
|
-
*
|
|
139
|
-
* If requesting a users by ID ({@link RequestGuildMembers.userIds | userIds} non-`undefined`):
|
|
140
|
-
* - Returns a maximum of 100 members.
|
|
141
|
-
*
|
|
142
|
-
* Fires a _Guild Members Chunk_ gateway event for every 1000 members fetched.
|
|
143
|
-
*
|
|
144
|
-
* @see {@link https://discord.com/developers/docs/topics/gateway#request-guild-members}
|
|
145
|
-
*/
|
|
146
|
-
requestMembers(guildId: BigString, options?: Omit<RequestGuildMembers, 'guildId'>): Promise<Camelize<DiscordMember[]>>;
|
|
147
|
-
/**
|
|
148
|
-
* Leaves the voice channel the bot user is currently in.
|
|
149
|
-
*
|
|
150
|
-
* This function sends the _Update Voice State_ gateway command over the gateway behind the scenes.
|
|
151
|
-
*
|
|
152
|
-
* @param guildId - The ID of the guild the voice channel to leave is in.
|
|
153
|
-
*
|
|
154
|
-
* @remarks
|
|
155
|
-
* Fires a _Voice State Update_ gateway event.
|
|
156
|
-
*
|
|
157
|
-
* @see {@link https://discord.com/developers/docs/topics/gateway#update-voice-state}
|
|
158
|
-
*/
|
|
159
|
-
leaveVoiceChannel(guildId: BigString): Promise<void>;
|
|
160
80
|
}
|
|
161
81
|
export interface ShardCreateOptions {
|
|
162
82
|
/** The shard id */
|
package/dist/Shard.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Shard.d.ts","sourceRoot":"","sources":["../src/Shard.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"Shard.d.ts","sourceRoot":"","sources":["../src/Shard.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,qBAAq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C7B,UAAU,EAAE,kBAAkB,CAAA;IAC9B,kDAAkD;IAClD,MAAM,EAAE,WAAW,CAAA;IACnB,kEAAkE;IAClE,eAAe,CAAC,EAAE,MAAM,OAAO,CAAC,IAAI,CAAC,CAAA;IACrC,2FAA2F;IAC3F,YAAY,CAAC,EAAE,MAAM,OAAO,CAAC,IAAI,CAAC,CAAA;CACnC;AAED,eAAe,eAAe,CAAA"}
|
package/dist/Shard.js
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
|
-
import { GatewayCloseEventCodes,
|
|
2
|
-
import {
|
|
3
|
-
import { clearInterval } from 'node:timers';
|
|
1
|
+
/* eslint-disable @typescript-eslint/no-confusing-void-expression */ import { GatewayCloseEventCodes, GatewayOpcodes } from '@discordeno/types';
|
|
2
|
+
import { camelize, delay, LeakyBucket, logger } from '@discordeno/utils';
|
|
4
3
|
import { inflateSync } from 'node:zlib';
|
|
5
|
-
import
|
|
4
|
+
import NodeWebSocket from 'ws';
|
|
6
5
|
import { ShardSocketCloseCodes, ShardState } from './types.js';
|
|
7
6
|
export class DiscordenoShard {
|
|
8
7
|
constructor(options){
|
|
@@ -14,15 +13,6 @@ export class DiscordenoShard {
|
|
|
14
13
|
/** The shard related event handlers. */ this.events = {};
|
|
15
14
|
/** Cache for pending gateway requests which should have been send while the gateway went offline. */ this.offlineSendQueue = [];
|
|
16
15
|
/** Resolve internal waiting states. Mapped by SelectedEvents => ResolveFunction */ this.resolves = new Map();
|
|
17
|
-
/** This managers cache related settings. */ this.cache = {
|
|
18
|
-
requestMembers: {
|
|
19
|
-
/**
|
|
20
|
-
* Whether or not request member requests should be cached.
|
|
21
|
-
* @default false
|
|
22
|
-
*/ enabled: false,
|
|
23
|
-
/** The pending requests. */ pending: new Collection()
|
|
24
|
-
}
|
|
25
|
-
};
|
|
26
16
|
this.id = options.id;
|
|
27
17
|
this.connection = options.connection;
|
|
28
18
|
this.events = options.events;
|
|
@@ -41,7 +31,7 @@ export class DiscordenoShard {
|
|
|
41
31
|
/** The gateway configuration which is used to connect to Discord. */ get gatewayConfig() {
|
|
42
32
|
return this.connection;
|
|
43
33
|
}
|
|
44
|
-
/** The url to connect to.
|
|
34
|
+
/** The url to connect to. Initially this is the discord gateway url, and then is switched to resume gateway url once a READY is received. */ get connectionUrl() {
|
|
45
35
|
// Use || and not ?? here. ?? will cause a bug.
|
|
46
36
|
return this.resumeGatewayUrl || this.gatewayConfig.url;
|
|
47
37
|
}
|
|
@@ -60,8 +50,8 @@ export class DiscordenoShard {
|
|
|
60
50
|
}
|
|
61
51
|
}
|
|
62
52
|
/** Close the socket connection to discord if present. */ close(code, reason) {
|
|
63
|
-
if (this.socket?.readyState !==
|
|
64
|
-
|
|
53
|
+
if (this.socket?.readyState !== NodeWebSocket.OPEN) return;
|
|
54
|
+
this.socket?.close(code, reason);
|
|
65
55
|
}
|
|
66
56
|
/** Connect the shard with the gateway and start heartbeating. This will not identify the shard to the gateway. */ async connect() {
|
|
67
57
|
// Only set the shard to `Connecting` state,
|
|
@@ -76,7 +66,8 @@ export class DiscordenoShard {
|
|
|
76
66
|
const url = new URL(this.connectionUrl);
|
|
77
67
|
url.searchParams.set('v', this.gatewayConfig.version.toString());
|
|
78
68
|
url.searchParams.set('encoding', 'json');
|
|
79
|
-
const socket =
|
|
69
|
+
const socket = // @ts-expect-error Deno
|
|
70
|
+
globalThis.Deno !== undefined && Reflect.has(globalThis, 'Deno') ? new WebSocket(url.toString()) : new NodeWebSocket(url.toString());
|
|
80
71
|
this.socket = socket;
|
|
81
72
|
// TODO: proper event handling
|
|
82
73
|
socket.onerror = (event)=>console.log({
|
|
@@ -146,13 +137,14 @@ export class DiscordenoShard {
|
|
|
146
137
|
});
|
|
147
138
|
}
|
|
148
139
|
/** Check whether the connection to Discord is currently open. */ isOpen() {
|
|
149
|
-
return this.socket?.readyState ===
|
|
140
|
+
return this.socket?.readyState === NodeWebSocket.OPEN;
|
|
150
141
|
}
|
|
151
142
|
/** Attempt to resume the previous shards session with the gateway. */ async resume() {
|
|
152
143
|
logger.debug(`[Gateway] Resuming Shard #${this.id}`);
|
|
153
144
|
// It has been requested to resume the Shards session.
|
|
154
145
|
// It's possible that the shard is still connected with Discord's gateway therefore we need to forcefully close it.
|
|
155
146
|
if (this.isOpen()) {
|
|
147
|
+
logger.debug(`[Gateway] Resuming Shard #${this.id} in isOpen`);
|
|
156
148
|
this.close(ShardSocketCloseCodes.ResumeClosingOldConnection, 'Reconnecting the shard, closing old connection.');
|
|
157
149
|
}
|
|
158
150
|
// Shard has never identified, so we cannot resume.
|
|
@@ -161,8 +153,11 @@ export class DiscordenoShard {
|
|
|
161
153
|
return await this.identify();
|
|
162
154
|
}
|
|
163
155
|
this.state = ShardState.Resuming;
|
|
156
|
+
logger.debug(`[Gateway] Resuming Shard #${this.id}, before connecting`);
|
|
164
157
|
// Before we can resume, we need to create a new connection with Discord's gateway.
|
|
165
158
|
await this.connect();
|
|
159
|
+
logger.debug(// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
|
|
160
|
+
`[Gateway] Resuming Shard #${this.id}, after connecting. ${this.gatewayConfig.token} | ${this.sessionId} | ${this.previousSequenceNumber}`);
|
|
166
161
|
this.send({
|
|
167
162
|
op: GatewayOpcodes.Resume,
|
|
168
163
|
d: {
|
|
@@ -171,6 +166,7 @@ export class DiscordenoShard {
|
|
|
171
166
|
seq: this.previousSequenceNumber ?? 0
|
|
172
167
|
}
|
|
173
168
|
}, true);
|
|
169
|
+
logger.debug(`[Gateway] Resuming Shard #${this.id} after send resumg`);
|
|
174
170
|
return await new Promise((resolve)=>{
|
|
175
171
|
this.resolves.set('RESUMED', ()=>resolve());
|
|
176
172
|
// If it is attempted to resume with an invalid session id,
|
|
@@ -250,6 +246,7 @@ export class DiscordenoShard {
|
|
|
250
246
|
case GatewayCloseEventCodes.AlreadyAuthenticated:
|
|
251
247
|
default:
|
|
252
248
|
{
|
|
249
|
+
logger.info(`[Shard] closed shard #${this.id}. Resuming...`);
|
|
253
250
|
this.state = ShardState.Resuming;
|
|
254
251
|
this.events.disconnected?.(this);
|
|
255
252
|
return await this.resume();
|
|
@@ -283,6 +280,7 @@ export class DiscordenoShard {
|
|
|
283
280
|
case GatewayOpcodes.Hello:
|
|
284
281
|
{
|
|
285
282
|
const interval = packet.d.heartbeat_interval;
|
|
283
|
+
logger.debug(`[Gateway] Hello on Shard #${this.id}`);
|
|
286
284
|
this.startHeartbeating(interval);
|
|
287
285
|
if (this.state !== ShardState.Resuming) {
|
|
288
286
|
const currentQueue = [
|
|
@@ -334,25 +332,29 @@ export class DiscordenoShard {
|
|
|
334
332
|
break;
|
|
335
333
|
}
|
|
336
334
|
}
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
335
|
+
switch(packet.t){
|
|
336
|
+
case 'RESUMED':
|
|
337
|
+
this.state = ShardState.Connected;
|
|
338
|
+
this.events.resumed?.(this);
|
|
339
|
+
// Continue the requests which have been queued since the shard went offline.
|
|
340
|
+
this.offlineSendQueue.map((resolve)=>resolve());
|
|
341
|
+
this.resolves.get('RESUMED')?.(packet);
|
|
342
|
+
this.resolves.delete('RESUMED');
|
|
343
|
+
break;
|
|
344
|
+
case 'READY':
|
|
345
|
+
{
|
|
346
|
+
// Important for future resumes.
|
|
347
|
+
const payload = packet.d;
|
|
348
|
+
this.resumeGatewayUrl = payload.resume_gateway_url;
|
|
349
|
+
this.sessionId = payload.session_id;
|
|
350
|
+
this.state = ShardState.Connected;
|
|
351
|
+
// Continue the requests which have been queued since the shard went offline.
|
|
352
|
+
// Important when this is a re-identify
|
|
353
|
+
this.offlineSendQueue.map((resolve)=>resolve());
|
|
354
|
+
this.resolves.get('READY')?.(packet);
|
|
355
|
+
this.resolves.delete('READY');
|
|
356
|
+
break;
|
|
357
|
+
}
|
|
356
358
|
}
|
|
357
359
|
// Update the sequence number if it is present
|
|
358
360
|
// `s` can be either `null` or a `number`.
|
|
@@ -360,6 +362,9 @@ export class DiscordenoShard {
|
|
|
360
362
|
if (packet.s !== null) {
|
|
361
363
|
this.previousSequenceNumber = packet.s;
|
|
362
364
|
}
|
|
365
|
+
this.forwardToBot(packet);
|
|
366
|
+
}
|
|
367
|
+
forwardToBot(packet) {
|
|
363
368
|
// The necessary handling required for the Shards connection has been finished.
|
|
364
369
|
// Now the event can be safely forwarded.
|
|
365
370
|
this.events.message?.(this, camelize(packet));
|
|
@@ -386,6 +391,7 @@ export class DiscordenoShard {
|
|
|
386
391
|
/** This function communicates with the management process, in order to know whether its free to identify. When this function resolves, this means that the shard is allowed to send an identify payload to discord. */ async requestIdentify() {}
|
|
387
392
|
/** This function communicates with the management process, in order to tell it can identify the next shard. */ async shardIsReady() {}
|
|
388
393
|
/** Start sending heartbeat payloads to Discord in the provided interval. */ startHeartbeating(interval) {
|
|
394
|
+
logger.debug(`[Gateway] Start Heartbeating Shard #${this.id}`);
|
|
389
395
|
// If old heartbeast exist like after resume, clear the old ones.
|
|
390
396
|
if (this.heart.intervalId) clearInterval(this.heart.intervalId);
|
|
391
397
|
if (this.heart.timeoutId) clearTimeout(this.heart.timeoutId);
|
|
@@ -396,6 +402,7 @@ export class DiscordenoShard {
|
|
|
396
402
|
ShardState.Disconnected,
|
|
397
403
|
ShardState.Offline
|
|
398
404
|
].includes(this.state)) {
|
|
405
|
+
logger.debug(`[Gateway] Start Heartbeating Shard #${this.id} a`);
|
|
399
406
|
this.state = ShardState.Unidentified;
|
|
400
407
|
}
|
|
401
408
|
// The first heartbeat needs to be send with a random delay between `0` and `interval`
|
|
@@ -404,17 +411,22 @@ export class DiscordenoShard {
|
|
|
404
411
|
// Reference: https://discord.com/developers/docs/topics/gateway#heartbeating
|
|
405
412
|
const jitter = Math.ceil(this.heart.interval * (Math.random() || 0.5));
|
|
406
413
|
this.heart.timeoutId = setTimeout(()=>{
|
|
414
|
+
logger.debug(`[Gateway] Start Heartbeating Shard #${this.id} b`);
|
|
407
415
|
if (!this.isOpen()) return;
|
|
416
|
+
logger.debug(`[Gateway] Start Heartbeating Shard #${this.id} c ${this.previousSequenceNumber}`);
|
|
408
417
|
// Using a direct socket.send call here because heartbeat requests are reserved by us.
|
|
409
418
|
this.socket?.send(JSON.stringify({
|
|
410
419
|
op: GatewayOpcodes.Heartbeat,
|
|
411
420
|
d: this.previousSequenceNumber
|
|
412
421
|
}));
|
|
422
|
+
logger.debug(`[Gateway] Start Heartbeating Shard #${this.id} d`);
|
|
413
423
|
this.heart.lastBeat = Date.now();
|
|
414
424
|
this.heart.acknowledged = false;
|
|
415
425
|
// After the random heartbeat jitter we can start a normal interval.
|
|
416
426
|
this.heart.intervalId = setInterval(async ()=>{
|
|
427
|
+
logger.debug(`[Gateway] Start Heartbeating Shard #${this.id} e`);
|
|
417
428
|
if (!this.isOpen()) return;
|
|
429
|
+
logger.debug(`[Gateway] Start Heartbeating Shard #${this.id} f`);
|
|
418
430
|
// gateway.debug("GW DEBUG", `Running setInterval in heartbeat file. Shard: ${shardId}`);
|
|
419
431
|
// gateway.debug("GW HEARTBEATING", { shardId, shard: currentShard });
|
|
420
432
|
// The Shard did not receive a heartbeat ACK from Discord in time,
|
|
@@ -427,11 +439,13 @@ export class DiscordenoShard {
|
|
|
427
439
|
return await this.identify();
|
|
428
440
|
}
|
|
429
441
|
this.heart.acknowledged = false;
|
|
442
|
+
logger.debug(`[Gateway] Start Heartbeating Shard #${this.id} g`);
|
|
430
443
|
// Using a direct socket.send call here because heartbeat requests are reserved by us.
|
|
431
444
|
this.socket?.send(JSON.stringify({
|
|
432
445
|
op: GatewayOpcodes.Heartbeat,
|
|
433
446
|
d: this.previousSequenceNumber
|
|
434
447
|
}));
|
|
448
|
+
logger.debug(`[Gateway] Start Heartbeating Shard #${this.id} h`);
|
|
435
449
|
this.heart.lastBeat = Date.now();
|
|
436
450
|
this.events.heartbeat?.(this);
|
|
437
451
|
}, this.heart.interval);
|
|
@@ -444,153 +458,6 @@ export class DiscordenoShard {
|
|
|
444
458
|
// To go safe we should clear the related timeout too.
|
|
445
459
|
clearTimeout(this.heart.timeoutId);
|
|
446
460
|
}
|
|
447
|
-
/**
|
|
448
|
-
* Connects the bot user to a voice or stage channel.
|
|
449
|
-
*
|
|
450
|
-
* This function sends the _Update Voice State_ gateway command over the gateway behind the scenes.
|
|
451
|
-
*
|
|
452
|
-
* @param guildId - The ID of the guild the voice channel to leave is in.
|
|
453
|
-
* @param channelId - The ID of the channel you want to join.
|
|
454
|
-
*
|
|
455
|
-
* @remarks
|
|
456
|
-
* Requires the `CONNECT` permission.
|
|
457
|
-
*
|
|
458
|
-
* Fires a _Voice State Update_ gateway event.
|
|
459
|
-
*
|
|
460
|
-
* @see {@link https://discord.com/developers/docs/topics/gateway#update-voice-state}
|
|
461
|
-
*/ async joinVoiceChannel(guildId, channelId, options) {
|
|
462
|
-
logger.debug(`[Shard] joinVoiceChannel guildId: ${guildId} channelId: ${channelId}`);
|
|
463
|
-
return await this.send({
|
|
464
|
-
op: GatewayOpcodes.VoiceStateUpdate,
|
|
465
|
-
d: {
|
|
466
|
-
guild_id: guildId.toString(),
|
|
467
|
-
channel_id: channelId.toString(),
|
|
468
|
-
self_mute: Boolean(options?.selfMute),
|
|
469
|
-
self_deaf: options?.selfDeaf ?? true
|
|
470
|
-
}
|
|
471
|
-
});
|
|
472
|
-
}
|
|
473
|
-
/**
|
|
474
|
-
* Edits the bot status in all shards that this gateway manages.
|
|
475
|
-
*
|
|
476
|
-
* @param data The status data to set the bots status to.
|
|
477
|
-
* @returns Promise<void>
|
|
478
|
-
*/ async editBotStatus(data) {
|
|
479
|
-
logger.debug(`[Shard] editBotStatus data: ${JSON.stringify(data)}`);
|
|
480
|
-
return await this.editShardStatus(data);
|
|
481
|
-
}
|
|
482
|
-
/**
|
|
483
|
-
* Edits the bot's status on one shard.
|
|
484
|
-
*
|
|
485
|
-
* @param shardId The shard id to edit the status for.
|
|
486
|
-
* @param data The status data to set the bots status to.
|
|
487
|
-
* @returns Promise<void>
|
|
488
|
-
*/ async editShardStatus(data) {
|
|
489
|
-
logger.debug(`[Shard] editShardStatus shardId: ${this.id} -> data: ${JSON.stringify(data)}`);
|
|
490
|
-
return await this.send({
|
|
491
|
-
op: GatewayOpcodes.PresenceUpdate,
|
|
492
|
-
d: {
|
|
493
|
-
since: null,
|
|
494
|
-
afk: false,
|
|
495
|
-
activities: data.activities,
|
|
496
|
-
status: data.status
|
|
497
|
-
}
|
|
498
|
-
});
|
|
499
|
-
}
|
|
500
|
-
/**
|
|
501
|
-
* Fetches the list of members for a guild over the gateway.
|
|
502
|
-
*
|
|
503
|
-
* @param guildId - The ID of the guild to get the list of members for.
|
|
504
|
-
* @param options - The parameters for the fetching of the members.
|
|
505
|
-
*
|
|
506
|
-
* @remarks
|
|
507
|
-
* If requesting the entire member list:
|
|
508
|
-
* - Requires the `GUILD_MEMBERS` intent.
|
|
509
|
-
*
|
|
510
|
-
* If requesting presences ({@link RequestGuildMembers.presences | presences} set to `true`):
|
|
511
|
-
* - Requires the `GUILD_PRESENCES` intent.
|
|
512
|
-
*
|
|
513
|
-
* If requesting a prefix ({@link RequestGuildMembers.query | query} non-`undefined`):
|
|
514
|
-
* - Returns a maximum of 100 members.
|
|
515
|
-
*
|
|
516
|
-
* If requesting a users by ID ({@link RequestGuildMembers.userIds | userIds} non-`undefined`):
|
|
517
|
-
* - Returns a maximum of 100 members.
|
|
518
|
-
*
|
|
519
|
-
* Fires a _Guild Members Chunk_ gateway event for every 1000 members fetched.
|
|
520
|
-
*
|
|
521
|
-
* @see {@link https://discord.com/developers/docs/topics/gateway#request-guild-members}
|
|
522
|
-
*/ async requestMembers(guildId, options) {
|
|
523
|
-
// You can request 1 member without the intent
|
|
524
|
-
// Check if intents is not 0 as proxy ws won't set intents in other instances
|
|
525
|
-
if (this.connection.intents && (!options?.limit || options.limit > 1) && !(this.connection.intents & GatewayIntents.GuildMembers)) {
|
|
526
|
-
throw new Error('MISSING_INTENT_GUILD_MEMBERS');
|
|
527
|
-
}
|
|
528
|
-
if (options?.userIds?.length) {
|
|
529
|
-
logger.debug(`[Shard] requestMembers guildId: ${guildId} -> setting user limit based on userIds length: ${options.userIds.length}`);
|
|
530
|
-
options.limit = options.userIds.length;
|
|
531
|
-
}
|
|
532
|
-
const nonce = `${guildId}-${Date.now()}`;
|
|
533
|
-
// Gateway does not require caching these requests so directly send and return
|
|
534
|
-
if (!this.cache.requestMembers?.enabled) {
|
|
535
|
-
logger.debug(`[Shard] requestMembers guildId: ${guildId} -> skipping cache -> options ${JSON.stringify(options)}`);
|
|
536
|
-
await this.send({
|
|
537
|
-
op: GatewayOpcodes.RequestGuildMembers,
|
|
538
|
-
d: {
|
|
539
|
-
guild_id: guildId.toString(),
|
|
540
|
-
// If a query is provided use it, OR if a limit is NOT provided use ""
|
|
541
|
-
query: options?.query ?? (options?.limit ? undefined : ''),
|
|
542
|
-
limit: options?.limit ?? 0,
|
|
543
|
-
presences: options?.presences ?? false,
|
|
544
|
-
user_ids: options?.userIds?.map((id)=>id.toString()),
|
|
545
|
-
nonce
|
|
546
|
-
}
|
|
547
|
-
});
|
|
548
|
-
return [];
|
|
549
|
-
}
|
|
550
|
-
return await new Promise((resolve)=>{
|
|
551
|
-
this.cache.requestMembers?.pending.set(nonce, {
|
|
552
|
-
nonce,
|
|
553
|
-
resolve,
|
|
554
|
-
members: []
|
|
555
|
-
});
|
|
556
|
-
logger.debug(`[Shard] requestMembers guildId: ${guildId} -> requesting members -> data: ${JSON.stringify(options)}`);
|
|
557
|
-
this.send({
|
|
558
|
-
op: GatewayOpcodes.RequestGuildMembers,
|
|
559
|
-
d: {
|
|
560
|
-
guild_id: guildId.toString(),
|
|
561
|
-
// If a query is provided use it, OR if a limit is NOT provided use ""
|
|
562
|
-
query: options?.query ?? (options?.limit ? undefined : ''),
|
|
563
|
-
limit: options?.limit ?? 0,
|
|
564
|
-
presences: options?.presences ?? false,
|
|
565
|
-
user_ids: options?.userIds?.map((id)=>id.toString()),
|
|
566
|
-
nonce
|
|
567
|
-
}
|
|
568
|
-
});
|
|
569
|
-
});
|
|
570
|
-
}
|
|
571
|
-
/**
|
|
572
|
-
* Leaves the voice channel the bot user is currently in.
|
|
573
|
-
*
|
|
574
|
-
* This function sends the _Update Voice State_ gateway command over the gateway behind the scenes.
|
|
575
|
-
*
|
|
576
|
-
* @param guildId - The ID of the guild the voice channel to leave is in.
|
|
577
|
-
*
|
|
578
|
-
* @remarks
|
|
579
|
-
* Fires a _Voice State Update_ gateway event.
|
|
580
|
-
*
|
|
581
|
-
* @see {@link https://discord.com/developers/docs/topics/gateway#update-voice-state}
|
|
582
|
-
*/ async leaveVoiceChannel(guildId) {
|
|
583
|
-
logger.debug(`[Shard] leaveVoiceChannel guildId: ${guildId} Shard ${this.id}`);
|
|
584
|
-
return await this.send({
|
|
585
|
-
op: GatewayOpcodes.VoiceStateUpdate,
|
|
586
|
-
d: {
|
|
587
|
-
guild_id: guildId.toString(),
|
|
588
|
-
channel_id: null,
|
|
589
|
-
self_mute: false,
|
|
590
|
-
self_deaf: false
|
|
591
|
-
}
|
|
592
|
-
});
|
|
593
|
-
}
|
|
594
461
|
}
|
|
595
462
|
export default DiscordenoShard;
|
|
596
463
|
|
package/dist/Shard.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/Shard.ts"],"sourcesContent":["import type {\n AtLeastOne,\n BigString,\n Camelize,\n DiscordGatewayPayload,\n DiscordHello,\n DiscordMember,\n DiscordReady,\n RequestGuildMembers,\n} from '@discordeno/types'\nimport { GatewayCloseEventCodes, GatewayIntents, GatewayOpcodes } from '@discordeno/types'\nimport { Collection, LeakyBucket, camelize, delay, logger } from '@discordeno/utils'\nimport { clearInterval } from 'node:timers'\nimport { inflateSync } from 'node:zlib'\nimport WebSocket from 'ws'\nimport type { RequestMemberRequest } from './manager.js'\nimport type { BotStatusUpdate, ShardEvents, ShardGatewayConfig, ShardHeart, ShardSocketRequest, StatusUpdate, UpdateVoiceState } from './types.js'\nimport { ShardSocketCloseCodes, ShardState } from './types.js'\n\nexport class DiscordenoShard {\n /** The id of the shard */\n id: number\n /** The connection config details that this shard will used to connect to discord. */\n connection: ShardGatewayConfig\n /** This contains all the heartbeat information */\n heart: ShardHeart\n /** The maximum of requests which can be send to discord per rate limit tick. Typically this value should not be changed. */\n maxRequestsPerRateLimitTick: number = 120\n /** The previous payload sequence number. */\n previousSequenceNumber: number | null = null\n /** In which interval (in milliseconds) the gateway resets it's rate limit. */\n rateLimitResetInterval: number = 60000\n /** Current session id of the shard if present. */\n sessionId?: string\n /** This contains the WebSocket connection to Discord, if currently connected. */\n socket?: WebSocket\n /** Current internal state of the this. */\n state = ShardState.Offline\n /** The url provided by discord to use when resuming a connection for this this. */\n resumeGatewayUrl: string = ''\n /** The shard related event handlers. */\n events: ShardEvents = {}\n /** Cache for pending gateway requests which should have been send while the gateway went offline. */\n offlineSendQueue: Array<(_?: unknown) => void> = []\n /** Resolve internal waiting states. Mapped by SelectedEvents => ResolveFunction */\n resolves = new Map<'READY' | 'RESUMED' | 'INVALID_SESSION', (payload: DiscordGatewayPayload) => void>()\n /** Shard bucket. Only access this if you know what you are doing. Bucket for handling shard request rate limits. */\n bucket: LeakyBucket\n\n /** This managers cache related settings. */\n cache = {\n requestMembers: {\n /**\n * Whether or not request member requests should be cached.\n * @default false\n */\n enabled: false,\n /** The pending requests. */\n pending: new Collection<string, RequestMemberRequest>(),\n },\n }\n\n constructor(options: ShardCreateOptions) {\n this.id = options.id\n this.connection = options.connection\n this.events = options.events\n\n this.heart = {\n acknowledged: false,\n interval: 45000,\n }\n\n if (options.requestIdentify) this.requestIdentify = options.requestIdentify\n if (options.shardIsReady) this.shardIsReady = options.shardIsReady\n\n this.bucket = new LeakyBucket({\n max: this.calculateSafeRequests(),\n refillAmount: this.calculateSafeRequests(),\n refillInterval: 60000,\n })\n }\n\n /** The gateway configuration which is used to connect to Discord. */\n get gatewayConfig(): ShardGatewayConfig {\n return this.connection\n }\n\n /** The url to connect to. Intially this is the discord gateway url, and then is switched to resume gateway url once a READY is received. */\n get connectionUrl(): string {\n // Use || and not ?? here. ?? will cause a bug.\n return this.resumeGatewayUrl || this.gatewayConfig.url\n }\n\n /** Calculate the amount of requests which can safely be made per rate limit interval, before the gateway gets disconnected due to an exceeded rate limit. */\n calculateSafeRequests(): number {\n // * 2 adds extra safety layer for discords OP 1 requests that we need to respond to\n const safeRequests = this.maxRequestsPerRateLimitTick - Math.ceil(this.rateLimitResetInterval / this.heart.interval) * 2\n\n return safeRequests < 0 ? 0 : safeRequests\n }\n\n async checkOffline(highPriority: boolean): Promise<void> {\n if (!this.isOpen()) {\n await new Promise((resolve) => {\n // Higher priority requests get added at the beginning of the array.\n if (highPriority) this.offlineSendQueue.unshift(resolve)\n else this.offlineSendQueue.push(resolve)\n })\n }\n }\n\n /** Close the socket connection to discord if present. */\n close(code: number, reason: string): void {\n if (this.socket?.readyState !== WebSocket.OPEN) return\n\n return this.socket?.close(code, reason)\n }\n\n /** Connect the shard with the gateway and start heartbeating. This will not identify the shard to the gateway. */\n async connect(): Promise<DiscordenoShard> {\n // Only set the shard to `Connecting` state,\n // if the connection request does not come from an identify or resume action.\n if (![ShardState.Identifying, ShardState.Resuming].includes(this.state)) {\n this.state = ShardState.Connecting\n }\n this.events.connecting?.(this)\n\n const url = new URL(this.connectionUrl)\n url.searchParams.set('v', this.gatewayConfig.version.toString())\n url.searchParams.set('encoding', 'json')\n\n const socket = new WebSocket(url.toString())\n this.socket = socket\n\n // TODO: proper event handling\n socket.onerror = (event) => console.log({ error: event, shardId: this.id })\n socket.onclose = async (event) => await this.handleClose(event)\n socket.onmessage = async (message) => await this.handleMessage(message)\n\n return await new Promise((resolve) => {\n socket.onopen = () => {\n // Only set the shard to `Unidentified` state,\n // if the connection request does not come from an identify or resume action.\n if (![ShardState.Identifying, ShardState.Resuming].includes(this.state)) {\n this.state = ShardState.Unidentified\n }\n this.events.connected?.(this)\n\n resolve(this)\n }\n })\n }\n\n /** Identify the shard to the gateway. If not connected, this will also connect the shard to the gateway. */\n async identify(): Promise<void> {\n // A new identify has been requested even though there is already a connection open.\n // Therefore we need to close the old connection and heartbeating before creating a new one.\n if (this.isOpen()) {\n logger.debug(`CLOSING EXISTING SHARD: #${this.id}`)\n this.close(ShardSocketCloseCodes.ReIdentifying, 'Re-identifying closure of old connection.')\n }\n\n this.state = ShardState.Identifying\n this.events.identifying?.(this)\n\n // It is possible that the shard is in Heartbeating state but not identified,\n // so check whether there is already a gateway connection existing.\n // If not we need to create one before we identify.\n if (!this.isOpen()) {\n await this.connect()\n }\n\n this.send(\n {\n op: GatewayOpcodes.Identify,\n d: {\n token: `Bot ${this.gatewayConfig.token}`,\n compress: this.gatewayConfig.compress,\n properties: this.gatewayConfig.properties,\n intents: this.gatewayConfig.intents,\n shard: [this.id, this.gatewayConfig.totalShards],\n presence: await this.makePresence?.(),\n },\n },\n true,\n )\n\n return await new Promise((resolve) => {\n this.resolves.set('READY', () => {\n this.events.identified?.(this)\n // Tells the manager that this shard is ready\n this.shardIsReady()\n resolve()\n })\n // When identifying too fast,\n // Discord sends an invalid session payload.\n // This can safely be ignored though and the shard starts a new identify action.\n this.resolves.set('INVALID_SESSION', () => {\n this.resolves.delete('READY')\n resolve()\n })\n })\n }\n\n /** Check whether the connection to Discord is currently open. */\n isOpen(): boolean {\n return this.socket?.readyState === WebSocket.OPEN\n }\n\n /** Attempt to resume the previous shards session with the gateway. */\n async resume(): Promise<void> {\n logger.debug(`[Gateway] Resuming Shard #${this.id}`)\n // It has been requested to resume the Shards session.\n // It's possible that the shard is still connected with Discord's gateway therefore we need to forcefully close it.\n if (this.isOpen()) {\n this.close(ShardSocketCloseCodes.ResumeClosingOldConnection, 'Reconnecting the shard, closing old connection.')\n }\n\n // Shard has never identified, so we cannot resume.\n if (!this.sessionId) {\n logger.debug(`[Shard] Trying to resume a shard #${this.id} that was NOT first identified. (No session id found)`)\n\n return await this.identify()\n }\n\n this.state = ShardState.Resuming\n\n // Before we can resume, we need to create a new connection with Discord's gateway.\n await this.connect()\n\n this.send(\n {\n op: GatewayOpcodes.Resume,\n d: {\n token: `Bot ${this.gatewayConfig.token}`,\n session_id: this.sessionId,\n seq: this.previousSequenceNumber ?? 0,\n },\n },\n true,\n )\n\n return await new Promise((resolve) => {\n this.resolves.set('RESUMED', () => resolve())\n // If it is attempted to resume with an invalid session id,\n // Discord sends an invalid session payload\n // Not erroring here since it is easy that this happens, also it would be not catchable\n this.resolves.set('INVALID_SESSION', () => {\n this.resolves.delete('RESUMED')\n resolve()\n })\n })\n }\n\n /** Send a message to Discord.\n * @param {boolean} [highPriority=false] - Whether this message should be send asap.\n */\n async send(message: ShardSocketRequest, highPriority = false): Promise<void> {\n // Before acquiring a token from the bucket, check whether the shard is currently offline or not.\n // Else bucket and token wait time just get wasted.\n await this.checkOffline(highPriority)\n\n await this.bucket.acquire(highPriority)\n\n // It's possible, that the shard went offline after a token has been acquired from the bucket.\n await this.checkOffline(highPriority)\n\n this.socket?.send(JSON.stringify(message))\n }\n\n /** Shutdown the this. Forcefully disconnect the shard from Discord. The shard may not attempt to reconnect with Discord. */\n async shutdown(): Promise<void> {\n this.close(ShardSocketCloseCodes.Shutdown, 'Shard shutting down.')\n this.state = ShardState.Offline\n }\n\n /** Handle a gateway connection close. */\n async handleClose(close: WebSocket.CloseEvent): Promise<void> {\n // gateway.debug(\"GW CLOSED\", { shardId, payload: event });\n\n this.stopHeartbeating()\n\n switch (close.code) {\n case ShardSocketCloseCodes.TestingFinished: {\n this.state = ShardState.Offline\n this.events.disconnected?.(this)\n\n return\n }\n // On these codes a manual start will be done.\n case ShardSocketCloseCodes.Shutdown:\n case ShardSocketCloseCodes.ReIdentifying:\n case ShardSocketCloseCodes.Resharded:\n case ShardSocketCloseCodes.ResumeClosingOldConnection:\n case ShardSocketCloseCodes.ZombiedConnection: {\n this.state = ShardState.Disconnected\n this.events.disconnected?.(this)\n\n // gateway.debug(\"GW CLOSED_RECONNECT\", { shardId, payload: event });\n return\n }\n // Gateway connection closes which require a new identify.\n case GatewayCloseEventCodes.UnknownOpcode:\n case GatewayCloseEventCodes.NotAuthenticated:\n case GatewayCloseEventCodes.InvalidSeq:\n case GatewayCloseEventCodes.RateLimited:\n case GatewayCloseEventCodes.SessionTimedOut: {\n logger.debug(`[Shard] Gateway connection closing requiring re-identify. Code: ${close.code}`)\n this.state = ShardState.Identifying\n this.events.disconnected?.(this)\n\n return await this.identify()\n }\n // When these codes are received something went really wrong.\n // On those we cannot start a reconnect attempt.\n case GatewayCloseEventCodes.AuthenticationFailed:\n case GatewayCloseEventCodes.InvalidShard:\n case GatewayCloseEventCodes.ShardingRequired:\n case GatewayCloseEventCodes.InvalidApiVersion:\n case GatewayCloseEventCodes.InvalidIntents:\n case GatewayCloseEventCodes.DisallowedIntents: {\n this.state = ShardState.Offline\n this.events.disconnected?.(this)\n\n throw new Error(close.reason || 'Discord gave no reason! GG! You broke Discord!')\n }\n // Gateway connection closes on which a resume is allowed.\n case GatewayCloseEventCodes.UnknownError:\n case GatewayCloseEventCodes.DecodeError:\n case GatewayCloseEventCodes.AlreadyAuthenticated:\n default: {\n this.state = ShardState.Resuming\n this.events.disconnected?.(this)\n\n return await this.resume()\n }\n }\n }\n\n /** Handles a incoming gateway packet. */\n async handleDiscordPacket(packet: DiscordGatewayPayload): Promise<void> {\n // Edge case start: https://github.com/discordeno/discordeno/issues/2311\n this.heart.lastAck = Date.now()\n // Manually calculating the round trip time for users who need it.\n if (this.heart.lastBeat && !this.heart.acknowledged) {\n this.heart.rtt = this.heart.lastAck - this.heart.lastBeat\n }\n this.heart.acknowledged = true\n // Edge case end!\n\n switch (packet.op) {\n case GatewayOpcodes.Heartbeat: {\n // TODO: can this actually happen\n if (!this.isOpen()) return\n\n this.heart.lastBeat = Date.now()\n // Discord randomly sends this requiring an immediate heartbeat back.\n // Using a direct socket.send call here because heartbeat requests are reserved by us.\n this.socket?.send(\n JSON.stringify({\n op: GatewayOpcodes.Heartbeat,\n d: this.previousSequenceNumber,\n }),\n )\n this.events.heartbeat?.(this)\n\n break\n }\n case GatewayOpcodes.Hello: {\n const interval = (packet.d as DiscordHello).heartbeat_interval\n\n this.startHeartbeating(interval)\n\n if (this.state !== ShardState.Resuming) {\n const currentQueue = [...this.bucket.queue]\n // HELLO has been send on a non resume action.\n // This means that the shard starts a new session,\n // therefore the rate limit interval has been reset too.\n this.bucket = new LeakyBucket({\n max: this.calculateSafeRequests(),\n refillInterval: 60000,\n refillAmount: this.calculateSafeRequests(),\n })\n\n // Queue should not be lost on a re-identify.\n this.bucket.queue.unshift(...currentQueue)\n }\n\n this.events.hello?.(this)\n\n break\n }\n case GatewayOpcodes.HeartbeatACK: {\n this.events.heartbeatAck?.(this)\n\n break\n }\n case GatewayOpcodes.Reconnect: {\n // gateway.debug(\"GW RECONNECT\", { shardId });\n\n this.events.requestedReconnect?.(this)\n\n await this.resume()\n\n break\n }\n case GatewayOpcodes.InvalidSession: {\n const resumable = packet.d as boolean\n logger.debug(`[Shard] Received Invalid Session for Shard #${this.id} with resumeable as ${resumable.toString()}`)\n\n this.events.invalidSession?.(this, resumable)\n\n // We need to wait for a random amount of time between 1 and 5\n // Reference: https://discord.com/developers/docs/topics/gateway#resuming\n await delay(Math.floor((Math.random() * 4 + 1) * 1000))\n\n this.resolves.get('INVALID_SESSION')?.(packet)\n this.resolves.delete('INVALID_SESSION')\n\n // When resumable is false we need to re-identify\n if (!resumable) {\n await this.requestIdentify()\n\n break\n }\n\n // The session is invalid but apparently it is resumable\n await this.resume()\n\n break\n }\n }\n\n if (packet.t === 'RESUMED') {\n // gateway.debug(\"GW RESUMED\", { shardId });\n\n this.state = ShardState.Connected\n this.events.resumed?.(this)\n\n // Continue the requests which have been queued since the shard went offline.\n this.offlineSendQueue.map((resolve) => resolve())\n\n this.resolves.get('RESUMED')?.(packet)\n this.resolves.delete('RESUMED')\n } else if (packet.t === 'READY') {\n // Important for future resumes.\n\n const payload = packet.d as DiscordReady\n\n this.resumeGatewayUrl = payload.resume_gateway_url\n\n this.sessionId = payload.session_id\n this.state = ShardState.Connected\n\n // Continue the requests which have been queued since the shard went offline.\n // Important when this is a re-identify\n this.offlineSendQueue.map((resolve) => resolve())\n\n this.resolves.get('READY')?.(packet)\n this.resolves.delete('READY')\n }\n\n // Update the sequence number if it is present\n // `s` can be either `null` or a `number`.\n // In order to prevent update misses when `s` is `0` we check against null.\n if (packet.s !== null) {\n this.previousSequenceNumber = packet.s\n }\n\n // The necessary handling required for the Shards connection has been finished.\n // Now the event can be safely forwarded.\n this.events.message?.(this, camelize(packet))\n }\n\n /** Handle an incoming gateway message. */\n async handleMessage(message: WebSocket.MessageEvent): Promise<void> {\n let preProcessMessage = message.data\n\n // If message compression is enabled,\n // Discord might send zlib compressed payloads.\n if (this.gatewayConfig.compress && preProcessMessage instanceof Blob) {\n preProcessMessage = inflateSync(await preProcessMessage.arrayBuffer()).toString()\n }\n\n // Safeguard incase decompression failed to make a string.\n if (typeof preProcessMessage !== 'string') return\n\n return await this.handleDiscordPacket(JSON.parse(preProcessMessage) as DiscordGatewayPayload)\n }\n\n /**\n * Override in order to make the shards presence.\n * async in case devs create the presence based on eg. database values.\n * Passing the shard's id there to make it easier for the dev to use this function.\n */\n async makePresence(): Promise<BotStatusUpdate | undefined> {\n // eslint-disable-next-line no-useless-return\n return\n }\n\n /** This function communicates with the management process, in order to know whether its free to identify. When this function resolves, this means that the shard is allowed to send an identify payload to discord. */\n async requestIdentify(): Promise<void> {}\n\n /** This function communicates with the management process, in order to tell it can identify the next shard. */\n async shardIsReady(): Promise<void> {}\n\n /** Start sending heartbeat payloads to Discord in the provided interval. */\n startHeartbeating(interval: number): void {\n // If old heartbeast exist like after resume, clear the old ones.\n if (this.heart.intervalId) clearInterval(this.heart.intervalId)\n if (this.heart.timeoutId) clearTimeout(this.heart.timeoutId)\n\n this.heart.interval = interval\n\n // Only set the shard's state to `Unidentified`\n // if heartbeating has not been started due to an identify or resume action.\n if ([ShardState.Disconnected, ShardState.Offline].includes(this.state)) {\n this.state = ShardState.Unidentified\n }\n\n // The first heartbeat needs to be send with a random delay between `0` and `interval`\n // Using a `setTimeout(_, jitter)` here to accomplish that.\n // `Math.random()` can be `0` so we use `0.5` if this happens\n // Reference: https://discord.com/developers/docs/topics/gateway#heartbeating\n const jitter = Math.ceil(this.heart.interval * (Math.random() || 0.5))\n this.heart.timeoutId = setTimeout(() => {\n if (!this.isOpen()) return\n\n // Using a direct socket.send call here because heartbeat requests are reserved by us.\n this.socket?.send(\n JSON.stringify({\n op: GatewayOpcodes.Heartbeat,\n d: this.previousSequenceNumber,\n }),\n )\n\n this.heart.lastBeat = Date.now()\n this.heart.acknowledged = false\n\n // After the random heartbeat jitter we can start a normal interval.\n this.heart.intervalId = setInterval(async () => {\n if (!this.isOpen()) return\n // gateway.debug(\"GW DEBUG\", `Running setInterval in heartbeat file. Shard: ${shardId}`);\n\n // gateway.debug(\"GW HEARTBEATING\", { shardId, shard: currentShard });\n\n // The Shard did not receive a heartbeat ACK from Discord in time,\n // therefore we have to assume that the connection has failed or got \"zombied\".\n // The Shard needs to start a re-identify action accordingly.\n // Reference: https://discord.com/developers/docs/topics/gateway#heartbeating-example-gateway-heartbeat-ack\n if (!this.heart.acknowledged) {\n logger.debug(`[Shard] Heartbeat not acknowledged for shard #${this.id}.`)\n this.close(ShardSocketCloseCodes.ZombiedConnection, 'Zombied connection, did not receive an heartbeat ACK in time.')\n\n return await this.identify()\n }\n\n this.heart.acknowledged = false\n\n // Using a direct socket.send call here because heartbeat requests are reserved by us.\n this.socket?.send(\n JSON.stringify({\n op: GatewayOpcodes.Heartbeat,\n d: this.previousSequenceNumber,\n }),\n )\n\n this.heart.lastBeat = Date.now()\n\n this.events.heartbeat?.(this)\n }, this.heart.interval)\n }, jitter)\n }\n\n /** Stop the heartbeating process with discord. */\n stopHeartbeating(): void {\n // Clear the regular heartbeat interval.\n clearInterval(this.heart.intervalId)\n // It's possible that the Shard got closed before the first jittered heartbeat.\n // To go safe we should clear the related timeout too.\n clearTimeout(this.heart.timeoutId)\n }\n\n /**\n * Connects the bot user to a voice or stage channel.\n *\n * This function sends the _Update Voice State_ gateway command over the gateway behind the scenes.\n *\n * @param guildId - The ID of the guild the voice channel to leave is in.\n * @param channelId - The ID of the channel you want to join.\n *\n * @remarks\n * Requires the `CONNECT` permission.\n *\n * Fires a _Voice State Update_ gateway event.\n *\n * @see {@link https://discord.com/developers/docs/topics/gateway#update-voice-state}\n */\n async joinVoiceChannel(\n guildId: BigString,\n channelId: BigString,\n options?: AtLeastOne<Omit<UpdateVoiceState, 'guildId' | 'channelId'>>,\n ): Promise<void> {\n logger.debug(`[Shard] joinVoiceChannel guildId: ${guildId} channelId: ${channelId}`)\n return await this.send({\n op: GatewayOpcodes.VoiceStateUpdate,\n d: {\n guild_id: guildId.toString(),\n channel_id: channelId.toString(),\n self_mute: Boolean(options?.selfMute),\n self_deaf: options?.selfDeaf ?? true,\n },\n })\n }\n\n /**\n * Edits the bot status in all shards that this gateway manages.\n *\n * @param data The status data to set the bots status to.\n * @returns Promise<void>\n */\n async editBotStatus(data: StatusUpdate): Promise<void> {\n logger.debug(`[Shard] editBotStatus data: ${JSON.stringify(data)}`)\n return await this.editShardStatus(data)\n }\n\n /**\n * Edits the bot's status on one shard.\n *\n * @param shardId The shard id to edit the status for.\n * @param data The status data to set the bots status to.\n * @returns Promise<void>\n */\n async editShardStatus(data: StatusUpdate): Promise<void> {\n logger.debug(`[Shard] editShardStatus shardId: ${this.id} -> data: ${JSON.stringify(data)}`)\n return await this.send({\n op: GatewayOpcodes.PresenceUpdate,\n d: {\n since: null,\n afk: false,\n activities: data.activities,\n status: data.status,\n },\n })\n }\n\n /**\n * Fetches the list of members for a guild over the gateway.\n *\n * @param guildId - The ID of the guild to get the list of members for.\n * @param options - The parameters for the fetching of the members.\n *\n * @remarks\n * If requesting the entire member list:\n * - Requires the `GUILD_MEMBERS` intent.\n *\n * If requesting presences ({@link RequestGuildMembers.presences | presences} set to `true`):\n * - Requires the `GUILD_PRESENCES` intent.\n *\n * If requesting a prefix ({@link RequestGuildMembers.query | query} non-`undefined`):\n * - Returns a maximum of 100 members.\n *\n * If requesting a users by ID ({@link RequestGuildMembers.userIds | userIds} non-`undefined`):\n * - Returns a maximum of 100 members.\n *\n * Fires a _Guild Members Chunk_ gateway event for every 1000 members fetched.\n *\n * @see {@link https://discord.com/developers/docs/topics/gateway#request-guild-members}\n */\n async requestMembers(guildId: BigString, options?: Omit<RequestGuildMembers, 'guildId'>): Promise<Camelize<DiscordMember[]>> {\n // You can request 1 member without the intent\n // Check if intents is not 0 as proxy ws won't set intents in other instances\n if (this.connection.intents && (!options?.limit || options.limit > 1) && !(this.connection.intents & GatewayIntents.GuildMembers)) {\n throw new Error('MISSING_INTENT_GUILD_MEMBERS')\n }\n\n if (options?.userIds?.length) {\n logger.debug(`[Shard] requestMembers guildId: ${guildId} -> setting user limit based on userIds length: ${options.userIds.length}`)\n options.limit = options.userIds.length\n }\n\n const nonce = `${guildId}-${Date.now()}`\n\n // Gateway does not require caching these requests so directly send and return\n if (!this.cache.requestMembers?.enabled) {\n logger.debug(`[Shard] requestMembers guildId: ${guildId} -> skipping cache -> options ${JSON.stringify(options)}`)\n await this.send({\n op: GatewayOpcodes.RequestGuildMembers,\n d: {\n guild_id: guildId.toString(),\n // If a query is provided use it, OR if a limit is NOT provided use \"\"\n query: options?.query ?? (options?.limit ? undefined : ''),\n limit: options?.limit ?? 0,\n presences: options?.presences ?? false,\n user_ids: options?.userIds?.map((id) => id.toString()),\n nonce,\n },\n })\n return []\n }\n\n return await new Promise((resolve) => {\n this.cache.requestMembers?.pending.set(nonce, { nonce, resolve, members: [] })\n\n logger.debug(`[Shard] requestMembers guildId: ${guildId} -> requesting members -> data: ${JSON.stringify(options)}`)\n this.send({\n op: GatewayOpcodes.RequestGuildMembers,\n d: {\n guild_id: guildId.toString(),\n // If a query is provided use it, OR if a limit is NOT provided use \"\"\n query: options?.query ?? (options?.limit ? undefined : ''),\n limit: options?.limit ?? 0,\n presences: options?.presences ?? false,\n user_ids: options?.userIds?.map((id) => id.toString()),\n nonce,\n },\n })\n })\n }\n\n /**\n * Leaves the voice channel the bot user is currently in.\n *\n * This function sends the _Update Voice State_ gateway command over the gateway behind the scenes.\n *\n * @param guildId - The ID of the guild the voice channel to leave is in.\n *\n * @remarks\n * Fires a _Voice State Update_ gateway event.\n *\n * @see {@link https://discord.com/developers/docs/topics/gateway#update-voice-state}\n */\n async leaveVoiceChannel(guildId: BigString): Promise<void> {\n logger.debug(`[Shard] leaveVoiceChannel guildId: ${guildId} Shard ${this.id}`)\n return await this.send({\n op: GatewayOpcodes.VoiceStateUpdate,\n d: {\n guild_id: guildId.toString(),\n channel_id: null,\n self_mute: false,\n self_deaf: false,\n },\n })\n }\n}\n\nexport interface ShardCreateOptions {\n /** The shard id */\n id: number\n /** The connection details */\n connection: ShardGatewayConfig\n /** The event handlers for events on the shard. */\n events: ShardEvents\n /** The handler to request a space to make an identify request. */\n requestIdentify?: () => Promise<void>\n /** The handler to alert the gateway manager that this shard has received a READY event. */\n shardIsReady?: () => Promise<void>\n}\n\nexport default DiscordenoShard\n"],"names":["GatewayCloseEventCodes","GatewayIntents","GatewayOpcodes","Collection","LeakyBucket","camelize","delay","logger","clearInterval","inflateSync","WebSocket","ShardSocketCloseCodes","ShardState","DiscordenoShard","constructor","options","maxRequestsPerRateLimitTick","previousSequenceNumber","rateLimitResetInterval","state","Offline","resumeGatewayUrl","events","offlineSendQueue","resolves","Map","cache","requestMembers","enabled","pending","id","connection","heart","acknowledged","interval","requestIdentify","shardIsReady","bucket","max","calculateSafeRequests","refillAmount","refillInterval","gatewayConfig","connectionUrl","url","safeRequests","Math","ceil","checkOffline","highPriority","isOpen","Promise","resolve","unshift","push","close","code","reason","socket","readyState","OPEN","connect","Identifying","Resuming","includes","Connecting","connecting","URL","searchParams","set","version","toString","onerror","event","console","log","error","shardId","onclose","handleClose","onmessage","message","handleMessage","onopen","Unidentified","connected","identify","debug","ReIdentifying","identifying","send","op","Identify","d","token","compress","properties","intents","shard","totalShards","presence","makePresence","identified","delete","resume","ResumeClosingOldConnection","sessionId","Resume","session_id","seq","acquire","JSON","stringify","shutdown","Shutdown","stopHeartbeating","TestingFinished","disconnected","Resharded","ZombiedConnection","Disconnected","UnknownOpcode","NotAuthenticated","InvalidSeq","RateLimited","SessionTimedOut","AuthenticationFailed","InvalidShard","ShardingRequired","InvalidApiVersion","InvalidIntents","DisallowedIntents","Error","UnknownError","DecodeError","AlreadyAuthenticated","handleDiscordPacket","packet","lastAck","Date","now","lastBeat","rtt","Heartbeat","heartbeat","Hello","heartbeat_interval","startHeartbeating","currentQueue","queue","hello","HeartbeatACK","heartbeatAck","Reconnect","requestedReconnect","InvalidSession","resumable","invalidSession","floor","random","get","t","Connected","resumed","map","payload","resume_gateway_url","s","preProcessMessage","data","Blob","arrayBuffer","parse","intervalId","timeoutId","clearTimeout","jitter","setTimeout","setInterval","joinVoiceChannel","guildId","channelId","VoiceStateUpdate","guild_id","channel_id","self_mute","Boolean","selfMute","self_deaf","selfDeaf","editBotStatus","editShardStatus","PresenceUpdate","since","afk","activities","status","limit","GuildMembers","userIds","length","nonce","RequestGuildMembers","query","undefined","presences","user_ids","members","leaveVoiceChannel"],"mappings":"AAUA,SAASA,sBAAsB,EAAEC,cAAc,EAAEC,cAAc,QAAQ,oBAAmB;AAC1F,SAASC,UAAU,EAAEC,WAAW,EAAEC,QAAQ,EAAEC,KAAK,EAAEC,MAAM,QAAQ,oBAAmB;AACpF,SAASC,aAAa,QAAQ,cAAa;AAC3C,SAASC,WAAW,QAAQ,YAAW;AACvC,OAAOC,eAAe,KAAI;AAG1B,SAASC,qBAAqB,EAAEC,UAAU,QAAQ,aAAY;AAE9D,OAAO,MAAMC;IA2CXC,YAAYC,OAA2B,CAAE;QApCzC,0HAA0H,QAC1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;QAED,OAAO,MAAM,IAAItJ,QAAQ,CAACC,UAAY;YACpC,IAAI,CAAC1B,KAAK,CAACC,cAAc,EAAEE,QAAQwC,GAAG,CAACoI,OAAO;gBAAEA;gBAAOrJ;gBAAS2J,SAAS,EAAE;YAAC;YAE5ExM,OAAOgF,KAAK,CAAC,CAAC,gCAAgC,EAAE6F,QAAQ,gCAAgC,EAAErE,KAAKC,SAAS,CAACjG,SAAS,CAAC;YACnH,IAAI,CAAC2E,IAAI,CAAC;gBACRC,IAAIzF,eAAewM,mBAAmB;gBACtC7G,GAAG;oBACD0F,UAAUH,QAAQ7G,QAAQ;oBAC1B,sEAAsE;oBACtEoI,OAAO5L,SAAS4L,SAAU5L,CAAAA,SAASsL,QAAQO,YAAY,EAAE,AAAD;oBACxDP,OAAOtL,SAASsL,SAAS;oBACzBQ,WAAW9L,SAAS8L,aAAa,KAAK;oBACtCC,UAAU/L,SAASwL,SAASnC,IAAI,CAACtI,KAAOA,GAAGyC,QAAQ;oBACnDkI;gBACF;YACF;QACF;IACF;IAEA;;;;;;;;;;;GAWC,GACD,MAAMO,kBAAkB5B,OAAkB,EAAiB;QACzD7K,OAAOgF,KAAK,CAAC,CAAC,mCAAmC,EAAE6F,QAAQ,OAAO,EAAE,IAAI,CAACtJ,EAAE,CAAC,CAAC;QAC7E,OAAO,MAAM,IAAI,CAAC4D,IAAI,CAAC;YACrBC,IAAIzF,eAAeoL,gBAAgB;YACnCzF,GAAG;gBACD0F,UAAUH,QAAQ7G,QAAQ;gBAC1BiH,YAAY,IAAI;gBAChBC,WAAW,KAAK;gBAChBG,WAAW,KAAK;YAClB;QACF;IACF;AACF,CAAC;AAeD,eAAe/K,gBAAe"}
|
|
1
|
+
{"version":3,"sources":["../src/Shard.ts"],"sourcesContent":["/* eslint-disable @typescript-eslint/no-confusing-void-expression */\nimport type { DiscordGatewayPayload, DiscordHello, DiscordReady } from '@discordeno/types'\nimport { GatewayCloseEventCodes, GatewayOpcodes } from '@discordeno/types'\nimport { camelize, delay, LeakyBucket, logger } from '@discordeno/utils'\nimport { inflateSync } from 'node:zlib'\nimport NodeWebSocket from 'ws'\nimport type { BotStatusUpdate, ShardEvents, ShardGatewayConfig, ShardHeart, ShardSocketRequest } from './types.js'\nimport { ShardSocketCloseCodes, ShardState } from './types.js'\n\ndeclare let WebSocket: any\n\nexport class DiscordenoShard {\n /** The id of the shard */\n id: number\n /** The connection config details that this shard will used to connect to discord. */\n connection: ShardGatewayConfig\n /** This contains all the heartbeat information */\n heart: ShardHeart\n /** The maximum of requests which can be send to discord per rate limit tick. Typically this value should not be changed. */\n maxRequestsPerRateLimitTick: number = 120\n /** The previous payload sequence number. */\n previousSequenceNumber: number | null = null\n /** In which interval (in milliseconds) the gateway resets it's rate limit. */\n rateLimitResetInterval: number = 60000\n /** Current session id of the shard if present. */\n sessionId?: string\n /** This contains the WebSocket connection to Discord, if currently connected. */\n socket?: NodeWebSocket\n /** Current internal state of the this. */\n state = ShardState.Offline\n /** The url provided by discord to use when resuming a connection for this this. */\n resumeGatewayUrl: string = ''\n /** The shard related event handlers. */\n events: ShardEvents = {}\n /** Cache for pending gateway requests which should have been send while the gateway went offline. */\n offlineSendQueue: Array<(_?: unknown) => void> = []\n /** Resolve internal waiting states. Mapped by SelectedEvents => ResolveFunction */\n resolves = new Map<'READY' | 'RESUMED' | 'INVALID_SESSION', (payload: DiscordGatewayPayload) => void>()\n /** Shard bucket. Only access this if you know what you are doing. Bucket for handling shard request rate limits. */\n bucket: LeakyBucket\n\n constructor(options: ShardCreateOptions) {\n this.id = options.id\n this.connection = options.connection\n this.events = options.events\n\n this.heart = {\n acknowledged: false,\n interval: 45000,\n }\n\n if (options.requestIdentify) this.requestIdentify = options.requestIdentify\n if (options.shardIsReady) this.shardIsReady = options.shardIsReady\n\n this.bucket = new LeakyBucket({\n max: this.calculateSafeRequests(),\n refillAmount: this.calculateSafeRequests(),\n refillInterval: 60000,\n })\n }\n\n /** The gateway configuration which is used to connect to Discord. */\n get gatewayConfig(): ShardGatewayConfig {\n return this.connection\n }\n\n /** The url to connect to. Initially this is the discord gateway url, and then is switched to resume gateway url once a READY is received. */\n get connectionUrl(): string {\n // Use || and not ?? here. ?? will cause a bug.\n return this.resumeGatewayUrl || this.gatewayConfig.url\n }\n\n /** Calculate the amount of requests which can safely be made per rate limit interval, before the gateway gets disconnected due to an exceeded rate limit. */\n calculateSafeRequests(): number {\n // * 2 adds extra safety layer for discords OP 1 requests that we need to respond to\n const safeRequests = this.maxRequestsPerRateLimitTick - Math.ceil(this.rateLimitResetInterval / this.heart.interval) * 2\n\n return safeRequests < 0 ? 0 : safeRequests\n }\n\n async checkOffline(highPriority: boolean): Promise<void> {\n if (!this.isOpen()) {\n await new Promise((resolve) => {\n // Higher priority requests get added at the beginning of the array.\n if (highPriority) this.offlineSendQueue.unshift(resolve)\n else this.offlineSendQueue.push(resolve)\n })\n }\n }\n\n /** Close the socket connection to discord if present. */\n close(code: number, reason: string): void {\n if (this.socket?.readyState !== NodeWebSocket.OPEN) return\n\n this.socket?.close(code, reason)\n }\n\n /** Connect the shard with the gateway and start heartbeating. This will not identify the shard to the gateway. */\n async connect(): Promise<DiscordenoShard> {\n // Only set the shard to `Connecting` state,\n // if the connection request does not come from an identify or resume action.\n if (![ShardState.Identifying, ShardState.Resuming].includes(this.state)) {\n this.state = ShardState.Connecting\n }\n this.events.connecting?.(this)\n\n const url = new URL(this.connectionUrl)\n url.searchParams.set('v', this.gatewayConfig.version.toString())\n url.searchParams.set('encoding', 'json')\n\n const socket: NodeWebSocket =\n // @ts-expect-error Deno\n globalThis.Deno !== undefined && Reflect.has(globalThis, 'Deno') ? new WebSocket(url.toString()) : new NodeWebSocket(url.toString())\n this.socket = socket\n\n // TODO: proper event handling\n socket.onerror = (event: NodeWebSocket.ErrorEvent) => console.log({ error: event, shardId: this.id })\n socket.onclose = async (event: NodeWebSocket.CloseEvent) => await this.handleClose(event)\n socket.onmessage = async (message: NodeWebSocket.MessageEvent) => await this.handleMessage(message)\n\n return await new Promise((resolve) => {\n socket.onopen = () => {\n // Only set the shard to `Unidentified` state,\n // if the connection request does not come from an identify or resume action.\n if (![ShardState.Identifying, ShardState.Resuming].includes(this.state)) {\n this.state = ShardState.Unidentified\n }\n this.events.connected?.(this)\n\n resolve(this)\n }\n })\n }\n\n /** Identify the shard to the gateway. If not connected, this will also connect the shard to the gateway. */\n async identify(): Promise<void> {\n // A new identify has been requested even though there is already a connection open.\n // Therefore we need to close the old connection and heartbeating before creating a new one.\n if (this.isOpen()) {\n logger.debug(`CLOSING EXISTING SHARD: #${this.id}`)\n this.close(ShardSocketCloseCodes.ReIdentifying, 'Re-identifying closure of old connection.')\n }\n\n this.state = ShardState.Identifying\n this.events.identifying?.(this)\n\n // It is possible that the shard is in Heartbeating state but not identified,\n // so check whether there is already a gateway connection existing.\n // If not we need to create one before we identify.\n if (!this.isOpen()) {\n await this.connect()\n }\n\n this.send(\n {\n op: GatewayOpcodes.Identify,\n d: {\n token: `Bot ${this.gatewayConfig.token}`,\n compress: this.gatewayConfig.compress,\n properties: this.gatewayConfig.properties,\n intents: this.gatewayConfig.intents,\n shard: [this.id, this.gatewayConfig.totalShards],\n presence: await this.makePresence?.(),\n },\n },\n true,\n )\n\n return await new Promise((resolve) => {\n this.resolves.set('READY', () => {\n this.events.identified?.(this)\n // Tells the manager that this shard is ready\n this.shardIsReady()\n resolve()\n })\n // When identifying too fast,\n // Discord sends an invalid session payload.\n // This can safely be ignored though and the shard starts a new identify action.\n this.resolves.set('INVALID_SESSION', () => {\n this.resolves.delete('READY')\n resolve()\n })\n })\n }\n\n /** Check whether the connection to Discord is currently open. */\n isOpen(): boolean {\n return this.socket?.readyState === NodeWebSocket.OPEN\n }\n\n /** Attempt to resume the previous shards session with the gateway. */\n async resume(): Promise<void> {\n logger.debug(`[Gateway] Resuming Shard #${this.id}`)\n // It has been requested to resume the Shards session.\n // It's possible that the shard is still connected with Discord's gateway therefore we need to forcefully close it.\n if (this.isOpen()) {\n logger.debug(`[Gateway] Resuming Shard #${this.id} in isOpen`)\n this.close(ShardSocketCloseCodes.ResumeClosingOldConnection, 'Reconnecting the shard, closing old connection.')\n }\n\n // Shard has never identified, so we cannot resume.\n if (!this.sessionId) {\n logger.debug(`[Shard] Trying to resume a shard #${this.id} that was NOT first identified. (No session id found)`)\n\n return await this.identify()\n }\n\n this.state = ShardState.Resuming\n\n logger.debug(`[Gateway] Resuming Shard #${this.id}, before connecting`)\n // Before we can resume, we need to create a new connection with Discord's gateway.\n await this.connect()\n logger.debug(\n // eslint-disable-next-line @typescript-eslint/restrict-template-expressions\n `[Gateway] Resuming Shard #${this.id}, after connecting. ${this.gatewayConfig.token} | ${this.sessionId} | ${this.previousSequenceNumber}`,\n )\n\n this.send(\n {\n op: GatewayOpcodes.Resume,\n d: {\n token: `Bot ${this.gatewayConfig.token}`,\n session_id: this.sessionId,\n seq: this.previousSequenceNumber ?? 0,\n },\n },\n true,\n )\n logger.debug(`[Gateway] Resuming Shard #${this.id} after send resumg`)\n\n return await new Promise((resolve) => {\n this.resolves.set('RESUMED', () => resolve())\n // If it is attempted to resume with an invalid session id,\n // Discord sends an invalid session payload\n // Not erroring here since it is easy that this happens, also it would be not catchable\n this.resolves.set('INVALID_SESSION', () => {\n this.resolves.delete('RESUMED')\n resolve()\n })\n })\n }\n\n /** Send a message to Discord.\n * @param {boolean} [highPriority=false] - Whether this message should be send asap.\n */\n async send(message: ShardSocketRequest, highPriority: boolean = false): Promise<void> {\n // Before acquiring a token from the bucket, check whether the shard is currently offline or not.\n // Else bucket and token wait time just get wasted.\n await this.checkOffline(highPriority)\n\n await this.bucket.acquire(highPriority)\n\n // It's possible, that the shard went offline after a token has been acquired from the bucket.\n await this.checkOffline(highPriority)\n\n this.socket?.send(JSON.stringify(message))\n }\n\n /** Shutdown the this. Forcefully disconnect the shard from Discord. The shard may not attempt to reconnect with Discord. */\n async shutdown(): Promise<void> {\n this.close(ShardSocketCloseCodes.Shutdown, 'Shard shutting down.')\n this.state = ShardState.Offline\n }\n\n /** Handle a gateway connection close. */\n async handleClose(close: NodeWebSocket.CloseEvent): Promise<void> {\n // gateway.debug(\"GW CLOSED\", { shardId, payload: event });\n\n this.stopHeartbeating()\n\n switch (close.code) {\n case ShardSocketCloseCodes.TestingFinished: {\n this.state = ShardState.Offline\n this.events.disconnected?.(this)\n\n return\n }\n // On these codes a manual start will be done.\n case ShardSocketCloseCodes.Shutdown:\n case ShardSocketCloseCodes.ReIdentifying:\n case ShardSocketCloseCodes.Resharded:\n case ShardSocketCloseCodes.ResumeClosingOldConnection:\n case ShardSocketCloseCodes.ZombiedConnection: {\n this.state = ShardState.Disconnected\n this.events.disconnected?.(this)\n\n // gateway.debug(\"GW CLOSED_RECONNECT\", { shardId, payload: event });\n return\n }\n // Gateway connection closes which require a new identify.\n case GatewayCloseEventCodes.UnknownOpcode:\n case GatewayCloseEventCodes.NotAuthenticated:\n case GatewayCloseEventCodes.InvalidSeq:\n case GatewayCloseEventCodes.RateLimited:\n case GatewayCloseEventCodes.SessionTimedOut: {\n logger.debug(`[Shard] Gateway connection closing requiring re-identify. Code: ${close.code}`)\n this.state = ShardState.Identifying\n this.events.disconnected?.(this)\n\n return await this.identify()\n }\n // When these codes are received something went really wrong.\n // On those we cannot start a reconnect attempt.\n case GatewayCloseEventCodes.AuthenticationFailed:\n case GatewayCloseEventCodes.InvalidShard:\n case GatewayCloseEventCodes.ShardingRequired:\n case GatewayCloseEventCodes.InvalidApiVersion:\n case GatewayCloseEventCodes.InvalidIntents:\n case GatewayCloseEventCodes.DisallowedIntents: {\n this.state = ShardState.Offline\n this.events.disconnected?.(this)\n\n throw new Error(close.reason || 'Discord gave no reason! GG! You broke Discord!')\n }\n // Gateway connection closes on which a resume is allowed.\n case GatewayCloseEventCodes.UnknownError:\n case GatewayCloseEventCodes.DecodeError:\n case GatewayCloseEventCodes.AlreadyAuthenticated:\n default: {\n logger.info(`[Shard] closed shard #${this.id}. Resuming...`)\n this.state = ShardState.Resuming\n this.events.disconnected?.(this)\n\n return await this.resume()\n }\n }\n }\n\n /** Handles a incoming gateway packet. */\n async handleDiscordPacket(packet: DiscordGatewayPayload): Promise<void> {\n // Edge case start: https://github.com/discordeno/discordeno/issues/2311\n this.heart.lastAck = Date.now()\n // Manually calculating the round trip time for users who need it.\n if (this.heart.lastBeat && !this.heart.acknowledged) {\n this.heart.rtt = this.heart.lastAck - this.heart.lastBeat\n }\n this.heart.acknowledged = true\n // Edge case end!\n\n switch (packet.op) {\n case GatewayOpcodes.Heartbeat: {\n // TODO: can this actually happen\n if (!this.isOpen()) return\n\n this.heart.lastBeat = Date.now()\n // Discord randomly sends this requiring an immediate heartbeat back.\n // Using a direct socket.send call here because heartbeat requests are reserved by us.\n this.socket?.send(\n JSON.stringify({\n op: GatewayOpcodes.Heartbeat,\n d: this.previousSequenceNumber,\n }),\n )\n this.events.heartbeat?.(this)\n\n break\n }\n case GatewayOpcodes.Hello: {\n const interval = (packet.d as DiscordHello).heartbeat_interval\n logger.debug(`[Gateway] Hello on Shard #${this.id}`)\n this.startHeartbeating(interval)\n\n if (this.state !== ShardState.Resuming) {\n const currentQueue = [...this.bucket.queue]\n // HELLO has been send on a non resume action.\n // This means that the shard starts a new session,\n // therefore the rate limit interval has been reset too.\n this.bucket = new LeakyBucket({\n max: this.calculateSafeRequests(),\n refillInterval: 60000,\n refillAmount: this.calculateSafeRequests(),\n })\n\n // Queue should not be lost on a re-identify.\n this.bucket.queue.unshift(...currentQueue)\n }\n\n this.events.hello?.(this)\n\n break\n }\n case GatewayOpcodes.HeartbeatACK: {\n this.events.heartbeatAck?.(this)\n\n break\n }\n case GatewayOpcodes.Reconnect: {\n // gateway.debug(\"GW RECONNECT\", { shardId });\n\n this.events.requestedReconnect?.(this)\n\n await this.resume()\n\n break\n }\n case GatewayOpcodes.InvalidSession: {\n const resumable = packet.d as boolean\n logger.debug(`[Shard] Received Invalid Session for Shard #${this.id} with resumeable as ${resumable.toString()}`)\n\n this.events.invalidSession?.(this, resumable)\n\n // We need to wait for a random amount of time between 1 and 5\n // Reference: https://discord.com/developers/docs/topics/gateway#resuming\n await delay(Math.floor((Math.random() * 4 + 1) * 1000))\n\n this.resolves.get('INVALID_SESSION')?.(packet)\n this.resolves.delete('INVALID_SESSION')\n\n // When resumable is false we need to re-identify\n if (!resumable) {\n await this.requestIdentify()\n\n break\n }\n\n // The session is invalid but apparently it is resumable\n await this.resume()\n\n break\n }\n }\n\n switch (packet.t) {\n case 'RESUMED':\n this.state = ShardState.Connected\n this.events.resumed?.(this)\n\n // Continue the requests which have been queued since the shard went offline.\n this.offlineSendQueue.map((resolve) => resolve())\n\n this.resolves.get('RESUMED')?.(packet)\n this.resolves.delete('RESUMED')\n break\n case 'READY': {\n // Important for future resumes.\n const payload = packet.d as DiscordReady\n\n this.resumeGatewayUrl = payload.resume_gateway_url\n\n this.sessionId = payload.session_id\n this.state = ShardState.Connected\n\n // Continue the requests which have been queued since the shard went offline.\n // Important when this is a re-identify\n this.offlineSendQueue.map((resolve) => resolve())\n\n this.resolves.get('READY')?.(packet)\n this.resolves.delete('READY')\n break\n }\n }\n\n // Update the sequence number if it is present\n // `s` can be either `null` or a `number`.\n // In order to prevent update misses when `s` is `0` we check against null.\n if (packet.s !== null) {\n this.previousSequenceNumber = packet.s\n }\n\n this.forwardToBot(packet)\n }\n\n forwardToBot(packet: DiscordGatewayPayload): void {\n // The necessary handling required for the Shards connection has been finished.\n // Now the event can be safely forwarded.\n this.events.message?.(this, camelize(packet))\n }\n\n /** Handle an incoming gateway message. */\n async handleMessage(message: NodeWebSocket.MessageEvent): Promise<void> {\n let preProcessMessage = message.data\n\n // If message compression is enabled,\n // Discord might send zlib compressed payloads.\n if (this.gatewayConfig.compress && preProcessMessage instanceof Blob) {\n preProcessMessage = inflateSync(await preProcessMessage.arrayBuffer()).toString()\n }\n\n // Safeguard incase decompression failed to make a string.\n if (typeof preProcessMessage !== 'string') return\n\n return await this.handleDiscordPacket(JSON.parse(preProcessMessage) as DiscordGatewayPayload)\n }\n\n /**\n * Override in order to make the shards presence.\n * async in case devs create the presence based on eg. database values.\n * Passing the shard's id there to make it easier for the dev to use this function.\n */\n async makePresence(): Promise<BotStatusUpdate | undefined> {\n // eslint-disable-next-line no-useless-return\n return\n }\n\n /** This function communicates with the management process, in order to know whether its free to identify. When this function resolves, this means that the shard is allowed to send an identify payload to discord. */\n async requestIdentify(): Promise<void> {}\n\n /** This function communicates with the management process, in order to tell it can identify the next shard. */\n async shardIsReady(): Promise<void> {}\n\n /** Start sending heartbeat payloads to Discord in the provided interval. */\n startHeartbeating(interval: number): void {\n logger.debug(`[Gateway] Start Heartbeating Shard #${this.id}`)\n // If old heartbeast exist like after resume, clear the old ones.\n if (this.heart.intervalId) clearInterval(this.heart.intervalId)\n if (this.heart.timeoutId) clearTimeout(this.heart.timeoutId)\n\n this.heart.interval = interval\n\n // Only set the shard's state to `Unidentified`\n // if heartbeating has not been started due to an identify or resume action.\n if ([ShardState.Disconnected, ShardState.Offline].includes(this.state)) {\n logger.debug(`[Gateway] Start Heartbeating Shard #${this.id} a`)\n this.state = ShardState.Unidentified\n }\n\n // The first heartbeat needs to be send with a random delay between `0` and `interval`\n // Using a `setTimeout(_, jitter)` here to accomplish that.\n // `Math.random()` can be `0` so we use `0.5` if this happens\n // Reference: https://discord.com/developers/docs/topics/gateway#heartbeating\n const jitter = Math.ceil(this.heart.interval * (Math.random() || 0.5))\n this.heart.timeoutId = setTimeout(() => {\n logger.debug(`[Gateway] Start Heartbeating Shard #${this.id} b`)\n if (!this.isOpen()) return\n logger.debug(`[Gateway] Start Heartbeating Shard #${this.id} c ${this.previousSequenceNumber!}`)\n\n // Using a direct socket.send call here because heartbeat requests are reserved by us.\n this.socket?.send(\n JSON.stringify({\n op: GatewayOpcodes.Heartbeat,\n d: this.previousSequenceNumber,\n }),\n )\n\n logger.debug(`[Gateway] Start Heartbeating Shard #${this.id} d`)\n this.heart.lastBeat = Date.now()\n this.heart.acknowledged = false\n\n // After the random heartbeat jitter we can start a normal interval.\n this.heart.intervalId = setInterval(async () => {\n logger.debug(`[Gateway] Start Heartbeating Shard #${this.id} e`)\n if (!this.isOpen()) return\n logger.debug(`[Gateway] Start Heartbeating Shard #${this.id} f`)\n // gateway.debug(\"GW DEBUG\", `Running setInterval in heartbeat file. Shard: ${shardId}`);\n\n // gateway.debug(\"GW HEARTBEATING\", { shardId, shard: currentShard });\n\n // The Shard did not receive a heartbeat ACK from Discord in time,\n // therefore we have to assume that the connection has failed or got \"zombied\".\n // The Shard needs to start a re-identify action accordingly.\n // Reference: https://discord.com/developers/docs/topics/gateway#heartbeating-example-gateway-heartbeat-ack\n if (!this.heart.acknowledged) {\n logger.debug(`[Shard] Heartbeat not acknowledged for shard #${this.id}.`)\n this.close(ShardSocketCloseCodes.ZombiedConnection, 'Zombied connection, did not receive an heartbeat ACK in time.')\n\n return await this.identify()\n }\n\n this.heart.acknowledged = false\n\n logger.debug(`[Gateway] Start Heartbeating Shard #${this.id} g`)\n // Using a direct socket.send call here because heartbeat requests are reserved by us.\n this.socket?.send(\n JSON.stringify({\n op: GatewayOpcodes.Heartbeat,\n d: this.previousSequenceNumber,\n }),\n )\n logger.debug(`[Gateway] Start Heartbeating Shard #${this.id} h`)\n\n this.heart.lastBeat = Date.now()\n\n this.events.heartbeat?.(this)\n }, this.heart.interval)\n }, jitter)\n }\n\n /** Stop the heartbeating process with discord. */\n stopHeartbeating(): void {\n // Clear the regular heartbeat interval.\n clearInterval(this.heart.intervalId)\n // It's possible that the Shard got closed before the first jittered heartbeat.\n // To go safe we should clear the related timeout too.\n clearTimeout(this.heart.timeoutId)\n }\n}\n\nexport interface ShardCreateOptions {\n /** The shard id */\n id: number\n /** The connection details */\n connection: ShardGatewayConfig\n /** The event handlers for events on the shard. */\n events: ShardEvents\n /** The handler to request a space to make an identify request. */\n requestIdentify?: () => Promise<void>\n /** The handler to alert the gateway manager that this shard has received a READY event. */\n shardIsReady?: () => Promise<void>\n}\n\nexport default DiscordenoShard\n"],"names":["GatewayCloseEventCodes","GatewayOpcodes","camelize","delay","LeakyBucket","logger","inflateSync","NodeWebSocket","ShardSocketCloseCodes","ShardState","DiscordenoShard","constructor","options","maxRequestsPerRateLimitTick","previousSequenceNumber","rateLimitResetInterval","state","Offline","resumeGatewayUrl","events","offlineSendQueue","resolves","Map","id","connection","heart","acknowledged","interval","requestIdentify","shardIsReady","bucket","max","calculateSafeRequests","refillAmount","refillInterval","gatewayConfig","connectionUrl","url","safeRequests","Math","ceil","checkOffline","highPriority","isOpen","Promise","resolve","unshift","push","close","code","reason","socket","readyState","OPEN","connect","Identifying","Resuming","includes","Connecting","connecting","URL","searchParams","set","version","toString","globalThis","Deno","undefined","Reflect","has","WebSocket","onerror","event","console","log","error","shardId","onclose","handleClose","onmessage","message","handleMessage","onopen","Unidentified","connected","identify","debug","ReIdentifying","identifying","send","op","Identify","d","token","compress","properties","intents","shard","totalShards","presence","makePresence","identified","delete","resume","ResumeClosingOldConnection","sessionId","Resume","session_id","seq","acquire","JSON","stringify","shutdown","Shutdown","stopHeartbeating","TestingFinished","disconnected","Resharded","ZombiedConnection","Disconnected","UnknownOpcode","NotAuthenticated","InvalidSeq","RateLimited","SessionTimedOut","AuthenticationFailed","InvalidShard","ShardingRequired","InvalidApiVersion","InvalidIntents","DisallowedIntents","Error","UnknownError","DecodeError","AlreadyAuthenticated","info","handleDiscordPacket","packet","lastAck","Date","now","lastBeat","rtt","Heartbeat","heartbeat","Hello","heartbeat_interval","startHeartbeating","currentQueue","queue","hello","HeartbeatACK","heartbeatAck","Reconnect","requestedReconnect","InvalidSession","resumable","invalidSession","floor","random","get","t","Connected","resumed","map","payload","resume_gateway_url","s","forwardToBot","preProcessMessage","data","Blob","arrayBuffer","parse","intervalId","clearInterval","timeoutId","clearTimeout","jitter","setTimeout","setInterval"],"mappings":"AAAA,kEAAkE,GAElE,SAASA,sBAAsB,EAAEC,cAAc,QAAQ,oBAAmB;AAC1E,SAASC,QAAQ,EAAEC,KAAK,EAAEC,WAAW,EAAEC,MAAM,QAA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CAACC,YAAY,GAAG,KAAK;gBAE/BrB,OAAOiF,KAAK,CAAC,CAAC,oCAAoC,EAAE,IAAI,CAAC/D,EAAE,CAAC,EAAE,CAAC;gBAC/D,sFAAsF;gBACtF,IAAI,CAAC4B,MAAM,EAAEsC,KACXqB,KAAKC,SAAS,CAAC;oBACbrB,IAAIzF,eAAe8I,SAAS;oBAC5BnD,GAAG,IAAI,CAAC9E,sBAAsB;gBAChC;gBAEFT,OAAOiF,KAAK,CAAC,CAAC,oCAAoC,EAAE,IAAI,CAAC/D,EAAE,CAAC,EAAE,CAAC;gBAE/D,IAAI,CAACE,KAAK,CAACoH,QAAQ,GAAGF,KAAKC,GAAG;gBAE9B,IAAI,CAACzH,MAAM,CAAC6H,SAAS,GAAG,IAAI;YAC9B,GAAG,IAAI,CAACvH,KAAK,CAACE,QAAQ;QACxB,GAAGuJ;IACL;IAEA,gDAAgD,GAChDhE,mBAAyB;QACvB,wCAAwC;QACxC6D,cAAc,IAAI,CAACtJ,KAAK,CAACqJ,UAAU;QACnC,+EAA+E;QAC/E,sDAAsD;QACtDG,aAAa,IAAI,CAACxJ,KAAK,CAACuJ,SAAS;IACnC;AACF,CAAC;AAeD,eAAetK,gBAAe"}
|
package/dist/manager.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import type
|
|
1
|
+
import { type AtLeastOne, type BigString, type Camelize, type DiscordGetGatewayBot, type DiscordMemberWithUser, type RequestGuildMembers } from '@discordeno/types';
|
|
2
2
|
import { Collection } from '@discordeno/utils';
|
|
3
3
|
import Shard from './Shard.js';
|
|
4
|
-
import type { ShardEvents, StatusUpdate, UpdateVoiceState } from './types.js';
|
|
4
|
+
import type { ShardEvents, ShardSocketRequest, StatusUpdate, UpdateVoiceState } from './types.js';
|
|
5
5
|
export declare function createGatewayManager(options: CreateGatewayManagerOptions): GatewayManager;
|
|
6
6
|
export interface CreateGatewayManagerOptions {
|
|
7
7
|
/**
|
|
@@ -19,6 +19,11 @@ export interface CreateGatewayManagerOptions {
|
|
|
19
19
|
* @default 5300
|
|
20
20
|
*/
|
|
21
21
|
spawnShardDelay?: number;
|
|
22
|
+
/**
|
|
23
|
+
* Whether to send the discord packets in snake case form.
|
|
24
|
+
* @default false
|
|
25
|
+
*/
|
|
26
|
+
preferSnakeCase?: boolean;
|
|
22
27
|
/**
|
|
23
28
|
* Total amount of shards your bot uses. Useful for zero-downtime updates or resharding.
|
|
24
29
|
* @default 1
|
|
@@ -113,6 +118,7 @@ export interface GatewayManager extends Required<CreateGatewayManagerOptions> {
|
|
|
113
118
|
spawnShards: () => Promise<void>;
|
|
114
119
|
/** Shutdown all shards. */
|
|
115
120
|
shutdown: (code: number, reason: string) => Promise<void>;
|
|
121
|
+
sendPayload: (shardId: number, payload: ShardSocketRequest) => Promise<void>;
|
|
116
122
|
/** Allows users to hook in and change to communicate to different workers across different servers or anything they like. For example using redis pubsub to talk to other servers. */
|
|
117
123
|
tellWorkerToIdentify: (workerId: number, shardId: number, bucketId: number) => Promise<void>;
|
|
118
124
|
/** Tell the manager to identify a Shard. If this Shard is not already managed this will also add the Shard to the manager. */
|
|
@@ -177,7 +183,7 @@ export interface GatewayManager extends Required<CreateGatewayManagerOptions> {
|
|
|
177
183
|
*
|
|
178
184
|
* @see {@link https://discord.com/developers/docs/topics/gateway#request-guild-members}
|
|
179
185
|
*/
|
|
180
|
-
requestMembers: (guildId: BigString, options?: Omit<RequestGuildMembers, 'guildId'>) => Promise<Camelize<
|
|
186
|
+
requestMembers: (guildId: BigString, options?: Omit<RequestGuildMembers, 'guildId'>) => Promise<Camelize<DiscordMemberWithUser[]>>;
|
|
181
187
|
/**
|
|
182
188
|
* Leaves the voice channel the bot user is currently in.
|
|
183
189
|
*
|
|
@@ -196,8 +202,8 @@ export interface RequestMemberRequest {
|
|
|
196
202
|
/** The unique nonce for this request. */
|
|
197
203
|
nonce: string;
|
|
198
204
|
/** The resolver handler to run when all members arrive. */
|
|
199
|
-
resolve: (value: Camelize<
|
|
205
|
+
resolve: (value: Camelize<DiscordMemberWithUser[]> | PromiseLike<Camelize<DiscordMemberWithUser[]>>) => void;
|
|
200
206
|
/** The members that have already arrived for this request. */
|
|
201
|
-
members:
|
|
207
|
+
members: DiscordMemberWithUser[];
|
|
202
208
|
}
|
|
203
209
|
//# sourceMappingURL=manager.d.ts.map
|
package/dist/manager.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"manager.d.ts","sourceRoot":"","sources":["../src/manager.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"manager.d.ts","sourceRoot":"","sources":["../src/manager.ts"],"names":[],"mappings":"AACA,OAAO,EAGL,KAAK,UAAU,EACf,KAAK,SAAS,EACd,KAAK,QAAQ,EACb,KAAK,oBAAoB,EACzB,KAAK,qBAAqB,EAC1B,KAAK,mBAAmB,EACzB,MAAM,mBAAmB,CAAA;AAC1B,OAAO,EAAE,UAAU,EAAiB,MAAM,mBAAmB,CAAA;AAC7D,OAAO,KAAK,MAAM,YAAY,CAAA;AAC9B,OAAO,KAAK,EAAE,WAAW,EAAE,kBAAkB,EAAE,YAAY,EAAE,gBAAgB,EAAE,MAAM,YAAY,CAAA;AAEjG,wBAAgB,oBAAoB,CAAC,OAAO,EAAE,2BAA2B,GAAG,cAAc,CA2UzF;AAED,MAAM,WAAW,2BAA2B;IAC1C;;;OAGG;IACH,YAAY,CAAC,EAAE,MAAM,CAAA;IACrB;;;OAGG;IACH,WAAW,CAAC,EAAE,MAAM,CAAA;IACpB;;;OAGG;IACH,eAAe,CAAC,EAAE,MAAM,CAAA;IACxB;;;OAGG;IACH,eAAe,CAAC,EAAE,OAAO,CAAA;IACzB;;;OAGG;IACH,WAAW,CAAC,EAAE,MAAM,CAAA;IACpB;;;OAGG;IACH,eAAe,CAAC,EAAE,MAAM,CAAA;IACxB;;;OAGG;IACH,YAAY,CAAC,EAAE,MAAM,CAAA;IACrB,oFAAoF;IACpF,UAAU,CAAC,EAAE,QAAQ,CA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AAC,IAAI,CAAC,CAAA;IACpD;;;;;;OAMG;IACH,eAAe,EAAE,CAAC,OAAO,EAAE,MAAM,EAAE,IAAI,EAAE,YAAY,KAAK,OAAO,CAAC,IAAI,CAAC,CAAA;IACvE;;;;;;;;;;;;;;;;;;;;;;OAsBG;IACH,cAAc,EAAE,CAAC,OAAO,EAAE,SAAS,EAAE,OAAO,CAAC,EAAE,IAAI,CAAC,mBAAmB,EAAE,SAAS,CAAC,KAAK,OAAO,CAAC,QAAQ,CAAC,qBAAqB,EAAE,CAAC,CAAC,CAAA;IAClI;;;;;;;;;;;OAWG;IACH,iBAAiB,EAAE,CAAC,OAAO,EAAE,SAAS,KAAK,OAAO,CAAC,IAAI,CAAC,CAAA;CACzD;AAED,MAAM,WAAW,oBAAoB;IACnC,yCAAyC;IACzC,KAAK,EAAE,MAAM,CAAA;IACb,2DAA2D;IAC3D,OAAO,EAAE,CAAC,KAAK,EAAE,QAAQ,CAAC,qBAAqB,EAAE,CAAC,GAAG,WAAW,CAAC,QAAQ,CAAC,qBAAqB,EAAE,CAAC,CAAC,KAAK,IAAI,CAAA;IAC5G,8DAA8D;IAC9D,OAAO,EAAE,qBAAqB,EAAE,CAAA;CACjC"}
|
package/dist/manager.js
CHANGED
|
@@ -1,18 +1,17 @@
|
|
|
1
|
+
/* eslint-disable @typescript-eslint/no-confusing-void-expression */ import { GatewayIntents, GatewayOpcodes } from '@discordeno/types';
|
|
1
2
|
import { Collection, delay, logger } from '@discordeno/utils';
|
|
2
3
|
import Shard from './Shard.js';
|
|
3
4
|
export function createGatewayManager(options) {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
};
|
|
15
|
-
}
|
|
5
|
+
const connectionOptions = options.connection ?? {
|
|
6
|
+
url: 'wss://gateway.discord.gg',
|
|
7
|
+
shards: 1,
|
|
8
|
+
sessionStartLimit: {
|
|
9
|
+
maxConcurrency: 1,
|
|
10
|
+
remaining: 1000,
|
|
11
|
+
total: 1000,
|
|
12
|
+
resetAfter: 1000 * 60 * 60 * 24
|
|
13
|
+
}
|
|
14
|
+
};
|
|
16
15
|
const gateway = {
|
|
17
16
|
events: options.events,
|
|
18
17
|
compress: options.compress ?? false,
|
|
@@ -23,15 +22,16 @@ export function createGatewayManager(options) {
|
|
|
23
22
|
device: options.properties?.device ?? 'Discordeno'
|
|
24
23
|
},
|
|
25
24
|
token: options.token,
|
|
26
|
-
url: options.url ??
|
|
25
|
+
url: options.url ?? connectionOptions.url ?? 'wss://gateway.discord.gg',
|
|
27
26
|
version: options.version ?? 10,
|
|
28
|
-
connection:
|
|
29
|
-
totalShards: options.totalShards ??
|
|
30
|
-
lastShardId: options.lastShardId ?? 0,
|
|
27
|
+
connection: connectionOptions,
|
|
28
|
+
totalShards: options.totalShards ?? connectionOptions.shards ?? 1,
|
|
29
|
+
lastShardId: options.lastShardId ?? (options.totalShards ? options.totalShards - 1 : connectionOptions ? connectionOptions.shards - 1 : 0),
|
|
31
30
|
firstShardId: options.firstShardId ?? 0,
|
|
32
31
|
totalWorkers: options.totalWorkers ?? 4,
|
|
33
32
|
shardsPerWorker: options.shardsPerWorker ?? 25,
|
|
34
33
|
spawnShardDelay: options.spawnShardDelay ?? 5300,
|
|
34
|
+
preferSnakeCase: options.preferSnakeCase ?? false,
|
|
35
35
|
shards: new Map(),
|
|
36
36
|
buckets: new Map(),
|
|
37
37
|
cache: {
|
|
@@ -52,13 +52,7 @@ export function createGatewayManager(options) {
|
|
|
52
52
|
(gateway.connection.sessionStartLimit.maxConcurrency === 1 ? 16 : gateway.connection.sessionStartLimit.maxConcurrency)) * gateway.connection.sessionStartLimit.maxConcurrency;
|
|
53
53
|
},
|
|
54
54
|
calculateWorkerId (shardId) {
|
|
55
|
-
|
|
56
|
-
let workerId = Math.floor(shardId / gateway.shardsPerWorker);
|
|
57
|
-
// If the workerId overflows the maximal allowed workers we by default just use to last worker.
|
|
58
|
-
if (workerId >= gateway.totalWorkers) {
|
|
59
|
-
// The Id of the last available worker is total -1
|
|
60
|
-
workerId = gateway.totalWorkers - 1;
|
|
61
|
-
}
|
|
55
|
+
const workerId = Math.min(shardId % gateway.shardsPerWorker, gateway.totalWorkers - 1);
|
|
62
56
|
logger.debug(`[Gateway] Calculating workerId: Shard: ${shardId} -> Worker: ${workerId} -> Per Worker: ${gateway.shardsPerWorker} -> Total: ${gateway.totalWorkers}`);
|
|
63
57
|
return workerId;
|
|
64
58
|
},
|
|
@@ -122,9 +116,16 @@ export function createGatewayManager(options) {
|
|
|
122
116
|
gateway.shards.forEach((shard)=>shard.close(code, reason));
|
|
123
117
|
await delay(5000);
|
|
124
118
|
},
|
|
119
|
+
async sendPayload (shardId, payload) {
|
|
120
|
+
const shard = gateway.shards.get(shardId);
|
|
121
|
+
if (!shard) {
|
|
122
|
+
throw new Error(`Shard (id: ${shardId} not found`);
|
|
123
|
+
}
|
|
124
|
+
await shard.send(payload);
|
|
125
|
+
},
|
|
125
126
|
async tellWorkerToIdentify (workerId, shardId, bucketId) {
|
|
126
127
|
logger.debug(`[Gateway] tell worker to identify (${workerId}, ${shardId}, ${bucketId})`);
|
|
127
|
-
|
|
128
|
+
await gateway.identify(shardId);
|
|
128
129
|
},
|
|
129
130
|
async identify (shardId) {
|
|
130
131
|
let shard = this.shards.get(shardId);
|
|
@@ -143,7 +144,7 @@ export function createGatewayManager(options) {
|
|
|
143
144
|
},
|
|
144
145
|
events: options.events,
|
|
145
146
|
requestIdentify: async ()=>{
|
|
146
|
-
|
|
147
|
+
await gateway.identify(shardId);
|
|
147
148
|
},
|
|
148
149
|
shardIsReady: async ()=>{
|
|
149
150
|
logger.debug(`[Shard] Shard #${shardId} is ready`);
|
|
@@ -152,6 +153,11 @@ export function createGatewayManager(options) {
|
|
|
152
153
|
gateway.buckets.get(shardId % gateway.connection.sessionStartLimit.maxConcurrency).identifyRequests.shift()?.();
|
|
153
154
|
}
|
|
154
155
|
});
|
|
156
|
+
if (this.preferSnakeCase) {
|
|
157
|
+
shard.forwardToBot = async (payload)=>{
|
|
158
|
+
options.events.message?.(shard, payload);
|
|
159
|
+
};
|
|
160
|
+
}
|
|
155
161
|
this.shards.set(shardId, shard);
|
|
156
162
|
}
|
|
157
163
|
const bucket = gateway.buckets.get(shardId % gateway.connection.sessionStartLimit.maxConcurrency);
|
|
@@ -171,7 +177,7 @@ export function createGatewayManager(options) {
|
|
|
171
177
|
}
|
|
172
178
|
logger.debug(`[Gateway] kill shard (${shardId})`);
|
|
173
179
|
this.shards.delete(shardId);
|
|
174
|
-
|
|
180
|
+
await shard.shutdown();
|
|
175
181
|
},
|
|
176
182
|
async requestIdentify (shardId) {
|
|
177
183
|
logger.debug(`[Gateway] requesting identify`);
|
|
@@ -195,12 +201,16 @@ export function createGatewayManager(options) {
|
|
|
195
201
|
},
|
|
196
202
|
async joinVoiceChannel (guildId, channelId, options) {
|
|
197
203
|
const shardId = gateway.calculateShardId(guildId);
|
|
198
|
-
const shard = gateway.shards.get(shardId);
|
|
199
|
-
if (!shard) {
|
|
200
|
-
throw new Error(`Shard (id: ${shardId} not found`);
|
|
201
|
-
}
|
|
202
204
|
logger.debug(`[Gateway] joinVoiceChannel guildId: ${guildId} channelId: ${channelId}`);
|
|
203
|
-
|
|
205
|
+
await gateway.sendPayload(shardId, {
|
|
206
|
+
op: GatewayOpcodes.VoiceStateUpdate,
|
|
207
|
+
d: {
|
|
208
|
+
guild_id: guildId.toString(),
|
|
209
|
+
channel_id: channelId.toString(),
|
|
210
|
+
self_mute: options?.selfMute ?? false,
|
|
211
|
+
self_deaf: options?.selfDeaf ?? true
|
|
212
|
+
}
|
|
213
|
+
});
|
|
204
214
|
},
|
|
205
215
|
async editBotStatus (data) {
|
|
206
216
|
logger.debug(`[Gateway] editBotStatus data: ${JSON.stringify(data)}`);
|
|
@@ -211,30 +221,63 @@ export function createGatewayManager(options) {
|
|
|
211
221
|
}));
|
|
212
222
|
},
|
|
213
223
|
async editShardStatus (shardId, data) {
|
|
214
|
-
const shard = gateway.shards.get(shardId);
|
|
215
|
-
if (!shard) {
|
|
216
|
-
throw new Error(`Shard (id: ${shardId}) not found.`);
|
|
217
|
-
}
|
|
218
224
|
logger.debug(`[Gateway] editShardStatus shardId: ${shardId} -> data: ${JSON.stringify(data)}`);
|
|
219
|
-
|
|
225
|
+
await gateway.sendPayload(shardId, {
|
|
226
|
+
op: GatewayOpcodes.PresenceUpdate,
|
|
227
|
+
d: {
|
|
228
|
+
since: null,
|
|
229
|
+
afk: false,
|
|
230
|
+
activities: data.activities,
|
|
231
|
+
status: data.status
|
|
232
|
+
}
|
|
233
|
+
});
|
|
220
234
|
},
|
|
221
235
|
async requestMembers (guildId, options) {
|
|
222
236
|
const shardId = gateway.calculateShardId(guildId);
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
237
|
+
if (gateway.intents && (!options?.limit || options.limit > 1) && !(gateway.intents & GatewayIntents.GuildMembers)) throw new Error('Cannot fetch more then 1 member without the GUILD_MEMBERS intent');
|
|
238
|
+
logger.debug(`[Gateway] requestMembers guildId: ${guildId} -> data: ${JSON.stringify(options)}`);
|
|
239
|
+
if (options?.userIds?.length) {
|
|
240
|
+
logger.debug(`[Gateway] requestMembers guildId: ${guildId} -> setting user limit based on userIds length: ${options.userIds.length}`);
|
|
241
|
+
options.limit = options.userIds.length;
|
|
226
242
|
}
|
|
227
|
-
|
|
228
|
-
|
|
243
|
+
const members = !gateway.cache.requestMembers?.enabled || !options?.nonce ? [] : new Promise((resolve, reject)=>{
|
|
244
|
+
// Should never happen.
|
|
245
|
+
if (!gateway.cache.requestMembers?.enabled || !options?.nonce) {
|
|
246
|
+
reject(new Error("Can't request the members without the nonce or with the feature disabled."));
|
|
247
|
+
return;
|
|
248
|
+
}
|
|
249
|
+
gateway.cache.requestMembers.pending.set(options.nonce, {
|
|
250
|
+
nonce: options.nonce,
|
|
251
|
+
resolve,
|
|
252
|
+
members: []
|
|
253
|
+
});
|
|
254
|
+
});
|
|
255
|
+
await gateway.sendPayload(shardId, {
|
|
256
|
+
op: GatewayOpcodes.RequestGuildMembers,
|
|
257
|
+
d: {
|
|
258
|
+
guild_id: guildId.toString(),
|
|
259
|
+
// If a query is provided use it, OR if a limit is NOT provided use ""
|
|
260
|
+
query: options?.query ?? (options?.limit ? undefined : ''),
|
|
261
|
+
limit: options?.limit ?? 0,
|
|
262
|
+
presences: options?.presences ?? false,
|
|
263
|
+
user_ids: options?.userIds?.map((id)=>id.toString()),
|
|
264
|
+
nonce: options?.nonce
|
|
265
|
+
}
|
|
266
|
+
});
|
|
267
|
+
return await members;
|
|
229
268
|
},
|
|
230
269
|
async leaveVoiceChannel (guildId) {
|
|
231
270
|
const shardId = gateway.calculateShardId(guildId);
|
|
232
|
-
const shard = gateway.shards.get(shardId);
|
|
233
|
-
if (!shard) {
|
|
234
|
-
throw new Error(`Shard (id: ${shardId} not found`);
|
|
235
|
-
}
|
|
236
271
|
logger.debug(`[Gateway] leaveVoiceChannel guildId: ${guildId} Shard ${shardId}`);
|
|
237
|
-
|
|
272
|
+
await gateway.sendPayload(shardId, {
|
|
273
|
+
op: GatewayOpcodes.VoiceStateUpdate,
|
|
274
|
+
d: {
|
|
275
|
+
guild_id: guildId.toString(),
|
|
276
|
+
channel_id: null,
|
|
277
|
+
self_mute: false,
|
|
278
|
+
self_deaf: false
|
|
279
|
+
}
|
|
280
|
+
});
|
|
238
281
|
}
|
|
239
282
|
};
|
|
240
283
|
return gateway;
|
package/dist/manager.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/manager.ts"],"sourcesContent":["import type { AtLeastOne, BigString, Camelize, DiscordGetGatewayBot, DiscordMember, RequestGuildMembers } from '@discordeno/types'\nimport { Collection, delay, logger } from '@discordeno/utils'\nimport Shard from './Shard.js'\nimport type { ShardEvents, StatusUpdate, UpdateVoiceState } from './types.js'\n\nexport function createGatewayManager(options: CreateGatewayManagerOptions): GatewayManager {\n if (!options.connection) {\n options.connection = {\n url: 'wss://gateway.discord.gg',\n shards: 1,\n sessionStartLimit: {\n maxConcurrency: 1,\n remaining: 1000,\n total: 1000,\n resetAfter: 1000 * 60 * 60 * 24,\n },\n }\n }\n\n const gateway: GatewayManager = {\n events: options.events,\n compress: options.compress ?? false,\n intents: options.intents ?? 0,\n properties: {\n os: options.properties?.os ?? process.platform,\n browser: options.properties?.browser ?? 'Discordeno',\n device: options.properties?.device ?? 'Discordeno',\n },\n token: options.token,\n url: options.url ?? options.connection.url ?? 'wss://gateway.discord.gg',\n version: options.version ?? 10,\n connection: options.connection,\n totalShards: options.totalShards ?? options.connection.shards ?? 1,\n lastShardId: options.lastShardId ?? 0,\n firstShardId: options.firstShardId ?? 0,\n totalWorkers: options.totalWorkers ?? 4,\n shardsPerWorker: options.shardsPerWorker ?? 25,\n spawnShardDelay: options.spawnShardDelay ?? 5300,\n shards: new Map(),\n buckets: new Map(),\n cache: {\n requestMembers: {\n enabled: options.cache?.requestMembers?.enabled ?? false,\n pending: new Collection(),\n },\n },\n\n calculateTotalShards() {\n // Bots under 100k servers do not have access to total shards.\n if (gateway.totalShards < 100) {\n logger.debug(`[Gateway] Calculating total shards: ${gateway.totalShards}`)\n return gateway.totalShards\n }\n\n logger.debug(`[Gateway] Calculating total shards`, gateway.totalShards, gateway.connection.sessionStartLimit.maxConcurrency)\n // Calculate a multiple of `maxConcurrency` which can be used to connect to the gateway.\n return (\n Math.ceil(\n gateway.totalShards /\n // If `maxConcurrency` is 1 we can safely use 16.\n (gateway.connection.sessionStartLimit.maxConcurrency === 1 ? 16 : gateway.connection.sessionStartLimit.maxConcurrency),\n ) * gateway.connection.sessionStartLimit.maxConcurrency\n )\n },\n calculateWorkerId(shardId) {\n // Ignore decimal numbers.\n let workerId = Math.floor(shardId / gateway.shardsPerWorker)\n // If the workerId overflows the maximal allowed workers we by default just use to last worker.\n if (workerId >= gateway.totalWorkers) {\n // The Id of the last available worker is total -1\n workerId = gateway.totalWorkers - 1\n }\n\n logger.debug(\n `[Gateway] Calculating workerId: Shard: ${shardId} -> Worker: ${workerId} -> Per Worker: ${gateway.shardsPerWorker} -> Total: ${gateway.totalWorkers}`,\n )\n\n return workerId\n },\n prepareBuckets() {\n for (let i = 0; i < gateway.connection.sessionStartLimit.maxConcurrency; ++i) {\n logger.debug(`[Gateway] Preparing buckets for concurrency: ${i}`)\n gateway.buckets.set(i, {\n workers: [],\n identifyRequests: [],\n })\n }\n\n // ORGANIZE ALL SHARDS INTO THEIR OWN BUCKETS\n for (let shardId = gateway.firstShardId; shardId <= gateway.lastShardId; ++shardId) {\n logger.debug(`[Gateway] Preparing buckets for shard: ${shardId}`)\n if (shardId >= gateway.totalShards) {\n throw new Error(`Shard (id: ${shardId}) is bigger or equal to the used amount of used shards which is ${gateway.totalShards}`)\n }\n\n const bucketId = shardId % gateway.connection.sessionStartLimit.maxConcurrency\n const bucket = gateway.buckets.get(bucketId)\n if (!bucket) {\n throw new Error(\n `Shard (id: ${shardId}) got assigned to an illegal bucket id: ${bucketId}, expected a bucket id between 0 and ${\n gateway.connection.sessionStartLimit.maxConcurrency - 1\n }`,\n )\n }\n\n // FIND A QUEUE IN THIS BUCKET THAT HAS SPACE\n // const worker = bucket.workers.find((w) => w.queue.length < gateway.shardsPerWorker);\n const workerId = gateway.calculateWorkerId(shardId)\n const worker = bucket.workers.find((w) => w.id === workerId)\n if (worker) {\n // IF THE QUEUE HAS SPACE JUST ADD IT TO THIS QUEUE\n worker.queue.push(shardId)\n } else {\n bucket.workers.push({ id: workerId, queue: [shardId] })\n }\n }\n\n for (const bucket of gateway.buckets.values()) {\n for (const worker of bucket.workers.values()) {\n // eslint-disable-next-line @typescript-eslint/require-array-sort-compare\n worker.queue = worker.queue.sort((a, b) => a - b)\n }\n }\n },\n async spawnShards() {\n // PREPARES ALL SHARDS IN SPECIFIC BUCKETS\n gateway.prepareBuckets()\n\n // Prefer concurrency of forEach instead of forof\n await Promise.all(\n [...gateway.buckets.entries()].map(async ([bucketId, bucket]) => {\n for (const worker of bucket.workers) {\n for (const shardId of worker.queue) {\n await gateway.tellWorkerToIdentify(worker.id, shardId, bucketId)\n }\n }\n }),\n )\n },\n async shutdown(code, reason) {\n gateway.shards.forEach((shard) => shard.close(code, reason))\n\n await delay(5000)\n },\n async tellWorkerToIdentify(workerId, shardId, bucketId) {\n logger.debug(`[Gateway] tell worker to identify (${workerId}, ${shardId}, ${bucketId})`)\n return await gateway.identify(shardId)\n },\n async identify(shardId: number) {\n let shard = this.shards.get(shardId)\n logger.debug(`[Gateway] identifying ${shard ? 'existing' : 'new'} shard (${shardId})`)\n\n if (!shard) {\n shard = new Shard({\n id: shardId,\n connection: {\n compress: this.compress,\n intents: this.intents,\n properties: this.properties,\n token: this.token,\n totalShards: this.totalShards,\n url: this.url,\n version: this.version,\n },\n events: options.events,\n requestIdentify: async () => {\n return await gateway.identify(shardId);\n },\n shardIsReady: async () => {\n logger.debug(`[Shard] Shard #${shardId} is ready`)\n await delay(gateway.spawnShardDelay)\n logger.debug(`[Shard] Resolving shard identify request`)\n gateway.buckets.get(shardId % gateway.connection.sessionStartLimit.maxConcurrency)!.identifyRequests.shift()?.()\n },\n })\n\n this.shards.set(shardId, shard)\n }\n\n const bucket = gateway.buckets.get(shardId % gateway.connection.sessionStartLimit.maxConcurrency)\n if (!bucket) return\n\n return await new Promise((resolve) => {\n // Mark that we are making an identify request so another is not made.\n bucket.identifyRequests.push(resolve)\n logger.debug(`[Gateway] identifying shard #(${shardId}).`)\n // This will trigger identify and when READY is received it will resolve the above request.\n shard?.identify()\n })\n },\n async kill(shardId: number) {\n const shard = this.shards.get(shardId)\n if (!shard) {\n return logger.debug(`[Gateway] kill shard but not found (${shardId})`)\n }\n\n logger.debug(`[Gateway] kill shard (${shardId})`)\n this.shards.delete(shardId)\n return await shard.shutdown()\n },\n\n async requestIdentify(shardId: number) {\n logger.debug(`[Gateway] requesting identify`)\n // const bucket = gateway.buckets.get(shardId % gateway.connection.sessionStartLimit.maxConcurrency)\n // if (!bucket) return\n\n // return await new Promise((resolve) => {\n // bucket.identifyRequests.push(resolve)\n // })\n },\n\n // Helpers methods below this\n\n calculateShardId(guildId, totalShards) {\n // If none is provided, use the total shards number from gateway object.\n if (!totalShards) totalShards = gateway.totalShards\n // If it is only 1 shard, it will always be shard id 0\n if (totalShards === 1) {\n logger.debug(`[Gateway] calculateShardId (1 shard)`)\n return 0\n }\n\n logger.debug(`[Gateway] calculateShardId (guildId: ${guildId}, totalShards: ${totalShards})`)\n return Number((BigInt(guildId) >> 22n) % BigInt(totalShards))\n },\n\n async joinVoiceChannel(guildId, channelId, options) {\n const shardId = gateway.calculateShardId(guildId)\n const shard = gateway.shards.get(shardId)\n if (!shard) {\n throw new Error(`Shard (id: ${shardId} not found`)\n }\n\n logger.debug(`[Gateway] joinVoiceChannel guildId: ${guildId} channelId: ${channelId}`)\n shard.joinVoiceChannel(guildId, channelId, options)\n },\n\n async editBotStatus(data) {\n logger.debug(`[Gateway] editBotStatus data: ${JSON.stringify(data)}`)\n await Promise.all(\n [...gateway.shards.values()].map(async (shard) => {\n gateway.editShardStatus(shard.id, data)\n }),\n )\n },\n\n async editShardStatus(shardId, data) {\n const shard = gateway.shards.get(shardId)\n if (!shard) {\n throw new Error(`Shard (id: ${shardId}) not found.`)\n }\n\n logger.debug(`[Gateway] editShardStatus shardId: ${shardId} -> data: ${JSON.stringify(data)}`)\n return await shard.editShardStatus(data)\n },\n\n async requestMembers(guildId, options) {\n const shardId = gateway.calculateShardId(guildId)\n const shard = gateway.shards.get(shardId)\n if (!shard) {\n throw new Error(`Shard (id: ${shardId}) not found.`)\n }\n\n logger.debug(`[Gateway] requestMembers guildId: ${guildId} -> options ${JSON.stringify(options)}`)\n return await shard.requestMembers(guildId, options)\n },\n\n async leaveVoiceChannel(guildId) {\n const shardId = gateway.calculateShardId(guildId)\n const shard = gateway.shards.get(shardId)\n if (!shard) {\n throw new Error(`Shard (id: ${shardId} not found`)\n }\n\n logger.debug(`[Gateway] leaveVoiceChannel guildId: ${guildId} Shard ${shardId}`)\n return await shard.leaveVoiceChannel(guildId)\n },\n }\n\n return gateway\n}\n\nexport interface CreateGatewayManagerOptions {\n /**\n * Id of the first Shard which should get controlled by this manager.\n * @default 0\n */\n firstShardId?: number\n /**\n * Id of the last Shard which should get controlled by this manager.\n * @default 0\n */\n lastShardId?: number\n /**\n * Delay in milliseconds to wait before spawning next shard. OPTIMAL IS ABOVE 5100. YOU DON'T WANT TO HIT THE RATE LIMIT!!!\n * @default 5300\n */\n spawnShardDelay?: number\n /**\n * Total amount of shards your bot uses. Useful for zero-downtime updates or resharding.\n * @default 1\n */\n totalShards?: number\n /**\n * The amount of shards to load per worker.\n * @default 25\n */\n shardsPerWorker?: number\n /**\n * The total amount of workers to use for your bot.\n * @default 4\n */\n totalWorkers?: number\n /** Important data which is used by the manager to connect shards to the gateway. */\n connection?: Camelize<DiscordGetGatewayBot>\n /** Whether incoming payloads are compressed using zlib.\n *\n * @default false\n */\n compress?: boolean\n /** The calculated intent value of the events which the shard should receive.\n *\n * @default 0\n */\n intents?: number\n /** Identify properties to use */\n properties?: {\n /** Operating system the shard runs on.\n *\n * @default \"darwin\" | \"linux\" | \"windows\"\n */\n os: string\n /** The \"browser\" where this shard is running on.\n *\n * @default \"Discordeno\"\n */\n browser: string\n /** The device on which the shard is running.\n *\n * @default \"Discordeno\"\n */\n device: string\n }\n /** Bot token which is used to connect to Discord */\n token: string\n /** The URL of the gateway which should be connected to.\n *\n * @default \"wss://gateway.discord.gg\"\n */\n url?: string\n /** The gateway version which should be used.\n *\n * @default 10\n */\n version?: number\n /** The events handlers */\n events: ShardEvents\n /** This managers cache related settings. */\n cache?: {\n requestMembers?: {\n /**\n * Whether or not request member requests should be cached.\n * @default false\n */\n enabled?: boolean\n /** The pending requests. */\n pending: Collection<string, RequestMemberRequest>\n }\n }\n}\n\nexport interface GatewayManager extends Required<CreateGatewayManagerOptions> {\n /** The max concurrency buckets. Those will be created when the `spawnShards` (which calls `prepareBuckets` under the hood) function gets called. */\n buckets: Map<\n number,\n {\n workers: Array<{ id: number; queue: number[] }>\n /** Requests to identify shards are made based on whether it is available to be made. */\n identifyRequests: Array<(value: void | PromiseLike<void>) => void>\n }\n >\n /** The shards that are created. */\n shards: Map<number, Shard>\n /** Determine max number of shards to use based upon the max concurrency. */\n calculateTotalShards: () => number\n /** Determine the id of the worker which is handling a shard. */\n calculateWorkerId: (shardId: number) => number\n /** Prepares all the buckets that are available for identifying the shards. */\n prepareBuckets: () => void\n /** Start identifying all the shards. */\n spawnShards: () => Promise<void>\n /** Shutdown all shards. */\n shutdown: (code: number, reason: string) => Promise<void>\n /** Allows users to hook in and change to communicate to different workers across different servers or anything they like. For example using redis pubsub to talk to other servers. */\n tellWorkerToIdentify: (workerId: number, shardId: number, bucketId: number) => Promise<void>\n /** Tell the manager to identify a Shard. If this Shard is not already managed this will also add the Shard to the manager. */\n identify: (shardId: number) => Promise<void>\n /** Kill a shard. Close a shards connection to Discord's gateway (if any) and remove it from the manager. */\n kill: (shardId: number) => Promise<void>\n /** This function makes sure that the bucket is allowed to make the next identify request. */\n requestIdentify: (shardId: number) => Promise<void>\n /** Calculates the number of shards based on the guild id and total shards. */\n calculateShardId: (guildId: BigString, totalShards?: number) => number\n /**\n * Connects the bot user to a voice or stage channel.\n *\n * This function sends the _Update Voice State_ gateway command over the gateway behind the scenes.\n *\n * @param guildId - The ID of the guild the voice channel to leave is in.\n * @param channelId - The ID of the channel you want to join.\n *\n * @remarks\n * Requires the `CONNECT` permission.\n *\n * Fires a _Voice State Update_ gateway event.\n *\n * @see {@link https://discord.com/developers/docs/topics/gateway#update-voice-state}\n */\n joinVoiceChannel: (guildId: BigString, channelId: BigString, options?: AtLeastOne<Omit<UpdateVoiceState, 'guildId' | 'channelId'>>) => Promise<void>\n /**\n * Edits the bot status in all shards that this gateway manages.\n *\n * @param data The status data to set the bots status to.\n * @returns Promise<void>\n */\n editBotStatus: (data: StatusUpdate) => Promise<void>\n /**\n * Edits the bot's status on one shard.\n *\n * @param shardId The shard id to edit the status for.\n * @param data The status data to set the bots status to.\n * @returns Promise<void>\n */\n editShardStatus: (shardId: number, data: StatusUpdate) => Promise<void>\n /**\n * Fetches the list of members for a guild over the gateway.\n *\n * @param guildId - The ID of the guild to get the list of members for.\n * @param options - The parameters for the fetching of the members.\n *\n * @remarks\n * If requesting the entire member list:\n * - Requires the `GUILD_MEMBERS` intent.\n *\n * If requesting presences ({@link RequestGuildMembers.presences | presences} set to `true`):\n * - Requires the `GUILD_PRESENCES` intent.\n *\n * If requesting a prefix ({@link RequestGuildMembers.query | query} non-`undefined`):\n * - Returns a maximum of 100 members.\n *\n * If requesting a users by ID ({@link RequestGuildMembers.userIds | userIds} non-`undefined`):\n * - Returns a maximum of 100 members.\n *\n * Fires a _Guild Members Chunk_ gateway event for every 1000 members fetched.\n *\n * @see {@link https://discord.com/developers/docs/topics/gateway#request-guild-members}\n */\n requestMembers: (guildId: BigString, options?: Omit<RequestGuildMembers, 'guildId'>) => Promise<Camelize<DiscordMember[]>>\n /**\n * Leaves the voice channel the bot user is currently in.\n *\n * This function sends the _Update Voice State_ gateway command over the gateway behind the scenes.\n *\n * @param guildId - The ID of the guild the voice channel to leave is in.\n *\n * @remarks\n * Fires a _Voice State Update_ gateway event.\n *\n * @see {@link https://discord.com/developers/docs/topics/gateway#update-voice-state}\n */\n leaveVoiceChannel: (guildId: BigString) => Promise<void>\n}\n\nexport interface RequestMemberRequest {\n /** The unique nonce for this request. */\n nonce: string\n /** The resolver handler to run when all members arrive. */\n resolve: (value: Camelize<DiscordMember[]> | PromiseLike<Camelize<DiscordMember[]>>) => void\n /** The members that have already arrived for this request. */\n members: Camelize<DiscordMember[]>\n}\n"],"names":["Collection","delay","logger","Shard","createGatewayManager","options","connection","url","shards","sessionStartLimit","maxConcurrency","remaining","total","resetAfter","gateway","events","compress","intents","properties","os","process","platform","browser","device","token","version","totalShards","lastShardId","firstShardId","totalWorkers","shardsPerWorker","spawnShardDelay","Map","buckets","cache","requestMembers","enabled","pending","calculateTotalShards","debug","Math","ceil","calculateWorkerId","shardId","workerId","floor","prepareBuckets","i","set","workers","identifyRequests","Error","bucketId","bucket","get","worker","find","w","id","queue","push","values","sort","a","b","spawnShards","Promise","all","entries","map","tellWorkerToIdentify","shutdown","code","reason","forEach","shard","close","identify","requestIdentify","shardIsReady","shift","resolve","kill","delete","calculateShardId","guildId","Number","BigInt","joinVoiceChannel","channelId","editBotStatus","data","JSON","stringify","editShardStatus","leaveVoiceChannel"],"mappings":"AACA,SAASA,UAAU,EAAEC,KAAK,EAAEC,MAAM,QAAQ,oBAAmB;AAC7D,OAAOC,WAAW,aAAY;AAG9B,OAAO,SAASC,qBAAqBC,OAAoC,EAAkB;IACzF,IAAI,CAACA,QAAQC,UAAU,EAAE;QACvBD,QAAQC,UAAU,GAAG;YACnBC,KAAK;YACLC,QAAQ;YACRC,mBAAmB;gBACjBC,gBAAgB;gBAChBC,WAAW;gBACXC,OAAO;gBACPC,YAAY,OAAO,KAAK,KAAK;YAC/B;QACF;IACF,CAAC;IAED,MAAMC,UAA0B;QAC9BC,QAAQV,QAAQU,MAAM;QACtBC,UAAUX,QAAQW,QAAQ,IAAI,KAAK;QACnCC,SAASZ,QAAQY,OAAO,IAAI;QAC5BC,YAAY;YACVC,IAAId,QAAQa,UAAU,EAAEC,MAAMC,QAAQC,QAAQ;YAC9CC,SAASjB,QAAQa,UAAU,EAAEI,WAAW;YACxCC,QAAQlB,QAAQa,UAAU,EAAEK,UAAU;QACxC;QACAC,OAAOnB,QAAQmB,KAAK;QACpBjB,KAAKF,QAAQE,GAAG,IAAIF,QAAQC,UAAU,CAACC,GAAG,IAAI;QAC9CkB,SAASpB,QAAQoB,OAAO,IAAI;QAC5BnB,YAAYD,QAAQC,UAAU;QAC9BoB,aAAarB,QAAQqB,WAAW,IAAIrB,QAAQC,UAAU,CAACE,MAAM,IAAI;QACjEmB,aAAatB,QAAQsB,WAAW,IAAI;QACp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AkBV,OAAO,EAAE;YAC/B,MAAM1C,UAAU7B,QAAQsE,gBAAgB,CAACC;YACzC,MAAMV,QAAQ7D,QAAQN,MAAM,CAAC8C,GAAG,CAACX;YACjC,IAAI,CAACgC,OAAO;gBACV,MAAM,IAAIxB,MAAM,CAAC,WAAW,EAAER,QAAQ,UAAU,CAAC,EAAC;YACpD,CAAC;YAEDzC,OAAOqC,KAAK,CAAC,CAAC,qCAAqC,EAAE8C,QAAQ,OAAO,EAAE1C,QAAQ,CAAC;YAC/E,OAAO,MAAMgC,MAAMoB,iBAAiB,CAACV;QACvC;IACF;IAEA,OAAOvE;AACT,CAAC"}
|
|
1
|
+
{"version":3,"sources":["../src/manager.ts"],"sourcesContent":["/* eslint-disable @typescript-eslint/no-confusing-void-expression */\nimport {\n GatewayIntents,\n GatewayOpcodes,\n type AtLeastOne,\n type BigString,\n type Camelize,\n type DiscordGetGatewayBot,\n type DiscordMemberWithUser,\n type RequestGuildMembers,\n} from '@discordeno/types'\nimport { Collection, delay, logger } from '@discordeno/utils'\nimport Shard from './Shard.js'\nimport type { ShardEvents, ShardSocketRequest, StatusUpdate, UpdateVoiceState } from './types.js'\n\nexport function createGatewayManager(options: CreateGatewayManagerOptions): GatewayManager {\n const connectionOptions = options.connection ?? {\n url: 'wss://gateway.discord.gg',\n shards: 1,\n sessionStartLimit: {\n maxConcurrency: 1,\n remaining: 1000,\n total: 1000,\n resetAfter: 1000 * 60 * 60 * 24,\n },\n }\n\n const gateway: GatewayManager = {\n events: options.events,\n compress: options.compress ?? false,\n intents: options.intents ?? 0,\n properties: {\n os: options.properties?.os ?? process.platform,\n browser: options.properties?.browser ?? 'Discordeno',\n device: options.properties?.device ?? 'Discordeno',\n },\n token: options.token,\n url: options.url ?? connectionOptions.url ?? 'wss://gateway.discord.gg',\n version: options.version ?? 10,\n connection: connectionOptions,\n totalShards: options.totalShards ?? connectionOptions.shards ?? 1,\n lastShardId: options.lastShardId ?? (options.totalShards ? options.totalShards - 1 : connectionOptions ? connectionOptions.shards - 1 : 0),\n firstShardId: options.firstShardId ?? 0,\n totalWorkers: options.totalWorkers ?? 4,\n shardsPerWorker: options.shardsPerWorker ?? 25,\n spawnShardDelay: options.spawnShardDelay ?? 5300,\n preferSnakeCase: options.preferSnakeCase ?? false,\n shards: new Map(),\n buckets: new Map(),\n cache: {\n requestMembers: {\n enabled: options.cache?.requestMembers?.enabled ?? false,\n pending: new Collection(),\n },\n },\n\n calculateTotalShards() {\n // Bots under 100k servers do not have access to total shards.\n if (gateway.totalShards < 100) {\n logger.debug(`[Gateway] Calculating total shards: ${gateway.totalShards}`)\n return gateway.totalShards\n }\n\n logger.debug(`[Gateway] Calculating total shards`, gateway.totalShards, gateway.connection.sessionStartLimit.maxConcurrency)\n // Calculate a multiple of `maxConcurrency` which can be used to connect to the gateway.\n return (\n Math.ceil(\n gateway.totalShards /\n // If `maxConcurrency` is 1 we can safely use 16.\n (gateway.connection.sessionStartLimit.maxConcurrency === 1 ? 16 : gateway.connection.sessionStartLimit.maxConcurrency),\n ) * gateway.connection.sessionStartLimit.maxConcurrency\n )\n },\n calculateWorkerId(shardId) {\n const workerId = Math.min(shardId % gateway.shardsPerWorker, gateway.totalWorkers - 1)\n logger.debug(\n `[Gateway] Calculating workerId: Shard: ${shardId} -> Worker: ${workerId} -> Per Worker: ${gateway.shardsPerWorker} -> Total: ${gateway.totalWorkers}`,\n )\n return workerId\n },\n prepareBuckets() {\n for (let i = 0; i < gateway.connection.sessionStartLimit.maxConcurrency; ++i) {\n logger.debug(`[Gateway] Preparing buckets for concurrency: ${i}`)\n gateway.buckets.set(i, {\n workers: [],\n identifyRequests: [],\n })\n }\n\n // ORGANIZE ALL SHARDS INTO THEIR OWN BUCKETS\n for (let shardId = gateway.firstShardId; shardId <= gateway.lastShardId; ++shardId) {\n logger.debug(`[Gateway] Preparing buckets for shard: ${shardId}`)\n if (shardId >= gateway.totalShards) {\n throw new Error(`Shard (id: ${shardId}) is bigger or equal to the used amount of used shards which is ${gateway.totalShards}`)\n }\n\n const bucketId = shardId % gateway.connection.sessionStartLimit.maxConcurrency\n const bucket = gateway.buckets.get(bucketId)\n if (!bucket) {\n throw new Error(\n `Shard (id: ${shardId}) got assigned to an illegal bucket id: ${bucketId}, expected a bucket id between 0 and ${\n gateway.connection.sessionStartLimit.maxConcurrency - 1\n }`,\n )\n }\n\n // FIND A QUEUE IN THIS BUCKET THAT HAS SPACE\n // const worker = bucket.workers.find((w) => w.queue.length < gateway.shardsPerWorker);\n const workerId = gateway.calculateWorkerId(shardId)\n const worker = bucket.workers.find((w) => w.id === workerId)\n if (worker) {\n // IF THE QUEUE HAS SPACE JUST ADD IT TO THIS QUEUE\n worker.queue.push(shardId)\n } else {\n bucket.workers.push({ id: workerId, queue: [shardId] })\n }\n }\n\n for (const bucket of gateway.buckets.values()) {\n for (const worker of bucket.workers.values()) {\n // eslint-disable-next-line @typescript-eslint/require-array-sort-compare\n worker.queue = worker.queue.sort((a, b) => a - b)\n }\n }\n },\n async spawnShards() {\n // PREPARES ALL SHARDS IN SPECIFIC BUCKETS\n gateway.prepareBuckets()\n\n // Prefer concurrency of forEach instead of forof\n await Promise.all(\n [...gateway.buckets.entries()].map(async ([bucketId, bucket]) => {\n for (const worker of bucket.workers) {\n for (const shardId of worker.queue) {\n await gateway.tellWorkerToIdentify(worker.id, shardId, bucketId)\n }\n }\n }),\n )\n },\n async shutdown(code, reason) {\n gateway.shards.forEach((shard) => shard.close(code, reason))\n\n await delay(5000)\n },\n async sendPayload(shardId, payload) {\n const shard = gateway.shards.get(shardId)\n\n if (!shard) {\n throw new Error(`Shard (id: ${shardId} not found`)\n }\n\n await shard.send(payload)\n },\n async tellWorkerToIdentify(workerId, shardId, bucketId) {\n logger.debug(`[Gateway] tell worker to identify (${workerId}, ${shardId}, ${bucketId})`)\n await gateway.identify(shardId)\n },\n async identify(shardId: number) {\n let shard = this.shards.get(shardId)\n logger.debug(`[Gateway] identifying ${shard ? 'existing' : 'new'} shard (${shardId})`)\n\n if (!shard) {\n shard = new Shard({\n id: shardId,\n connection: {\n compress: this.compress,\n intents: this.intents,\n properties: this.properties,\n token: this.token,\n totalShards: this.totalShards,\n url: this.url,\n version: this.version,\n },\n events: options.events,\n requestIdentify: async () => {\n await gateway.identify(shardId)\n },\n shardIsReady: async () => {\n logger.debug(`[Shard] Shard #${shardId} is ready`)\n await delay(gateway.spawnShardDelay)\n logger.debug(`[Shard] Resolving shard identify request`)\n gateway.buckets.get(shardId % gateway.connection.sessionStartLimit.maxConcurrency)!.identifyRequests.shift()?.()\n },\n })\n\n if (this.preferSnakeCase) {\n shard.forwardToBot = async (payload) => {\n options.events.message?.(shard!, payload)\n }\n }\n\n this.shards.set(shardId, shard)\n }\n\n const bucket = gateway.buckets.get(shardId % gateway.connection.sessionStartLimit.maxConcurrency)\n if (!bucket) return\n\n return await new Promise((resolve) => {\n // Mark that we are making an identify request so another is not made.\n bucket.identifyRequests.push(resolve)\n logger.debug(`[Gateway] identifying shard #(${shardId}).`)\n // This will trigger identify and when READY is received it will resolve the above request.\n shard?.identify()\n })\n },\n async kill(shardId: number) {\n const shard = this.shards.get(shardId)\n if (!shard) {\n return logger.debug(`[Gateway] kill shard but not found (${shardId})`)\n }\n\n logger.debug(`[Gateway] kill shard (${shardId})`)\n this.shards.delete(shardId)\n await shard.shutdown()\n },\n async requestIdentify(shardId: number) {\n logger.debug(`[Gateway] requesting identify`)\n // const bucket = gateway.buckets.get(shardId % gateway.connection.sessionStartLimit.maxConcurrency)\n // if (!bucket) return\n\n // return await new Promise((resolve) => {\n // bucket.identifyRequests.push(resolve)\n // })\n },\n\n // Helpers methods below this\n\n calculateShardId(guildId, totalShards) {\n // If none is provided, use the total shards number from gateway object.\n if (!totalShards) totalShards = gateway.totalShards\n // If it is only 1 shard, it will always be shard id 0\n if (totalShards === 1) {\n logger.debug(`[Gateway] calculateShardId (1 shard)`)\n return 0\n }\n\n logger.debug(`[Gateway] calculateShardId (guildId: ${guildId}, totalShards: ${totalShards})`)\n return Number((BigInt(guildId) >> 22n) % BigInt(totalShards))\n },\n\n async joinVoiceChannel(guildId, channelId, options) {\n const shardId = gateway.calculateShardId(guildId)\n\n logger.debug(`[Gateway] joinVoiceChannel guildId: ${guildId} channelId: ${channelId}`)\n\n await gateway.sendPayload(shardId, {\n op: GatewayOpcodes.VoiceStateUpdate,\n d: {\n guild_id: guildId.toString(),\n channel_id: channelId.toString(),\n self_mute: options?.selfMute ?? false,\n self_deaf: options?.selfDeaf ?? true,\n },\n })\n },\n\n async editBotStatus(data) {\n logger.debug(`[Gateway] editBotStatus data: ${JSON.stringify(data)}`)\n\n await Promise.all(\n [...gateway.shards.values()].map(async (shard) => {\n gateway.editShardStatus(shard.id, data)\n }),\n )\n },\n\n async editShardStatus(shardId, data) {\n logger.debug(`[Gateway] editShardStatus shardId: ${shardId} -> data: ${JSON.stringify(data)}`)\n\n await gateway.sendPayload(shardId, {\n op: GatewayOpcodes.PresenceUpdate,\n d: {\n since: null,\n afk: false,\n activities: data.activities,\n status: data.status,\n },\n })\n },\n\n async requestMembers(guildId, options) {\n const shardId = gateway.calculateShardId(guildId)\n\n if (gateway.intents && (!options?.limit || options.limit > 1) && !(gateway.intents & GatewayIntents.GuildMembers))\n throw new Error('Cannot fetch more then 1 member without the GUILD_MEMBERS intent')\n\n logger.debug(`[Gateway] requestMembers guildId: ${guildId} -> data: ${JSON.stringify(options)}`)\n\n if (options?.userIds?.length) {\n logger.debug(`[Gateway] requestMembers guildId: ${guildId} -> setting user limit based on userIds length: ${options.userIds.length}`)\n\n options.limit = options.userIds.length\n }\n\n const members =\n !gateway.cache.requestMembers?.enabled || !options?.nonce\n ? []\n : new Promise<Camelize<DiscordMemberWithUser[]>>((resolve, reject) => {\n // Should never happen.\n if (!gateway.cache.requestMembers?.enabled || !options?.nonce) {\n reject(new Error(\"Can't request the members without the nonce or with the feature disabled.\"))\n return\n }\n\n gateway.cache.requestMembers.pending.set(options.nonce, {\n nonce: options.nonce,\n resolve,\n members: [],\n })\n })\n\n await gateway.sendPayload(shardId, {\n op: GatewayOpcodes.RequestGuildMembers,\n d: {\n guild_id: guildId.toString(),\n // If a query is provided use it, OR if a limit is NOT provided use \"\"\n query: options?.query ?? (options?.limit ? undefined : ''),\n limit: options?.limit ?? 0,\n presences: options?.presences ?? false,\n user_ids: options?.userIds?.map((id) => id.toString()),\n nonce: options?.nonce,\n },\n })\n\n return await members\n },\n\n async leaveVoiceChannel(guildId) {\n const shardId = gateway.calculateShardId(guildId)\n\n logger.debug(`[Gateway] leaveVoiceChannel guildId: ${guildId} Shard ${shardId}`)\n\n await gateway.sendPayload(shardId, {\n op: GatewayOpcodes.VoiceStateUpdate,\n d: {\n guild_id: guildId.toString(),\n channel_id: null,\n self_mute: false,\n self_deaf: false,\n },\n })\n },\n }\n\n return gateway\n}\n\nexport interface CreateGatewayManagerOptions {\n /**\n * Id of the first Shard which should get controlled by this manager.\n * @default 0\n */\n firstShardId?: number\n /**\n * Id of the last Shard which should get controlled by this manager.\n * @default 0\n */\n lastShardId?: number\n /**\n * Delay in milliseconds to wait before spawning next shard. OPTIMAL IS ABOVE 5100. YOU DON'T WANT TO HIT THE RATE LIMIT!!!\n * @default 5300\n */\n spawnShardDelay?: number\n /**\n * Whether to send the discord packets in snake case form.\n * @default false\n */\n preferSnakeCase?: boolean\n /**\n * Total amount of shards your bot uses. Useful for zero-downtime updates or resharding.\n * @default 1\n */\n totalShards?: number\n /**\n * The amount of shards to load per worker.\n * @default 25\n */\n shardsPerWorker?: number\n /**\n * The total amount of workers to use for your bot.\n * @default 4\n */\n totalWorkers?: number\n /** Important data which is used by the manager to connect shards to the gateway. */\n connection?: Camelize<DiscordGetGatewayBot>\n /** Whether incoming payloads are compressed using zlib.\n *\n * @default false\n */\n compress?: boolean\n /** The calculated intent value of the events which the shard should receive.\n *\n * @default 0\n */\n intents?: number\n /** Identify properties to use */\n properties?: {\n /** Operating system the shard runs on.\n *\n * @default \"darwin\" | \"linux\" | \"windows\"\n */\n os: string\n /** The \"browser\" where this shard is running on.\n *\n * @default \"Discordeno\"\n */\n browser: string\n /** The device on which the shard is running.\n *\n * @default \"Discordeno\"\n */\n device: string\n }\n /** Bot token which is used to connect to Discord */\n token: string\n /** The URL of the gateway which should be connected to.\n *\n * @default \"wss://gateway.discord.gg\"\n */\n url?: string\n /** The gateway version which should be used.\n *\n * @default 10\n */\n version?: number\n /** The events handlers */\n events: ShardEvents\n /** This managers cache related settings. */\n cache?: {\n requestMembers?: {\n /**\n * Whether or not request member requests should be cached.\n * @default false\n */\n enabled?: boolean\n /** The pending requests. */\n pending: Collection<string, RequestMemberRequest>\n }\n }\n}\n\nexport interface GatewayManager extends Required<CreateGatewayManagerOptions> {\n /** The max concurrency buckets. Those will be created when the `spawnShards` (which calls `prepareBuckets` under the hood) function gets called. */\n buckets: Map<\n number,\n {\n workers: Array<{ id: number; queue: number[] }>\n /** Requests to identify shards are made based on whether it is available to be made. */\n identifyRequests: Array<(value: void | PromiseLike<void>) => void>\n }\n >\n /** The shards that are created. */\n shards: Map<number, Shard>\n /** Determine max number of shards to use based upon the max concurrency. */\n calculateTotalShards: () => number\n /** Determine the id of the worker which is handling a shard. */\n calculateWorkerId: (shardId: number) => number\n /** Prepares all the buckets that are available for identifying the shards. */\n prepareBuckets: () => void\n /** Start identifying all the shards. */\n spawnShards: () => Promise<void>\n /** Shutdown all shards. */\n shutdown: (code: number, reason: string) => Promise<void>\n sendPayload: (shardId: number, payload: ShardSocketRequest) => Promise<void>\n /** Allows users to hook in and change to communicate to different workers across different servers or anything they like. For example using redis pubsub to talk to other servers. */\n tellWorkerToIdentify: (workerId: number, shardId: number, bucketId: number) => Promise<void>\n /** Tell the manager to identify a Shard. If this Shard is not already managed this will also add the Shard to the manager. */\n identify: (shardId: number) => Promise<void>\n /** Kill a shard. Close a shards connection to Discord's gateway (if any) and remove it from the manager. */\n kill: (shardId: number) => Promise<void>\n /** This function makes sure that the bucket is allowed to make the next identify request. */\n requestIdentify: (shardId: number) => Promise<void>\n /** Calculates the number of shards based on the guild id and total shards. */\n calculateShardId: (guildId: BigString, totalShards?: number) => number\n /**\n * Connects the bot user to a voice or stage channel.\n *\n * This function sends the _Update Voice State_ gateway command over the gateway behind the scenes.\n *\n * @param guildId - The ID of the guild the voice channel to leave is in.\n * @param channelId - The ID of the channel you want to join.\n *\n * @remarks\n * Requires the `CONNECT` permission.\n *\n * Fires a _Voice State Update_ gateway event.\n *\n * @see {@link https://discord.com/developers/docs/topics/gateway#update-voice-state}\n */\n joinVoiceChannel: (guildId: BigString, channelId: BigString, options?: AtLeastOne<Omit<UpdateVoiceState, 'guildId' | 'channelId'>>) => Promise<void>\n /**\n * Edits the bot status in all shards that this gateway manages.\n *\n * @param data The status data to set the bots status to.\n * @returns Promise<void>\n */\n editBotStatus: (data: StatusUpdate) => Promise<void>\n /**\n * Edits the bot's status on one shard.\n *\n * @param shardId The shard id to edit the status for.\n * @param data The status data to set the bots status to.\n * @returns Promise<void>\n */\n editShardStatus: (shardId: number, data: StatusUpdate) => Promise<void>\n /**\n * Fetches the list of members for a guild over the gateway.\n *\n * @param guildId - The ID of the guild to get the list of members for.\n * @param options - The parameters for the fetching of the members.\n *\n * @remarks\n * If requesting the entire member list:\n * - Requires the `GUILD_MEMBERS` intent.\n *\n * If requesting presences ({@link RequestGuildMembers.presences | presences} set to `true`):\n * - Requires the `GUILD_PRESENCES` intent.\n *\n * If requesting a prefix ({@link RequestGuildMembers.query | query} non-`undefined`):\n * - Returns a maximum of 100 members.\n *\n * If requesting a users by ID ({@link RequestGuildMembers.userIds | userIds} non-`undefined`):\n * - Returns a maximum of 100 members.\n *\n * Fires a _Guild Members Chunk_ gateway event for every 1000 members fetched.\n *\n * @see {@link https://discord.com/developers/docs/topics/gateway#request-guild-members}\n */\n requestMembers: (guildId: BigString, options?: Omit<RequestGuildMembers, 'guildId'>) => Promise<Camelize<DiscordMemberWithUser[]>>\n /**\n * Leaves the voice channel the bot user is currently in.\n *\n * This function sends the _Update Voice State_ gateway command over the gateway behind the scenes.\n *\n * @param guildId - The ID of the guild the voice channel to leave is in.\n *\n * @remarks\n * Fires a _Voice State Update_ gateway event.\n *\n * @see {@link https://discord.com/developers/docs/topics/gateway#update-voice-state}\n */\n leaveVoiceChannel: (guildId: BigString) => Promise<void>\n}\n\nexport interface RequestMemberRequest {\n /** The unique nonce for this request. */\n nonce: string\n /** The resolver handler to run when all members arrive. */\n resolve: (value: Camelize<DiscordMemberWithUser[]> | PromiseLike<Camelize<DiscordMemberWithUser[]>>) => void\n /** The members that have already arrived for this request. */\n members: DiscordMemberWithUser[]\n}\n"],"names":["GatewayIntents","GatewayOpcodes","Collection","delay","logger","Shard","createGatewayManager","options","connectionOptions","connection","url","shards","sessionStartLimit","maxConcurrency","remaining","total","resetAfter","gateway","events","compress","intents","properties","os","process","platform","browser","device","token","version","totalShards","lastShardId","firstShardId","totalWorkers","shardsPerWorker","spawnShardDelay","preferSnakeCase","Map","buckets","cache","requestMembers","enabled","pending","calculateTotalShards","debug","Math","ceil","calculateWorkerId","shardId","workerId","min","prepareBuckets","i","set","workers","identifyRequests","Error","bucketId","bucket","get","worker","find","w","id","queue","push","values","sort","a","b","spawnShards","Promise","all","entries","map","tellWorkerToIdentify","shutdown","code","reason","forEach","shard","close","sendPayload","payload","send","identify","requestIdentify","shardIsReady","shift","forwardToBot","message","resolve","kill","delete","calculateShardId","guildId","Number","BigInt","joinVoiceChannel","channelId","op","VoiceStateUpdate","d","guild_id","toString","channel_id","self_mute","selfMute","self_deaf","selfDeaf","editBotStatus","data","JSON","stringify","editShardStatus","PresenceUpdate","since","afk","activities","status","limit","GuildMembers","userIds","length","members","nonce","reject","RequestGuildMembers","query","undefined","presences","user_ids","leaveVoiceChannel"],"mappings":"AAAA,kEAAkE,GAClE,SACEA,cAAc,EACdC,cAAc,QAOT,oBAAmB;AAC1B,SAASC,UAAU,EAAEC,KAAK,EAAEC,MAAM,QAAQ,oBAAmB;AAC7D,OAAOC,WAAW,aAAY;AAG9B,OAAO,SAASC,qBAAqBC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;oBAC1BC,YAAY,IAAI;oBAChBC,WAAW,KAAK;oBAChBE,WAAW,KAAK;gBAClB;YACF;QACF;IACF;IAEA,OAAO1F;AACT,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@discordeno/gateway",
|
|
3
|
-
"version": "19.0.0-next.
|
|
3
|
+
"version": "19.0.0-next.a26c7c0",
|
|
4
4
|
"main": "./dist/index.js",
|
|
5
5
|
"types": "./dist/index.d.ts",
|
|
6
6
|
"type": "module",
|
|
@@ -24,8 +24,8 @@
|
|
|
24
24
|
"test:test-type": "tsc --project tsconfig.test.json"
|
|
25
25
|
},
|
|
26
26
|
"dependencies": {
|
|
27
|
-
"@discordeno/types": "19.0.0-next.
|
|
28
|
-
"@discordeno/utils": "19.0.0-next.
|
|
27
|
+
"@discordeno/types": "19.0.0-next.a26c7c0",
|
|
28
|
+
"@discordeno/utils": "19.0.0-next.a26c7c0",
|
|
29
29
|
"ws": "^8.13.0"
|
|
30
30
|
},
|
|
31
31
|
"devDependencies": {
|