# ModalHandler

`class` in `http` · v0.5.1

<https://docs.seedcord.org/packages/http/0.5.1/classes/modal-handler>

Base class for a modal submit handler on the HTTP transport.

Register the customId definitions this handler decodes with `@ModalRoute` and list the same ones in the generic. Read the submitted inputs from `this.fields`. Reply through the handler members. `showModal` fails compilation on this kind, because Discord forbids opening a modal in response to a modal.

```ts
abstract class ModalHandler<
    Defs extends readonly AnyCustomId[]
> extends ComponentHandler<APIModalSubmitInteraction, Defs>
```

## Examples

```ts
\@ModalRoute(ConfigId)
class ConfigModal extends ModalHandler<[typeof ConfigId]> {
    async execute() {
        const { guildId } = this.params;
        const name = this.fields.getTextInputValue('name');
        await this.reply(`saved ${name} for ${guildId}`);
    }
}
```

## Constructors

### constructor

```ts
InteractionHandler(
    event: Event,
    core: Core,
    dispatch?: DispatchContext
)
```

Constructs a new instance of the `InteractionHandler` 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 | undefined
```

### event

```ts
protected readonly event: Event
```

### fields

```ts
protected fields: ModalFields
```

The fields this modal submitted, read by custom id.

### logger

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

### params

```ts
protected params: SingleParams<Defs>
```

The decoded params of the single route this handler is registered for.

Reading this decodes `this.event.data.custom_id` once (cached after the first read) and throws `StaleCustomId` or `InvalidCustomId` when the wire no longer matches the current shape, which the dispatcher boundary turns into a reply. On a handler registered for several routes this is `never`, so use [`match`](/packages/http/0.5.1/classes/autocomplete-handler#match) instead.

### routeId

```ts
protected readonly routeId: string
```

### sender

```ts
readonly sender: TSender
```

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

## Methods

### buildSender

```ts
protected buildSender(
    event: Event,
    core: Core,
    routeId: string
): ReplySender
```

### defer

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

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

### deferUpdate

```ts
protected async deferUpdate(): Promise<void>
```

Acknowledge the submit without changing the source message. Throws when a command opened the modal.

### 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 async execute(): Promise<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.

### match

```ts
protected async match<Ret>(arms: MatchArms<Defs, Ret>): Promise<Ret>
```

Run the arm for whichever route the component was minted from. Use this only when the handler is registered for several routes. A single-route handler reads `this.params` directly. On a multi-route handler `this.params` is `never`, so match is the only way to read the decoded params.

Provide one arm per registered route, keyed by its prefix, and each arm receives that route's own decoded params. The arms cover every registered route prefix, checked at compile time, and a prefix unmatched at runtime throws `CustomIdMatchArmMissing`. Decoding runs before any arm, so a stale or corrupt wire throws before an arm body executes.

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

### showModal

```ts
protected async showModal(
    this: InteractionHandler<
        Exclude<Repliables, APIModalSubmitInteraction>
    >,
    modal: ModalLike
): Promise<void>
```

Open a modal. Must be the initial response to this interaction. The modal kind rejects this call at compile time (Discord forbids a modal in response to a modal).

### update

```ts
protected async update(
    response: ReplyResponse | string
): Promise<SentMessage>
```

Rewrite the message this modal was opened from. Throws when a command opened the modal.
