Skip to content

Commit ec8ccf1

Browse files
committed
add ability to control if the gateway should identify or not
Allows you to add a `gateway.onIdentifyCheck = () => true` function to control if the gateway should identify during automatic instances, like when an invalid session event is received or on a hello event.
1 parent d59ab10 commit ec8ccf1

File tree

3 files changed

+15
-4
lines changed

3 files changed

+15
-4
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "detritus-client-socket",
3-
"version": "0.5.0",
3+
"version": "0.5.1",
44
"description": "A TypeScript NodeJS library to interact with Discord's Gateway",
55
"main": "lib/index.js",
66
"types": "lib/index.d.ts",

src/constants.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
export const Package = Object.freeze({
22
URL: 'https://github.com/detritusjs/client-socket',
3-
VERSION: '0.5.0',
3+
VERSION: '0.5.1',
44
});
55

66
function normalize(object: {[key: string]: any}) {

src/gateway.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ export interface SocketOptions {
8181
reconnectMax?: number,
8282
shardCount?: number,
8383
shardId?: number,
84+
onIdentifyCheck?: () => boolean | Promise<boolean>,
8485
}
8586

8687
export class Socket extends EventSpewer {
@@ -126,6 +127,8 @@ export class Socket extends EventSpewer {
126127
url: URL | null = null;
127128
userId: null | string = null;
128129

130+
onIdentifyCheck?(): boolean | Promise<boolean>;
131+
129132
constructor(
130133
token: string,
131134
options: SocketOptions = {},
@@ -155,6 +158,8 @@ export class Socket extends EventSpewer {
155158
this.shardId = <number> options.shardId;
156159
this.token = token;
157160

161+
this.onIdentifyCheck = options.onIdentifyCheck || this.onIdentifyCheck;
162+
158163
if (options.presence) {
159164
this.presence = Object.assign({}, defaultPresence, options.presence);
160165
}
@@ -490,7 +495,7 @@ export class Socket extends EventSpewer {
490495
if (this.sessionId) {
491496
this.resume();
492497
} else {
493-
this.identify();
498+
this.identifyTry();
494499
}
495500
}; break;
496501
case GatewayOpCodes.INVALID_SESSION: {
@@ -501,7 +506,7 @@ export class Socket extends EventSpewer {
501506
} else {
502507
this.sequence = 0;
503508
this.sessionId = null;
504-
this.identify();
509+
this.identifyTry();
505510
}
506511
}, Math.floor(Math.random() * 5 + 1) * 1000);
507512
}; break;
@@ -733,6 +738,12 @@ export class Socket extends EventSpewer {
733738
}, true);
734739
}
735740

741+
async identifyTry(): Promise<void> {
742+
if (!this.onIdentifyCheck || (await Promise.resolve(this.onIdentifyCheck()))) {
743+
this.identify();
744+
}
745+
}
746+
736747
resume(): void {
737748
this.resuming = true;
738749
const data = this.getResumeData();

0 commit comments

Comments
 (0)