Events
The EventLoader docs scans a directory for event subscriber classes and returns them grouped into six typed arrays.
Folder structure
There is no enforced folder structure, any file in any subdirectory at any depth will be classified by the subscriber class it extends:
AbstractDJSClientSubscriberfor Discord.js client events- AbstractCommandSubscriber docs for command manager events
AbstractServiceSubscriberfor bot service eventsAbstractPluginSubscriberfor plugin manager eventsAbstractBusSubscriberfor event bus eventsAbstractScheduleSubscriberfor schedule manager events
A recommended convention:
Usage
By default, events are automatically subscribed to the appropriate buses:
import { } from '@nyx-discord/loaders';
await .({ , : './dist/events' });Pass register: false to handle registration yourself:
import { } from '@nyx-discord/loaders';
const = await .({
,
: './dist/events',
: false,
});
await .(....);
await .().(....);
await .().(....);
await .().(....);
await .().(....);EventBus subscribers (events.bus) cannot be registered automatically, so you need to attach them manually
Writing subscribers for the loader
Any class extending one of the six abstract subscriber base classes will be detected:
import { , type } from '@nyx-discord/djs';
import type { } from 'discord.js';
export class extends <'ready'> {
protected readonly = 'ready' as ;
public (: , : <true>) {
.(`Bot logged in as ${..}`);
}
}Top-level subscribers are instantiated with new Class() (no arguments). Subscribers don't receive a parent instance.
Alternatively, you can specify a static create(bot) factory:
import { } from '@nyx-discord/djs';
import type { , } from '@nyx-discord/djs';
import type { } from 'discord.js';
export class extends <'ready'> {
protected readonly = 'ready' as ;
public static (: ) {
return new ();
}
// ...
}The loader will call this method instead of the constructor if it exists. This allows you to access the bot instance, or add logic, for example to inject services or other dependencies with a DI container.
Last updated on
