💼 Event Manager
The EventManager
is the object that holds together the nyx event system.
It consists of:
- The
EventBus
collection for storing buses, mapped by their IDs. - An
EventBus
for Bot events. - An
EventBus
for Client events. - An
EventBus
for Manager events.
As well as methods to interact with them.
👷 Creation
You can create a custom event manager by either:
- Extending
DefaultEventManager
from@nyx-discord/framework
(recommended). - Implementing
EventManager
from@nyx-discord/core
.
Then you can pass it to your bot:
class MyEventManager extends DefaultEventManager {
public myCustomPublicMethod() {}
}
const myBot = Bot.create((bot: NyxBot) => ({
// ...
events: new MyEventManager(/** ... */),
}));
myBot.getEventManager().myCustomPublicMethod() // Works!
The Bot
class is able to infer the type of your custom manager via generics, so accessing any custom public method or
property will work without errors.
👂 Client event bus
The manager holds a EventBus
that wraps the Discord.js Client
, for client events. You can get it with
EventManager#getClientBus()
, and you can directly subscribe to it with EventManager#subscribeClient()
.
You can use the utility abstraction on @nyx-discord/framework
for a subscriber for a Client,
the AbstractDJSClientSubscriber
. It receives a generic which is the handled event, where you can use the
discord.js Event
enum. Said value will be used to safely type the arguments of the event handler.
import { Events } from 'discord.js';
import { AbstractDJSClientSubscriber } from '@nyx-discord/framework';
class InteractionCreateSubscriber
extends AbstractDJSClientSubscriber<Events.InteractionCreate> {
protected override readonly event = Events.InteractionCreate;
public handleEvent(
meta: EventDispatchMeta,
interaction: Interaction,
): void {
// Since the built-in client event bus has a bot, this will never throw an error.
const bot = meta.getBot(true);
bot.getLogger().info(`Interaction ${interaction.id} received.`);
}
}
const subscriber = new InteractionCreateSubscriber();
await bot.getEventManager().subscribeClient(subscriber);
// same as:
const bus = bot.getEventManager().getClientBus();
await bus.subscribe(subscriber);