Skip to content

Commit 4884fbe

Browse files
committed
add options autocomplete docs
1 parent fa300e1 commit 4884fbe

File tree

1 file changed

+97
-0
lines changed

1 file changed

+97
-0
lines changed
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,100 @@
11
---
22
title: Options autocomplete
33
---
4+
5+
:::warning
6+
7+
This feature is only available for chat input (slash) commands.
8+
9+
:::
10+
11+
In your chat input command options, autocomplete allows users on
12+
Discord to see a list of suggested options based on what they're
13+
actively typing.
14+
15+
## Autocomplete with CommandKit
16+
17+
In addition to registering your application commands, CommandKit can
18+
also handle any options you mark as 'autocomplete'. This means that
19+
CommandKit will automatically identify which command the autocomplete
20+
interaction belongs to. This will save you the hassle of registering
21+
and managing your own `interactionCreate` event listener.
22+
23+
To begin, export the `autocomplete` function from the command where
24+
you have an option with `autocomplete` set to `true`.
25+
26+
```ts title="src/app/commands/pet.ts"
27+
import type { CommandData, AutocompleteCommand } from 'commandkit';
28+
import { ApplicationCommandOptionType } from 'discord.js';
29+
30+
export const command: CommandData = {
31+
name: 'pet',
32+
description: 'Check in one of your pets.',
33+
options: [
34+
{
35+
name: 'name',
36+
description: 'The name of your pet',
37+
type: ApplicationCommandOptionType.String,
38+
autocomplete: true,
39+
required: true,
40+
},
41+
],
42+
};
43+
44+
const pets = Array.from({ length: 20 }, (_, i) => ({
45+
name: `Pet ${i + 1}`, // e.g. Pet 1, Pet 2, Pet 3, etc.
46+
value: `petId_${i}`,
47+
}));
48+
49+
export const autocomplete: AutocompleteCommand = async ({
50+
interaction,
51+
}) => {
52+
try {
53+
// Get the input value of your autocomplete option
54+
const input = interaction.options.getString('name', false);
55+
if (!input) {
56+
interaction.respond(pets);
57+
return;
58+
}
59+
60+
const filteredPets = pets.filter((pet) =>
61+
pet.name.toLowerCase().includes(input.toLowerCase()),
62+
);
63+
64+
// Respond with what you think the user may trying to find
65+
interaction.respond(filteredPets);
66+
} catch (error) {
67+
console.error('Autocomplete error', error);
68+
}
69+
};
70+
71+
export const chatInput: ChatInputCommand = async ({
72+
interaction,
73+
}) => {
74+
const chosenPetId = interaction.options.getString('name', true);
75+
const chosenPet = pets.find((pet) => pet.value === chosenPetId);
76+
77+
if (!chosenPet) {
78+
interaction.reply('Pet not found.');
79+
return;
80+
}
81+
82+
interaction.reply(`You chose ${chosenPet.name}`);
83+
};
84+
```
85+
86+
:::warning
87+
88+
The Discord API only allows a maximum of 25 autocomplete options per
89+
response. To ensure you don't run into errors, it's highly recommended
90+
to manually limit your response array to this amount.
91+
92+
:::
93+
94+
:::tip
95+
96+
It's highly recommended to use try/catch blocks in your `autocomplete`
97+
function as it's very common for autocomplete responses to run into
98+
errors, as they may take longer due to their dynamic nature.
99+
100+
:::

0 commit comments

Comments
 (0)