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 parameterValue
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`

Source

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

Properties

_listener

private _listener: null | (...args: any[]) => void

Source

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


emitter

readonly emitter: null | __module

The emitter, if any.

Since

2.0.0

Source

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

Source

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

Source

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

Methods

_run()

private _run(...args: unknown[]): Promise<void>

Parameters

ParameterType
...argsunknown[]

Returns

Promise<void>

Source

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


_runOnce()

private _runOnce(...args: unknown[]): Promise<void>

Parameters

ParameterType
...argsunknown[]

Returns

Promise<void>

Source

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


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

Source

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


toJSON()

toJSON(): ListenerJSON

Returns

ListenerJSON

Overrides

Piece.toJSON

Source

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