# ButtonHandler

`class` in `seedcord` · v0.15.0

<https://docs.seedcord.org/packages/seedcord/0.15.0/classes/button-handler>

Base class for a button interaction handler.

Register the customId definitions this handler decodes with `@ButtonRoute`, list the same ones in the generic, then read `this.params` for a single route or `this.match` for several. Passing different definitions to the decorator and the generic is a compile error.

```ts
abstract class ButtonHandler<
    Defs extends readonly AnyCustomId[],
    Cache extends CacheType = "cached"
> extends ComponentHandler<ButtonInteraction<Cache>, Defs>
```

## Examples

```ts
\@ButtonRoute(ApproveId)
class ApproveButton extends ButtonHandler<[typeof ApproveId]> {
    async execute() {
        const { userId } = this.params;
        await this.event.reply(`approved <@${userId}>`);
    }
}
```

## Constructors

### constructor

```ts
InteractionHandler(event: Repliable, core: Core)
```

Constructs a new instance of the `InteractionHandler` class

## Properties

### core

```ts
readonly core: Core
```

### event

```ts
protected readonly event: ValidEvent
```

### 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.customId` once (cached after the first read) and throws `StaleCustomId` or `InvalidCustomId` when the wire no longer matches the current shape, which the controller boundary turns into a reply. On a handler registered for several routes this is `never`, so use [`match`](/packages/seedcord/0.15.0/classes/autocomplete-handler#match) instead.

## Methods

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

### 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 must cover every registered def, a missing prefix or an unknown key is a compile error. Decoding runs before any arm, so a stale or corrupt wire throws before an arm body executes.
