Once
function in meocord/decorator Since 4.1.0
Once<E extends keyof ClientEvents>(
event: E,
): <F extends (...args: ClientEvents[E]) => unknown>(
target: object,
propertyKey: string,
_descriptor: TypedPropertyDescriptor<F>,
) => voidHandles a discord.js client event the first time it is emitted only, on a controller or a service.
Use it for one-off work on an event, such as warming a cache once the client is ready. For every emission,
use On; for work at startup and shutdown, a service's onReady and onShutdown hooks.
Where it runs
- The handler — with the guards, interceptors and filters that apply to events: the app's global ones, its class's and its own
Parameters
| Name | Type | Description |
|---|---|---|
event | E | The client event to handle, such as |
Returns
Returns a method decorator.
<F extends (...args: ClientEvents[E]) => unknown>(
target: object,
propertyKey: string,
_descriptor: TypedPropertyDescriptor<F>,
) => voidExamples
@Service()
export class CacheWarmer {
@Once('clientReady')
async warm(client: Client<true>) {
await client.guilds.fetch()
}
}