Skip to content

Commit 75308f2

Browse files
Mogyuchikodiakhq[bot]
andauthoredJun 9, 2023
fix(WebSocketManager): await WebSocket destroy (#9519)
fix(WebSocketManager): await ws destroy Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
1 parent bc2798b commit 75308f2

File tree

6 files changed

+12
-12
lines changed

6 files changed

+12
-12
lines changed
 

‎packages/discord.js/src/client/Client.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ class Client extends BaseClient {
226226
await this.ws.connect();
227227
return this.token;
228228
} catch (error) {
229-
this.destroy();
229+
await this.destroy();
230230
throw error;
231231
}
232232
}
@@ -242,13 +242,13 @@ class Client extends BaseClient {
242242

243243
/**
244244
* Logs out, terminates the connection to Discord, and destroys the client.
245-
* @returns {void}
245+
* @returns {Promise<void>}
246246
*/
247-
destroy() {
247+
async destroy() {
248248
super.destroy();
249249

250250
this.sweepers.destroy();
251-
this.ws.destroy();
251+
await this.ws.destroy();
252252
this.token = null;
253253
this.rest.setToken(null);
254254
}

‎packages/discord.js/src/client/websocket/WebSocketManager.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -320,12 +320,12 @@ class WebSocketManager extends EventEmitter {
320320
* Destroys this manager and all its shards.
321321
* @private
322322
*/
323-
destroy() {
323+
async destroy() {
324324
if (this.destroyed) return;
325325
// TODO: Make a util for getting a stack
326326
this.debug(`Manager was destroyed. Called by:\n${new Error().stack}`);
327327
this.destroyed = true;
328-
this._ws?.destroy({ code: CloseCodes.Normal });
328+
await this._ws?.destroy({ code: CloseCodes.Normal });
329329
}
330330

331331
/**

‎packages/discord.js/test/createGuild.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ client.on('ready', async () => {
2626
} catch (error) {
2727
console.error(error);
2828
} finally {
29-
client.destroy();
29+
await client.destroy();
3030
}
3131
});
3232

‎packages/discord.js/test/shard.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ process.send(123);
2828
client.on('ready', () => {
2929
console.log('Ready', client.options.shards);
3030
if (client.options.shards === 0) {
31-
setTimeout(() => {
31+
setTimeout(async () => {
3232
console.log('kek dying');
33-
client.destroy();
33+
await client.destroy();
3434
}, 5_000);
3535
}
3636
});

‎packages/discord.js/test/templateCreateGuild.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ client
2020
} catch (error) {
2121
console.error(error);
2222
} finally {
23-
client.destroy();
23+
await client.destroy();
2424
}
2525
})
2626
.login(token)

‎packages/discord.js/typings/index.d.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -956,7 +956,7 @@ export class Client<Ready extends boolean = boolean> extends BaseClient {
956956
public users: UserManager;
957957
public voice: ClientVoiceManager;
958958
public ws: WebSocketManager;
959-
public destroy(): void;
959+
public destroy(): Promise<void>;
960960
public fetchGuildPreview(guild: GuildResolvable): Promise<GuildPreview>;
961961
public fetchInvite(invite: InviteResolvable, options?: ClientFetchInviteOptions): Promise<Invite>;
962962
public fetchGuildTemplate(template: GuildTemplateResolvable): Promise<GuildTemplate>;
@@ -3329,7 +3329,7 @@ export class WebSocketManager extends EventEmitter {
33293329
private debug(message: string, shardId?: number): void;
33303330
private connect(): Promise<void>;
33313331
private broadcast(packet: unknown): void;
3332-
private destroy(): void;
3332+
private destroy(): Promise<void>;
33333333
private handlePacket(packet?: unknown, shard?: WebSocketShard): boolean;
33343334
private checkShardsReady(): void;
33353335
private triggerClientReady(): void;

0 commit comments

Comments
 (0)
Please sign in to comment.