# EventMiddleware

`class` in `gateway` · v0.9.0

<https://seedcord.org/docs/packages/gateway/0.9.0/classes/event-middleware>

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. If you span several events (or omit `{ events }` for
a catchall), `this.event` narrows to `never`. Read `this.route` there, for work that doesn't depend on the
payload shape. Write one class per event for a middleware that needs each event's payload.

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

## Constructors

### constructor

```ts
EventMiddleware(
    event: ClientEvents[EventName],
    core: Core,
    dispatch: DispatchContext
)
```

Constructs a new instance of the `EventMiddleware` class

## Properties

### core

```ts
readonly core: TCore
```

### dispatch

```ts
protected readonly dispatch: DispatchContext
```

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

### event

```ts
protected readonly event: SingleEventPayload<EventName>
```

### logger

```ts
protected readonly logger: Logger
```

### route

```ts
protected readonly route: EventName
```

The event that started this dispatch, one of the names in the generic. Read it on a catchall or multi-event
middleware, where `this.event` is `never`, to do work that doesn't depend on the payload shape.

## Methods

### after

```ts
public after?(result: EventDispatchResult): Promisable<void>
```

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.

### execute

```ts
abstract execute(): Promisable<void>
```

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.

### getEvent

```ts
getEvent(): ValidEvent
```
