An extension to the discord.js `Client` class with `[Symbol.asyncDispose]` added. 🚮
MIT License
👉 This is built into by Discord.js now! See discordjs/discord.js#10063 feat: add support for using keyword on discord.js Client and WebSocketManager.
npm i disposable-discord-client
import { DisposableClient } from "disposable-discord-client";
await using client = new DisposableClient(/* ... */);
await client.login();
DisposableClient
takes all the same type and value parameters as Client
.
In fact, its implementation is small enough to fit here!
import { Client } from "discord.js";
export class DisposableClient<
Ready extends boolean = boolean,
> extends Client<Ready> {
async [Symbol.asyncDispose]() {
await this.destroy();
}
}
Explicit Resource Management is a TC39 proposal to add explicit syntax for situations where a resource should have some method called upon disposal. It's stage 3 now and supported in TypeScript >=5.2.
The discord.js Client
class is a good example of a disposable resource.
When a Client
instance is no longer needed, its destroy()
should generally be called:
try {
const client = new Client(/* ... */);
await client.login();
} finally {
await client.destroy();
}
Pending Discord issue #10057: BaseClient/Client: Add support for 'using' keyword (Explicit Resource Management), DisposableDiscordClient
helps streamline code that creates and destroys a Client
instance.
💙 This package was templated with
create-typescript-app
.