# InteractionMiddleware

`class` in `http` · v0.12.0

<https://seedcord.org/docs/packages/http/0.12.0/classes/interaction-middleware>

Base class for interaction middleware on the HTTP transport.

Middleware runs after the dispatcher builds the handler and before the gates, over the handler's own
reply surface. Name the kinds in the generic and list the same ones in `{ kinds }`. Several kinds give
`this.event` a union. Narrow it on `type` and `data.component_type`. A catchall reads every repliable
kind.

```ts
abstract class InteractionMiddleware<
    Kind extends MiddlewareKind = MiddlewareKind
> extends RepliableHandler<InteractionOf<Kind>>
```

## Examples

```ts
\@RegisterInteractionMiddleware({ kinds: [InteractionKind.Button] })
class Audit extends InteractionMiddleware<InteractionKind.Button> {
    async execute() {
        this.logger.info(this.event.data.custom_id);
    }
}
```

## Constructors

### constructor

```ts
InteractionMiddleware(
    event: InteractionOf<Kind>,
    core: Core,
    dispatch: DispatchContext,
    sender: ReplySender
)
```

Constructs a new instance of the `InteractionMiddleware` class

## Properties

### api

```ts
protected api: API
```

Typed Discord REST API (`@discordjs/core/http-only`) over `core.rest`, one instance per core.
The interaction callbacks on `api.interactions` bypass the reply surface. The
`no-raw-interaction-acks` rule flags them in handler classes.

### 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: Event
```

### logger

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

### route

```ts
protected readonly route: MiddlewareRoute<Kind>
```

What started this dispatch: a slash command path like `config/set`, a context menu command's
name, or a custom-ID prefix. When the generic lists only slash or context menu kinds, your editor
offers your registered commands. It can still be any string, since middleware also runs when no
handler matched, like on a deleted command that Discord still shows. For a custom ID that seedcord
didn't make, it can be empty or a cut-off piece of that ID.

### sender

```ts
readonly sender: TSender
```

The reply surface for this interaction. Pass the handler to reply from outside its class.

### user

```ts
protected user: APIUser
```

The user who sent this interaction, read from `member.user` in a guild and `user` in a DM.

## Methods

### after

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

Runs once the handler settles, newest middleware first. A refusal or a throw still reaches it,
since seedcord calls it on every middleware whose `execute()` started. Implement it to release
something this middleware took in `execute()`, such as a lock or an open span. A throw in here
is logged and goes no further.

### defer

```ts
protected async defer(opts?: DeferOpts): Promise<void>
```

Show a "thinking" placeholder, then fill it later with [`edit`](/docs/packages/http/0.12.0/classes/user-menu-handler#edit).

### delete

```ts
protected async delete(target?: TMessage): Promise<void>
```

Delete the initial reply or deferred placeholder. Pass a target to delete a message a prior send returned.

### edit

```ts
protected async edit(
    response: ReplyResponse<TNative> | string
): Promise<TMessage>
```

```ts
protected async edit(
    target: TMessage,
    response: ReplyResponse<TNative> | string
): Promise<TMessage>
```

Rewrite the initial reply or deferred placeholder. The targeted form rewrites a message a prior send returned.

### 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.

### followUp

```ts
protected async followUp(
    response: ReplyResponse<TNative> | string,
    opts?: SendOpts
): Promise<TMessage>
```

Send a new message once the interaction is acknowledged.

### reply

```ts
protected async reply(
    response: ReplyResponse<TNative> | string,
    opts?: SendOpts
): Promise<TMessage>
```

Send the initial response, exactly once.

### send

```ts
protected async send(
    response: ReplyResponse<TNative> | string,
    opts?: SendOpts
): Promise<TMessage>
```

Routes to reply, followUp, or edit based on the current ack state.
