Class: abstract Listener<E, Options>
The base event class. This class is abstract and is to be extended by subclasses, which should implement the methods. In Sapphire's workflow, listeners are called when the emitter they listen on emits a new message with the same event name.
Examples
// TypeScript:
import { Events, Listener } from '@sapphire/framework';
// Define a class extending `Listener`, then export it.
// NOTE: You can use `export default` or `export =` too.
export class CoreListener extends Listener<typeof Events.ClientReady> {
public constructor(context: Listener.LoaderContext) {
super(context, { event: Events.ClientReady, once: true });
}
public run() {
this.container.client.id ??= this.container.client.user?.id ?? null;
}
}
// JavaScript:
const { Events, Listener } = require('@sapphire/framework');
// Define a class extending `Listener`, then export it.
module.exports = class CoreListener extends Listener {
constructor(context) {
super(context, { event: Events.ClientReady, once: true });
}
run() {
this.container.client.id ??= this.container.client.user?.id ?? null;
}
}
Extends
Piece<Options,"listeners">
Type parameters
| Type parameter | Value |
|---|---|
E extends keyof ClientEvents | symbol | "" |
Options extends Options | Options |
Constructors
new Listener()
new Listener<
E,Options>(context:LoaderContext,options:Options):Listener<E,Options>
Parameters
| Parameter | Type |
|---|---|
context | LoaderContext |
options | Options |
Returns
Listener<E, Options>
Overrides
`Piece< Options, 'listeners'
.constructor`
Source
projects/framework/src/lib/structures/Listener.ts:70
Properties
_listener
private_listener:null| (...args:any[]) =>void
Source
projects/framework/src/lib/structures/Listener.ts:68
emitter
readonlyemitter:null|__module
The emitter, if any.
Since
2.0.0
Source
projects/framework/src/lib/structures/Listener.ts:54
event
readonlyevent:string|symbol
The name of the event the listener listens to.
Since
2.0.0
Source
projects/framework/src/lib/structures/Listener.ts:60
once
readonlyonce:boolean
Whether the listener will be unloaded after the first run.
Since
2.0.0
Source
projects/framework/src/lib/structures/Listener.ts:66
Methods
_run()
private_run(...args:unknown[]):Promise<void>
Parameters
| Parameter | Type |
|---|---|
...args | unknown[] |
Returns
Promise<void>
Source
projects/framework/src/lib/structures/Listener.ts:98
_runOnce()
private_runOnce(...args:unknown[]):Promise<void>
Parameters
| Parameter | Type |
|---|---|
...args | unknown[] |
Returns
Promise<void>
Source
projects/framework/src/lib/structures/Listener.ts:103
run()
abstractrun(...args:Eextends keyofClientEvents?ClientEvents[E<E>] :unknown[]):unknown
Parameters
| Parameter | Type |
|---|---|
...args | E extends keyof ClientEvents ? ClientEvents[E<E>] : unknown[] |
Returns
unknown
Source
projects/framework/src/lib/structures/Listener.ts:88
toJSON()
toJSON():
ListenerJSON
Returns
Overrides
Piece.toJSON