Skip to main content

Class: abstract 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.

Examples

// 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;
}
}
// 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;
}
}

Extends

  • Piece<Options, "listeners">

Type Parameters

Type ParameterDefault type
E extends keyof ClientEvents | symbol""
Options extends OptionsOptions

Constructors

new Listener()

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

Parameters

ParameterType
contextLoaderContext
optionsOptions

Returns

Listener<E, Options>

Overrides

`Piece< Options, 'listeners'

.constructor`

Defined in

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

Properties

emitter

readonly emitter: null | EventEmitter<DefaultEventMap>

The emitter, if any.

Since

2.0.0

Defined in

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


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


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

Methods

run()

abstract run(...args: E extends keyof ClientEvents ? ClientEvents[E<E>] : unknown[]): unknown

Parameters

ParameterType
...argsE extends keyof ClientEvents ? ClientEvents[E<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