Skip to content

An extension to the discord.js `Client` class with `[Symbol.asyncDispose]` added. ๐Ÿšฎ

License

Notifications You must be signed in to change notification settings

JoshuaKGoldberg/disposable-discord-client

Repository files navigation

Disposable Discord Client

An extension to the discord.js Client class with [Symbol.asyncDispose] added. ๐Ÿšฎ

All Contributors: 1 ๐Ÿ‘ช Contributor Covenant License: MIT Style: Prettier TypeScript: Strict npm package version

Usage

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();
	}
}

What?

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.

Contributors

Josh Goldberg โœจ
Josh Goldberg โœจ

๐Ÿ’ป ๐Ÿ–‹ ๐Ÿ“– ๐Ÿค” ๐Ÿš‡ ๐Ÿšง ๐Ÿ“† ๐Ÿ”ง

๐Ÿ’™ This package was templated with create-typescript-app.

About

An extension to the discord.js `Client` class with `[Symbol.asyncDispose]` added. ๐Ÿšฎ

Resources

License

Code of conduct

Security policy

Stars

Watchers

Forks

Packages

No packages published