Skip to main content

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

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

Hierarchy

  • Piece<Options, "listeners">

    Listener

Constructors

constructor

new Listener<E, Options>(context, options?): Listener<E, Options>

Type parameters

NameType
Eextends symbol | keyof ClientEvents = ""
Optionsextends ListenerOptions = ListenerOptions

Parameters

NameType
contextLoaderContext
optionsOptions

Returns

Listener<E, Options>

Overrides

Piece.constructor

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

Piece.enabled

Defined in

node_modules/@sapphire/pieces/dist/esm/index.d.mts:234


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

Piece.location

Defined in

node_modules/@sapphire/pieces/dist/esm/index.d.mts:226


name

Readonly name: string

The name of the piece.

Inherited from

Piece.name

Defined in

node_modules/@sapphire/pieces/dist/esm/index.d.mts:230


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

Piece.options

Defined in

node_modules/@sapphire/pieces/dist/esm/index.d.mts:238


store

Readonly store: ListenerStore

The store that contains the piece.

Inherited from

Piece.store

Defined in

node_modules/@sapphire/pieces/dist/esm/index.d.mts:222

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/esm/index.d.mts:244

Methods

_run

_run(...args): Promise<void>

Parameters

NameType
...argsunknown[]

Returns

Promise<void>

Defined in

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


_runOnce

_runOnce(...args): Promise<void>

Parameters

NameType
...argsunknown[]

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

Piece.onLoad

Defined in

node_modules/@sapphire/pieces/dist/esm/index.d.mts:249


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

Piece.onUnload

Defined in

node_modules/@sapphire/pieces/dist/esm/index.d.mts:254


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/esm/index.d.mts:262


run

run(...args): unknown

Parameters

NameType
...argsE 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

ListenerJSON

Overrides

Piece.toJSON

Defined in

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


unload

unload(): Promise<void>

Unloads and disables the piece.

Returns

Promise<void>

Inherited from

Piece.unload

Defined in

node_modules/@sapphire/pieces/dist/esm/index.d.mts:258