Skip to content

Commit 8e31c90

Browse files
authored
Merge pull request #258 from Nzii3/patch-1
Add autocomplete example
2 parents 0d4a3ba + 58cae22 commit 8e31c90

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

docs/interactions/application-commands/slash-commands.mdx

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,42 @@ bot.run("TOKEN")
213213
</TabItem>
214214
</Tabs>
215215

216+
## Autocomplete
217+
Discord's autocomplete allows developers to determine option choices that are used in a slash command option. By defining a function, you can do this.
218+
219+
```py
220+
async def get_animal_types(ctx: discord.AutocompleteContext):
221+
"""
222+
Here we will check if 'ctx.options['animal_type']' is and check if it's a marine or land animal and return specific option choices
223+
"""
224+
animal_type = ctx.options['animal_type']
225+
if animal_type == 'Marine':
226+
return ['Whale', 'Shark', 'Fish', 'Octopus', 'Turtle']
227+
else: # is land animal
228+
return ['Snake', 'Wolf', 'Lizard', 'Lion', 'Bird']
229+
230+
@bot.slash_command(name="animal")
231+
async def animal_command(ctx: discord.ApplicationContext, animal_type: discord.Option(str, choices=['Marine', 'Land']), animal: discord.Option(str, autocomplete=discord.utils.basic_autocomplete(get_animals)):
232+
await ctx.respond(f'You picked an animal type of `{animal_type}` that led you to pick `{animal}`!')
233+
```
234+
235+
<DiscordComponent>
236+
<DiscordMessage profile="robocord">
237+
<div slot="interactions">
238+
<DiscordInteraction profile="bob" command>
239+
animal
240+
</DiscordInteraction>
241+
</div>
242+
You picked an animal type of <code>Marine</code> that led you to pick <code>Shark</code>!
243+
</DiscordMessage>
244+
</DiscordComponent>
245+
246+
:::warning
247+
248+
Autocomplete can **only** be used with slash commands.
249+
250+
:::
251+
216252
:::info Related Topics
217253

218254
- [Interactions Index](../../interactions)

0 commit comments

Comments
 (0)