Buses
Reading time:1 min read
The EventBus is the component that stores subscribers and allows events to be emitted to them.
When to override
| ✅ Override if you want to... | ❌ Don't override if you want to... |
|---|---|
| Wrap a new event source (e.g. Redis, WebSockets). | Change how subscribers are called (use a custom Dispatcher instead). |
| Change how subscribers are stored or retrieved. | Filter events globally (use a Middleware instead). |
Overriding
Creation
Create a custom event bus by either:
- Extending
BasicEventBus(recommended for general purpose). - Extending
BasicEventEmitterBus(recommended if wrapping a Node.jsEventEmitter). - Implementing
EventBusfrom scratch.
import { } from '@nyx-discord/djs';
class extends <{ : [string] }> {}Adapting external sources
If you are wrapping an external source, you can listen to it and call emit():
import { , , } from '@nyx-discord/djs';
import type { , MetadataFactory } from '@nyx-discord/djs';
// Mock external source
declare const : { : (: string, : (: string) => void) => void };
class extends <{ : [string] }> {
constructor(
: MetadataFactory = new (),
) {
super(
(, ) => .() - .(),
.(),
);
// Listen to external source and emit internally
.('message', () => {
this.('message', []);
});
}
}Edit on GitHub
Last updated on
