From d3615ae68f724413aa30122ae4ec2f2acff7aaef Mon Sep 17 00:00:00 2001 From: GrapeColor Date: Wed, 7 Jun 2023 00:46:56 +0900 Subject: [PATCH 1/4] fix(WebSocketManager): Fix not ready after reconnect --- packages/discord.js/src/client/websocket/WebSocketManager.js | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/discord.js/src/client/websocket/WebSocketManager.js b/packages/discord.js/src/client/websocket/WebSocketManager.js index b0e4fb10a2ef..fad819490894 100644 --- a/packages/discord.js/src/client/websocket/WebSocketManager.js +++ b/packages/discord.js/src/client/websocket/WebSocketManager.js @@ -243,6 +243,7 @@ class WebSocketManager extends EventEmitter { this._ws.on(WSWebSocketShardEvents.Ready, ({ data, shardId }) => { this.shards.get(shardId).onReadyPacket(data); + this.client.emit(Events.ShardReady, shardId); }); this._ws.on(WSWebSocketShardEvents.Closed, ({ code, shardId }) => { From c3280358adc2130ba924002604b3d1a9a5e8479b Mon Sep 17 00:00:00 2001 From: GrapeColor Date: Wed, 7 Jun 2023 08:04:07 +0900 Subject: [PATCH 2/4] fix(WebSocketManager): Move fixes from Ready to Resume --- packages/discord.js/src/client/websocket/WebSocketManager.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/discord.js/src/client/websocket/WebSocketManager.js b/packages/discord.js/src/client/websocket/WebSocketManager.js index fad819490894..c4e7f1f35fdc 100644 --- a/packages/discord.js/src/client/websocket/WebSocketManager.js +++ b/packages/discord.js/src/client/websocket/WebSocketManager.js @@ -243,7 +243,6 @@ class WebSocketManager extends EventEmitter { this._ws.on(WSWebSocketShardEvents.Ready, ({ data, shardId }) => { this.shards.get(shardId).onReadyPacket(data); - this.client.emit(Events.ShardReady, shardId); }); this._ws.on(WSWebSocketShardEvents.Closed, ({ code, shardId }) => { @@ -288,6 +287,7 @@ class WebSocketManager extends EventEmitter { * @event WebSocketShard#resumed */ shard.emit(WebSocketShardEvents.Resumed); + this.client.emit(Events.ShardReady, shardId); }); this._ws.on(WSWebSocketShardEvents.HeartbeatComplete, ({ heartbeatAt, latency, shardId }) => { From dbabb62a0bb3adf696364a5f0eb338c670c62ef7 Mon Sep 17 00:00:00 2001 From: GrapeColor Date: Wed, 7 Jun 2023 21:45:43 +0900 Subject: [PATCH 3/4] fix: undo changes --- packages/discord.js/src/client/websocket/WebSocketManager.js | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/discord.js/src/client/websocket/WebSocketManager.js b/packages/discord.js/src/client/websocket/WebSocketManager.js index c4e7f1f35fdc..b0e4fb10a2ef 100644 --- a/packages/discord.js/src/client/websocket/WebSocketManager.js +++ b/packages/discord.js/src/client/websocket/WebSocketManager.js @@ -287,7 +287,6 @@ class WebSocketManager extends EventEmitter { * @event WebSocketShard#resumed */ shard.emit(WebSocketShardEvents.Resumed); - this.client.emit(Events.ShardReady, shardId); }); this._ws.on(WSWebSocketShardEvents.HeartbeatComplete, ({ heartbeatAt, latency, shardId }) => { From 6e9c02c606e9d0f17b639d9a4f3ffc5dd86c35a8 Mon Sep 17 00:00:00 2001 From: GrapeColor Date: Wed, 7 Jun 2023 21:45:57 +0900 Subject: [PATCH 4/4] feat: Add message to send resume event to shard --- packages/discord.js/src/sharding/Shard.js | 6 ++++++ packages/discord.js/src/sharding/ShardClientUtil.js | 6 ++++++ 2 files changed, 12 insertions(+) diff --git a/packages/discord.js/src/sharding/Shard.js b/packages/discord.js/src/sharding/Shard.js index e7d2bea43ab7..d0bccf09d6c4 100644 --- a/packages/discord.js/src/sharding/Shard.js +++ b/packages/discord.js/src/sharding/Shard.js @@ -367,6 +367,12 @@ class Shard extends EventEmitter { return; } + // Shard has resumed + if (message._resume) { + this.ready = true; + return; + } + // Shard is requesting a property fetch if (message._sFetchProp) { const resp = { _sFetchProp: message._sFetchProp, _sFetchPropShard: message._sFetchPropShard }; diff --git a/packages/discord.js/src/sharding/ShardClientUtil.js b/packages/discord.js/src/sharding/ShardClientUtil.js index edb799d016ec..c1bd4a800571 100644 --- a/packages/discord.js/src/sharding/ShardClientUtil.js +++ b/packages/discord.js/src/sharding/ShardClientUtil.js @@ -42,6 +42,9 @@ class ShardClientUtil { client.on(Events.ShardReconnecting, () => { process.send({ _reconnecting: true }); }); + client.on(Events.ShardResume, () => { + process.send({ _resume: true }); + }); break; case 'worker': this.parentPort = require('node:worker_threads').parentPort; @@ -55,6 +58,9 @@ class ShardClientUtil { client.on(Events.ShardReconnecting, () => { this.parentPort.postMessage({ _reconnecting: true }); }); + client.on(Events.ShardResume, () => { + this.parentPort.postMessage({ _resume: true }); + }); break; } }