From 027a8f87abab11130a07197b7850eaa8af1097f7 Mon Sep 17 00:00:00 2001 From: Helloyunho Date: Fri, 17 May 2024 22:33:09 +0900 Subject: [PATCH] =?UTF-8?q?=F0=9F=9A=91=EF=B8=8F=20fix:=20url=20for=20resu?= =?UTF-8?q?ming=20should=20be=20different?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/gateway/handlers/ready.ts | 1 + src/gateway/mod.ts | 11 ++++++++--- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/src/gateway/handlers/ready.ts b/src/gateway/handlers/ready.ts index f19474e1..7ebc23a1 100644 --- a/src/gateway/handlers/ready.ts +++ b/src/gateway/handlers/ready.ts @@ -6,6 +6,7 @@ export const ready: GatewayEventHandler = async ( gateway: Gateway, d: Ready ) => { + gateway._resumeGatewayURL = d.resume_gateway_url gateway._guildsToBeLoaded = gateway.client.intents!.includes(GatewayIntents.GUILDS) === true ? d.guilds.length diff --git a/src/gateway/mod.ts b/src/gateway/mod.ts index 89b22c72..143d527c 100644 --- a/src/gateway/mod.ts +++ b/src/gateway/mod.ts @@ -70,6 +70,7 @@ export class Gateway extends HarmonyEventEmitter { shards?: number[] ping: number = 0 + _resumeGatewayURL?: string _readyReceived: Promise _resolveReadyReceived?: () => void _guildsToBeLoaded?: number @@ -466,17 +467,21 @@ export class Gateway extends HarmonyEventEmitter { } this.closeGateway(RECONNECT_CODE, 'Reconnecting...') - this.initWebsocket() + this.initWebsocket(!(forceNew ?? false)) } - initWebsocket(): void { + initWebsocket(resume: boolean = false): void { if (this.#destroyCalled) return + if (resume && this._resumeGatewayURL === undefined) + throw new Error('Resume was requested but no resume URL was found') this.emit('init') this.debug('Initializing WebSocket...') + const url = resume ? this._resumeGatewayURL : Constants.DISCORD_GATEWAY_URL + this._resumeGatewayURL = undefined this.websocket = new WebSocket( // eslint-disable-next-line @typescript-eslint/restrict-template-expressions - `${Constants.DISCORD_GATEWAY_URL}/?v=${Constants.DISCORD_API_VERSION}&encoding=json`, + `${url}/?v=${Constants.DISCORD_API_VERSION}&encoding=json`, [] ) this.websocket.binaryType = 'arraybuffer'