Skip to main content

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.Ready> {
public constructor(context: Listener.Context) {
super(context, { event: Events.Ready, 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.Ready, once: true });
}

run() {
this.container.client.id ??= this.container.client.user?.id ?? null;
}
}

Type parameters

NameType
Eextends keyof ClientEvents | symbol = ""
Oextends Options = Options

Hierarchy

Constructors

constructor

β€’ new Listener<E, O>(context, options?)

Type parameters

NameType
Eextends symbol | keyof ClientEvents = ""
Oextends ListenerOptions<O> = ListenerOptions

Parameters

NameType
contextPieceContext
optionsListenerOptions

Overrides

Piece.constructor

Defined in

projects/framework/src/lib/structures/Listener.ts:67

Properties

_listener

β€’ Private _listener: null | (...args: any[]) => void

Defined in

projects/framework/src/lib/structures/Listener.ts:65


emitter

β€’ Readonly emitter: null | EventEmitter

The emitter, if any.

since 2.0.0

Defined in

projects/framework/src/lib/structures/Listener.ts:51


enabled

β€’ enabled: boolean

Whether or not the piece is enabled.

Inherited from

Piece.enabled

Defined in

node_modules/@sapphire/pieces/dist/index.d.ts:447


event

β€’ Readonly event: string

The name of the event the listener listens to.

since 2.0.0

Defined in

projects/framework/src/lib/structures/Listener.ts:57


location

β€’ Readonly location: PieceLocation

The location metadata for the piece's file.

Inherited from

Piece.location

Defined in

node_modules/@sapphire/pieces/dist/index.d.ts:439


name

β€’ Readonly name: string

The name of the piece.

Inherited from

Piece.name

Defined in

node_modules/@sapphire/pieces/dist/index.d.ts:443


once

β€’ Readonly once: boolean

Whether or not the listener will be unloaded after the first run.

since 2.0.0

Defined in

projects/framework/src/lib/structures/Listener.ts:63


options

β€’ Readonly options: O

The raw options passed to this Piece

Inherited from

Piece.options

Defined in

node_modules/@sapphire/pieces/dist/index.d.ts:451


store

β€’ Readonly store: Store<Piece<PieceOptions>>

The store that contains the piece.

Inherited from

Piece.store

Defined in

node_modules/@sapphire/pieces/dist/index.d.ts:435

Accessors

container

β€’ get container(): Container

A reference to the {@link Container} object for ease of use.

see container

Returns

Container

Inherited from

Piece.container

Defined in

node_modules/@sapphire/pieces/dist/index.d.ts:457

Methods

_run

β–Έ Private _run(...args): Promise<void>

Parameters

NameType
...argsunknown[]

Returns

Promise<void>

Defined in

projects/framework/src/lib/structures/Listener.ts:122


_runOnce

β–Έ Private _runOnce(...args): Promise<void>

Parameters

NameType
...argsunknown[]

Returns

Promise<void>

Defined in

projects/framework/src/lib/structures/Listener.ts:130


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

Piece.onLoad

Defined in

projects/framework/src/lib/structures/Listener.ts:86


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

Piece.onUnload

Defined in

projects/framework/src/lib/structures/Listener.ts:99


reload

β–Έ reload(): Promise<void>

Reloads the piece by loading the same path in the store.

Returns

Promise<void>

Inherited from

Piece.reload

Defined in

node_modules/@sapphire/pieces/dist/index.d.ts:475


run

β–Έ Abstract run(...args): unknown

Parameters

NameType
...argsE extends keyof ClientEvents ? ClientEvents[E] : unknown[]

Returns

unknown

Defined in

projects/framework/src/lib/structures/Listener.ts:84


toJSON

β–Έ toJSON(): ListenerJSON

Defines the JSON.stringify behavior of this piece.

Returns

ListenerJSON

Overrides

Piece.toJSON

Defined in

projects/framework/src/lib/structures/Listener.ts:114


unload

β–Έ unload(): Promise<void>

Unloads and disables the piece.

Returns

Promise<void>

Inherited from

Piece.unload

Defined in

node_modules/@sapphire/pieces/dist/index.d.ts:471