From 57dc14bbd86273ed5cc74427e0d69051fb41a48f Mon Sep 17 00:00:00 2001 From: Mogyuchi Date: Fri, 5 May 2023 23:37:06 +0900 Subject: [PATCH] fix(WebSocketManager): await ws destroy --- 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 8e5456d77bb5..a79b7c42806a 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 6ddddbdcae1c..099b90000508 100644 --- a/packages/discord.js/typings/index.d.ts +++ b/packages/discord.js/typings/index.d.ts @@ -955,7 +955,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; @@ -3325,7 +3325,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;