Skip to main content

Class: abstract Listener<E, Options>

Defined in: projects/framework/src/lib/structures/Listener.ts:46

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>

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

Parameters

ParameterType
contextLoaderContext
optionsOptions

Returns

Listener<E, Options>

Overrides

Piece< Options, 'listeners' >.constructor

Properties

emitter

readonly emitter: null | EventEmitter

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

The emitter, if any.

Since

2.0.0


event

readonly event: string | symbol

Defined in: projects/framework/src/lib/structures/Listener.ts:60

The name of the event the listener listens to.

Since

2.0.0


once

readonly once: boolean

Defined in: projects/framework/src/lib/structures/Listener.ts:66

Whether the listener will be unloaded after the first run.

Since

2.0.0

Methods

run()

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

Defined in: projects/framework/src/lib/structures/Listener.ts:88

Parameters

ParameterType
...argsE extends keyof ClientEvents ? ClientEvents[E<E>] : unknown[]

Returns

unknown


toJSON()

toJSON(): ListenerJSON

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

Defines the JSON.stringify behavior of this piece.

Returns

ListenerJSON

Overrides

Piece.toJSON