# SlashHandler

`class` in `gateway` · v0.9.0

<https://seedcord.org/docs/packages/gateway/0.9.0/classes/slash-handler>

Base class for a chat-input (slash) command handler.

Pass the route from the generated registry as the generic, the same string as `@SlashRoute`, then read
`this.options` for a single command or `this.match` for several. Command authoring stays plain
discord.js. `seedcord codegen` reads its `toJSON()` to type these options.

```ts
abstract class SlashHandler<
    Route extends keyof SlashRegistry,
    Cache extends CacheType = CacheFor<Route>
> extends InteractionHandler<ChatInputCommandInteraction<Cache>>
```

## Examples

```ts
\@SlashRoute('ban')
class BanHandler extends SlashHandler<'ban'> {
    async execute() {
        const target = this.options.getUser('target'); // User
        const reason = this.options.getString('reason'); // string | null
    }
}
```

## Constructors

### constructor

```ts
InteractionHandler(
    event: Repliable,
    core: Core,
    dispatch: DispatchContext,
    sender: ReplySender
)
```

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

### logger

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

### options

```ts
protected options: SlashOptions<Route, Cache>
```

The typed options for this command's route. Only the getters for kinds this command declares appear.
Each getter's name argument is restricted to that command's options of its kind.

- A required option's getter never returns `null`.

- Choices narrow to their literal union.

- A channel option declared with `addChannelTypes` narrows to those types.

Use `this.event.options` directly for anything outside this view, such as `getSubcommand()`.

### route

```ts
protected readonly route: Route
```

The selected command's path, such as `search/docs`.

### sender

```ts
readonly sender: TSender
```

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

### user

```ts
protected user: User
```

The user who sent this interaction.

## Methods

### defer

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

Show a "thinking" placeholder, then fill it later with [`edit`](/docs/packages/gateway/0.9.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.

### match

```ts
protected async match<Ret, Cases>(
    cases: OnlyCases<Cases, SlashMatchCases<Route, Cache, Ret>>
): Promise<MatchResult<Ret, Cases>>
```

Run the case for the selected command. Use this only when the handler is registered for several
routes.

Provide one case per registered route, keyed by its route string, and each case receives that route's
own narrowed options. The cases must cover every route in the generic. A missing route or an unknown
key is a compile error.

### 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<NonModalInteraction>,
    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).
