Skip to content

Commit

Permalink
fix(WebSocketManager): await WebSocket destroy (#9519)
Browse files Browse the repository at this point in the history
fix(WebSocketManager): await ws destroy

Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
  • Loading branch information
Mogyuchi and kodiakhq[bot] committed Jun 9, 2023
1 parent bc2798b commit 75308f2
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 12 deletions.
8 changes: 4 additions & 4 deletions packages/discord.js/src/client/Client.js
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ class Client extends BaseClient {
await this.ws.connect();
return this.token;
} catch (error) {
this.destroy();
await this.destroy();
throw error;
}
}
Expand All @@ -242,13 +242,13 @@ class Client extends BaseClient {

/**
* Logs out, terminates the connection to Discord, and destroys the client.
* @returns {void}
* @returns {Promise<void>}
*/
destroy() {
async destroy() {
super.destroy();

this.sweepers.destroy();
this.ws.destroy();
await this.ws.destroy();
this.token = null;
this.rest.setToken(null);
}
Expand Down
4 changes: 2 additions & 2 deletions packages/discord.js/src/client/websocket/WebSocketManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
}

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/discord.js/test/createGuild.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ client.on('ready', async () => {
} catch (error) {
console.error(error);
} finally {
client.destroy();
await client.destroy();
}
});

Expand Down
4 changes: 2 additions & 2 deletions packages/discord.js/test/shard.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
});
Expand Down
2 changes: 1 addition & 1 deletion packages/discord.js/test/templateCreateGuild.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ client
} catch (error) {
console.error(error);
} finally {
client.destroy();
await client.destroy();
}
})
.login(token)
Expand Down
4 changes: 2 additions & 2 deletions packages/discord.js/typings/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -956,7 +956,7 @@ export class Client<Ready extends boolean = boolean> extends BaseClient {
public users: UserManager;
public voice: ClientVoiceManager;
public ws: WebSocketManager;
public destroy(): void;
public destroy(): Promise<void>;
public fetchGuildPreview(guild: GuildResolvable): Promise<GuildPreview>;
public fetchInvite(invite: InviteResolvable, options?: ClientFetchInviteOptions): Promise<Invite>;
public fetchGuildTemplate(template: GuildTemplateResolvable): Promise<GuildTemplate>;
Expand Down Expand Up @@ -3329,7 +3329,7 @@ export class WebSocketManager extends EventEmitter {
private debug(message: string, shardId?: number): void;
private connect(): Promise<void>;
private broadcast(packet: unknown): void;
private destroy(): void;
private destroy(): Promise<void>;
private handlePacket(packet?: unknown, shard?: WebSocketShard): boolean;
private checkShardsReady(): void;
private triggerClientReady(): void;
Expand Down

0 comments on commit 75308f2

Please sign in to comment.