Skip to content

Commit eb43fa3

Browse files
authored
refactor(autocomplete): Update getFocused() section (#1594)
1 parent 885b364 commit eb43fa3

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

Diff for: guide/slash-commands/autocomplete.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ The <DocsLink path="AutocompleteInteraction:Class"/> class provides the <DocsLin
9797
Unlike static choices, autocompletion suggestions are *not* enforced, and users may still enter free text.
9898
:::
9999

100-
The <DocsLink path="CommandInteractionOptionResolver:Class#getFocused" type="method"/> method returns the currently focused option's value, which can be used to apply filtering to the choices presented. For example, to only display options starting with the focused value you can use the `Array#filter()` method, then using `Array#map()`, you can transform the array into an array of <DocsLink path="ApplicationCommandOptionChoiceData:Interface" /> objects.
100+
The <DocsLink path="CommandInteractionOptionResolver:Class#getFocused" type="method"/> method returns the currently focused option, which can be used to apply filtering to the choices presented. For example, to only display options starting with the focused value, you can use the `Array#filter()` method. Then, using `Array#map()`, you can transform the array into an array of <DocsLink path="ApplicationCommandOptionChoiceData:Interface" /> objects.
101101

102102
```js {10-15}
103103
module.exports = {
@@ -109,7 +109,7 @@ module.exports = {
109109
.setDescription('Phrase to search for')
110110
.setAutocomplete(true)),
111111
async autocomplete(interaction) {
112-
const focusedValue = interaction.options.getFocused();
112+
const focusedValue = interaction.options.getFocused().value;
113113
const choices = ['Popular Topics: Threads', 'Sharding: Getting started', 'Library: Voice Connections', 'Interactions: Replying to slash commands', 'Popular Topics: Embed preview'];
114114
const filtered = choices.filter(choice => choice.startsWith(focusedValue));
115115
await interaction.respond(
@@ -121,7 +121,7 @@ module.exports = {
121121

122122
### Handling multiple autocomplete options
123123

124-
To distinguish between multiple options, you can pass `true` into <DocsLink path="CommandInteractionOptionResolver:Class#getFocused" type="method"/>, which will now return the full focused object instead of just the value. This is used to get the name of the focused option below, allowing for multiple options to each have their own set of suggestions:
124+
By making use of `name`, you can distinguish between multiple options and provide different suggestions for each:
125125

126126
```js {10-19}
127127
module.exports = {
@@ -137,7 +137,7 @@ module.exports = {
137137
.setDescription('Version to search in')
138138
.setAutocomplete(true)),
139139
async autocomplete(interaction) {
140-
const focusedOption = interaction.options.getFocused(true);
140+
const focusedOption = interaction.options.getFocused();
141141
let choices;
142142

143143
if (focusedOption.name === 'query') {

0 commit comments

Comments
 (0)