# AutocompleteHandler

`class` in `gateway` · v0.9.0

<https://seedcord.org/docs/packages/gateway/0.9.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 read the selected command's path from `this.route`.

```ts
abstract class AutocompleteHandler<
    Route extends keyof SlashRegistry,
    Cache extends CacheType = CacheFor<Route>
> extends BaseHandler<AutocompleteInteraction<Cache>>
```

## 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,
    dispatch: DispatchContext,
    responder: AutocompleteResponder<ApplicationCommandOptionChoiceData>
)
```

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

### focused

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

The focused option for this interaction. `value` is always the raw partial string the user is typing,
even for an integer or number option, so coerce it yourself when you need a number.

### 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. The focused field is on `this.focused`.

### route

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

The selected command's path, such as `search/docs`, without the interaction-kind prefix.

### user

```ts
protected user: User
```

The user who sent this interaction.

## Methods

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

### getEvent

```ts
getEvent(): ValidEvent
```

### match

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

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

Provide one case per autocompletable field across the registered commands, keyed by field name. Each
case receives the focused partial value and a `respond` typed to that field's choice value. The
cases must cover every autocompletable field. A missing field or an unknown key is a compile error.

### respond

```ts
protected async respond(
    choices: readonly ApplicationCommandOptionChoiceData[]
): Promise<void>
```

Send autocomplete suggestions, callback type 8. Prefer [`match`](/docs/packages/gateway/0.9.0/classes/autocomplete-handler#match), which restricts each field's choices to its declared type.
