From 75308f266933454301b10ee8e1d940d782fdb637 Mon Sep 17 00:00:00 2001 From: Mogyuchi Date: Fri, 9 Jun 2023 18:59:18 +0900 Subject: [PATCH] fix(WebSocketManager): await WebSocket destroy (#9519) fix(WebSocketManager): await ws destroy Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com> --- packages/discord.js/src/client/Client.js | 8 ++++---- .../discord.js/src/client/websocket/WebSocketManager.js | 4 ++-- packages/discord.js/test/createGuild.js | 2 +- packages/discord.js/test/shard.js | 4 ++-- packages/discord.js/test/templateCreateGuild.js | 2 +- packages/discord.js/typings/index.d.ts | 4 ++-- 6 files changed, 12 insertions(+), 12 deletions(-) diff --git a/packages/discord.js/src/client/Client.js b/packages/discord.js/src/client/Client.js index 907912860f73..07d15c973f6d 100644 --- a/packages/discord.js/src/client/Client.js +++ b/packages/discord.js/src/client/Client.js @@ -226,7 +226,7 @@ class Client extends BaseClient { await this.ws.connect(); return this.token; } catch (error) { - this.destroy(); + await this.destroy(); throw error; } } @@ -242,13 +242,13 @@ class Client extends BaseClient { /** * Logs out, terminates the connection to Discord, and destroys the client. - * @returns {void} + * @returns {Promise} */ - destroy() { + async destroy() { super.destroy(); this.sweepers.destroy(); - this.ws.destroy(); + await this.ws.destroy(); this.token = null; this.rest.setToken(null); } diff --git a/packages/discord.js/src/client/websocket/WebSocketManager.js b/packages/discord.js/src/client/websocket/WebSocketManager.js index b0e4fb10a2ef..e3a2ec749778 100644 --- a/packages/discord.js/src/client/websocket/WebSocketManager.js +++ b/packages/discord.js/src/client/websocket/WebSocketManager.js @@ -320,12 +320,12 @@ class WebSocketManager extends EventEmitter { * Destroys this manager and all its shards. * @private */ - destroy() { + async destroy() { if (this.destroyed) return; // TODO: Make a util for getting a stack this.debug(`Manager was destroyed. Called by:\n${new Error().stack}`); this.destroyed = true; - this._ws?.destroy({ code: CloseCodes.Normal }); + await this._ws?.destroy({ code: CloseCodes.Normal }); } /** diff --git a/packages/discord.js/test/createGuild.js b/packages/discord.js/test/createGuild.js index 90e529a43182..21ac04ea16b5 100644 --- a/packages/discord.js/test/createGuild.js +++ b/packages/discord.js/test/createGuild.js @@ -26,7 +26,7 @@ client.on('ready', async () => { } catch (error) { console.error(error); } finally { - client.destroy(); + await client.destroy(); } }); diff --git a/packages/discord.js/test/shard.js b/packages/discord.js/test/shard.js index a5dfbe8e1a28..f27b30562a79 100644 --- a/packages/discord.js/test/shard.js +++ b/packages/discord.js/test/shard.js @@ -28,9 +28,9 @@ process.send(123); client.on('ready', () => { console.log('Ready', client.options.shards); if (client.options.shards === 0) { - setTimeout(() => { + setTimeout(async () => { console.log('kek dying'); - client.destroy(); + await client.destroy(); }, 5_000); } }); diff --git a/packages/discord.js/test/templateCreateGuild.js b/packages/discord.js/test/templateCreateGuild.js index 42730c567188..adf0e319b8cf 100644 --- a/packages/discord.js/test/templateCreateGuild.js +++ b/packages/discord.js/test/templateCreateGuild.js @@ -20,7 +20,7 @@ client } catch (error) { console.error(error); } finally { - client.destroy(); + await client.destroy(); } }) .login(token) diff --git a/packages/discord.js/typings/index.d.ts b/packages/discord.js/typings/index.d.ts index c8e8e4b13e8f..5c7264488f7f 100644 --- a/packages/discord.js/typings/index.d.ts +++ b/packages/discord.js/typings/index.d.ts @@ -956,7 +956,7 @@ export class Client extends BaseClient { public users: UserManager; public voice: ClientVoiceManager; public ws: WebSocketManager; - public destroy(): void; + public destroy(): Promise; public fetchGuildPreview(guild: GuildResolvable): Promise; public fetchInvite(invite: InviteResolvable, options?: ClientFetchInviteOptions): Promise; public fetchGuildTemplate(template: GuildTemplateResolvable): Promise; @@ -3329,7 +3329,7 @@ export class WebSocketManager extends EventEmitter { private debug(message: string, shardId?: number): void; private connect(): Promise; private broadcast(packet: unknown): void; - private destroy(): void; + private destroy(): Promise; private handlePacket(packet?: unknown, shard?: WebSocketShard): boolean; private checkShardsReady(): void; private triggerClientReady(): void;