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'