Class: 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.
Example
// 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;
}
}
Example
// 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;
}
}
Type parameters
Name | Type |
---|---|
E | extends keyof ClientEvents | symbol = "" |
Options | extends Options = Options |
Hierarchy
Constructors
constructor
• new Listener<E
, Options
>(context
, options?
): Listener
<E
, Options
>
Type parameters
Name | Type |
---|---|
E | extends symbol | keyof ClientEvents = "" |
Options | extends ListenerOptions = ListenerOptions |
Parameters
Name | Type |
---|---|
context | LoaderContext |
options | Options |
Returns
Overrides
Defined in
projects/framework/src/lib/structures/Listener.ts:70
Properties
_listener
• Private
_listener: null
| (...args
: any
[]) => void
Defined in
projects/framework/src/lib/structures/Listener.ts:68
emitter
• Readonly
emitter: null
| __module
The emitter, if any.
Since
2.0.0
Defined in
projects/framework/src/lib/structures/Listener.ts:54
enabled
• enabled: boolean
Whether or not the piece is enabled.
Inherited from
Defined in
node_modules/@sapphire/pieces/dist/index.d.ts:193
event
• Readonly
event: string
| symbol
The name of the event the listener listens to.
Since
2.0.0
Defined in
projects/framework/src/lib/structures/Listener.ts:60
location
• Readonly
location: PieceLocation
The location metadata for the piece's file.
Inherited from
Defined in
node_modules/@sapphire/pieces/dist/index.d.ts:185
name
• Readonly
name: string
The name of the piece.
Inherited from
Defined in
node_modules/@sapphire/pieces/dist/index.d.ts:189
once
• Readonly
once: boolean
Whether the listener will be unloaded after the first run.
Since
2.0.0
Defined in
projects/framework/src/lib/structures/Listener.ts:66
options
• Readonly
options: Options
The raw options passed to this Piece
Inherited from
Defined in
node_modules/@sapphire/pieces/dist/index.d.ts:197
store
• Readonly
store: ListenerStore
The store that contains the piece.
Inherited from
Defined in
node_modules/@sapphire/pieces/dist/index.d.ts:181
Accessors
container
• get
container(): Container
A reference to the Container object for ease of use.
Returns
Container
See
container
Inherited from
Piece.container
Defined in
node_modules/@sapphire/pieces/dist/index.d.ts:203
Methods
_run
▸ _run(...args
): Promise
<void
>
Parameters
Name | Type |
---|---|
...args | unknown [] |
Returns
Promise
<void
>
Defined in
projects/framework/src/lib/structures/Listener.ts:98
_runOnce
▸ _runOnce(...args
): Promise
<void
>
Parameters
Name | Type |
---|---|
...args | unknown [] |
Returns
Promise
<void
>
Defined in
projects/framework/src/lib/structures/Listener.ts:103
onLoad
▸ onLoad(): unknown
Per-piece listener that is called when the piece is loaded into the store. Useful to set-up asynchronous initialization tasks.
Returns
unknown
Inherited from
Defined in
node_modules/@sapphire/pieces/dist/index.d.ts:208
onUnload
▸ onUnload(): unknown
Per-piece listener that is called when the piece is unloaded from the store. Useful to set-up clean-up tasks.
Returns
unknown
Inherited from
Defined in
node_modules/@sapphire/pieces/dist/index.d.ts:213
reload
▸ reload(): Promise
<void
>
Reloads the piece by loading the same path in the store.
Returns
Promise
<void
>
Inherited from
Defined in
node_modules/@sapphire/pieces/dist/index.d.ts:221
run
▸ run(...args
): unknown
Parameters
Name | Type |
---|---|
...args | E extends keyof ClientEvents ? ClientEvents [E ] : unknown [] |
Returns
unknown
Defined in
projects/framework/src/lib/structures/Listener.ts:88
toJSON
▸ toJSON(): ListenerJSON
Defines the JSON.stringify
behavior of this piece.
Returns
Overrides
Defined in
projects/framework/src/lib/structures/Listener.ts:90
unload
▸ unload(): Promise
<void
>
Unloads and disables the piece.
Returns
Promise
<void
>
Inherited from
Defined in
node_modules/@sapphire/pieces/dist/index.d.ts:217