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

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
optionsO

Overrides

Piece.constructor

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

Piece.enabled

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

Piece.location

Defined in

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


name

Readonly name: string

The name of the piece.

Inherited from

Piece.name

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

Piece.options

Defined in

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


store

store: ListenerStore

The ListenerStore that contains this Listener.

Overrides

Piece.store

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

NameType
...argsunknown[]

Returns

Promise<void>

Defined in

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


_runOnce

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

Parameters

NameType
...argsunknown[]

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

Piece.onLoad

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

Piece.onUnload

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

Piece.reload

Defined in

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


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:90


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:120


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:474