# AutocompleteHandler

`class` in `seedcord` · v0.14.0

<https://docs.seedcord.org/packages/seedcord/0.14.0/classes/autocomplete-handler>

Base class for a Discord autocomplete handler.

Pass the command route(s) from the generated registry as the generic, the same string(s) as `@AutocompleteRoute`. Branch on the focused field with `this.match`, read already-entered sibling options with `this.options`, and find which command fired with `this.route`.

```ts
abstract class AutocompleteHandler<
    Route extends keyof SlashOptionRegistry,
    Cache extends CacheType = "cached"
>
    extends BaseHandler<AutocompleteInteraction<Cache>>
    implements Handler
```

## Examples

```ts
\@AutocompleteRoute('search')
class SearchAutocomplete extends AutocompleteHandler<'search'> {
    async execute() {
        await this.match({
            query: (value, respond) => respond([{ name: value, value }])
        });
    }
}
```

## Constructors

### constructor

```ts
AutocompleteHandler(
    event: AutocompleteInteraction<Cache>,
    core: Core
)
```

Constructs a new instance of the `AutocompleteHandler` class

## Properties

### core

```ts
readonly core: Core
```

### event

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

### focused

```ts
protected focused: FocusedField<Route>
```

### logger

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

### options

```ts
protected options: AutocompleteOptions<Route>
```

The already-entered options on this command, restricted to the kinds Discord resolves during autocomplete (string, integer, number, boolean) with every read nullable, since a sibling is partial while the user types the focused field. Read the focused field from `this.focused`, not here.

### route

```ts
protected route: Route
```

The firing command route, for a field whose completion differs per registered command.

## 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: FocusedArms<Route, Ret>
): Promise<Ret>
```

Run the arm for the focused field. An autocomplete always dispatches by which field is focused, so match is how you read it. There is no single-field shortcut the way slash/component handlers have one.

Provide one arm per autocompletable field across the registered commands, keyed by field name. Each arm receives the focused partial value and a `respond` pinned to that field's choice value type. The arms must cover every autocompletable field, a missing field or an unknown key is a compile error. A focused field with no arm, only reachable from a stale-deployed command, throws at runtime.
