Class: Listener<E, O>
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.Context) {
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 = "" |
O | extends Options = Options |
Hierarchy
Piece
<O
>↳
Listener
Constructors
constructor
• new Listener<E
, O
>(context
, options?
)
Type parameters
Name | Type |
---|---|
E | extends symbol | keyof ClientEvents = "" |
O | extends ListenerOptions <O > = ListenerOptions |
Parameters
Name | Type |
---|---|
context | PieceContext |
options | O |
Overrides
Defined in
projects/framework/src/lib/structures/Listener.ts:73
Properties
_listener
• Private
_listener: null
| (...args
: any
[]) => void
Defined in
projects/framework/src/lib/structures/Listener.ts:71
emitter
• Readonly
emitter: null
| __module
The emitter, if any.
Since
2.0.0
Defined in
projects/framework/src/lib/structures/Listener.ts:57
enabled
• enabled: boolean
Whether or not the piece is enabled.
Inherited from
Defined in
node_modules/@sapphire/pieces/dist/index.d.ts:450
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:63
location
• Readonly
location: PieceLocation
The location metadata for the piece's file.
Inherited from
Defined in
node_modules/@sapphire/pieces/dist/index.d.ts:442
name
• Readonly
name: string
The name of the piece.
Inherited from
Defined in
node_modules/@sapphire/pieces/dist/index.d.ts:446
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:69
options
• Readonly
options: O
The raw options passed to this Piece
Inherited from
Defined in
node_modules/@sapphire/pieces/dist/index.d.ts:454
store
• store: ListenerStore
The ListenerStore that contains this Listener.
Overrides
Defined in
projects/framework/src/lib/structures/Listener.ts:51
Accessors
container
• get
container(): Container
A reference to the Container object for ease of use.
See
container
Returns
Container
Inherited from
Piece.container
Defined in
node_modules/@sapphire/pieces/dist/index.d.ts:460
Methods
_run
▸ Private
_run(...args
): Promise
<void
>
Parameters
Name | Type |
---|---|
...args | unknown [] |
Returns
Promise
<void
>
Defined in
projects/framework/src/lib/structures/Listener.ts:128
_runOnce
▸ Private
_runOnce(...args
): Promise
<void
>
Parameters
Name | Type |
---|---|
...args | unknown [] |
Returns
Promise
<void
>
Defined in
projects/framework/src/lib/structures/Listener.ts:133
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
Overrides
Defined in
projects/framework/src/lib/structures/Listener.ts:92
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
Overrides
Defined in
projects/framework/src/lib/structures/Listener.ts:105
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:478
run
▸ Abstract
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:90
toJSON
▸ toJSON(): ListenerJSON
Defines the JSON.stringify
behavior of this piece.
Returns
Overrides
Defined in
projects/framework/src/lib/structures/Listener.ts:120
unload
▸ unload(): Promise
<void
>
Unloads and disables the piece.
Returns
Promise
<void
>
Inherited from
Defined in
node_modules/@sapphire/pieces/dist/index.d.ts:474