Skip to content
Classgatewayv0.7.5

EventMiddleware

Base class for Discord event middleware.

Middleware runs before event handlers and can stop the event by throwing a Silence. Unlike EventHandler, it runs the SAME for every event it is registered for, so it has no match. Specify a single event in the generic and { events } to read this.event fully typed. Span several events (or omit { events } for a catchall) and this.event narrows to never, read this.eventName and do work that does not depend on the payload shape. A middleware that needs each event's payload is written one event per class.

abstract class EventMiddleware<
    EventName extends ValidNonInteractionKeys = ValidNonInteractionKeys
> extends BaseHandler<ClientEvents[EventName]>

Member visibility

Filter class members by access level.

constructor

Constructs a new instance of the EventMiddleware class

EventMiddleware(
    event: ClientEvents[EventName],
    core: Core,
    dispatch: DispatchContext,
    eventName?: EventName
)

Showing members with Protected visibility and higher.

dispatch

The bag for this dispatch. Each interaction gets its own. Every handler for one event shares a single bag.

protected readonly dispatch: DispatchContext

Inherited from:BaseHandler

eventName

The event that fired this middleware. Read it on a catchall or multi-event middleware, where this.event is never, to do work that does not depend on the payload shape. A single-event middleware reads this.event directly and does not need this.

protected eventName: EventName

after()

Runs once the event finishes, newest middleware first. Every middleware whose execute() started gets the call, a stopped chain and a throw included. Implement it to release something this middleware took in execute(). A throw in here is logged and goes no further.

public async after?(result: EventDispatchResult): Promise<void>
Parameter: result

outcome reports the chain alone. handlers holds one entry per handler that ran, empty exactly when the chain stopped the event.

execute()

Holds the main logic of your handler. The dispatcher calls it after the handler's gates pass, so a gate that refuses stops execute() from running.

abstract execute(): Promise<void>

Inherited from:BaseHandler

getEvent()

getEvent(): ValidEvent

Inherited from:BaseHandler