Skip to content

Commit 4eeb9fd

Browse files
committed
♻️ Merge the 2 examples
1 parent 84f7383 commit 4eeb9fd

File tree

2 files changed

+37
-49
lines changed

2 files changed

+37
-49
lines changed

examples/app_commands/slash_autocomplete.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from functools import partial
2+
13
import discord
24
from discord.commands import option
35

@@ -196,4 +198,39 @@ async def autocomplete_basic_example(
196198
await ctx.respond(f"You picked {color} as your color, and {animal} as your animal!")
197199

198200

201+
FRUITS = ["Apple", "Banana", "Orange"]
202+
VEGETABLES = ["Carrot", "Lettuce", "Potato"]
203+
204+
205+
async def food_autocomplete(
206+
ctx: discord.AutocompleteContext, food_type: str
207+
) -> list[discord.OptionChoice]:
208+
items = FRUITS if food_type == "fruit" else VEGETABLES
209+
return [
210+
discord.OptionChoice(name=item)
211+
for item in items
212+
if ctx.value.lower() in item.lower()
213+
]
214+
215+
216+
@bot.slash_command(name="fruit")
217+
@option(
218+
"choice",
219+
"Pick a fruit",
220+
autocomplete=partial(food_autocomplete, food_type="fruit"),
221+
)
222+
async def get_fruit(self, ctx: discord.ApplicationContext, choice: str):
223+
await ctx.respond(f'You picked "{choice}"')
224+
225+
226+
@bot.slash_command(name="vegetable")
227+
@option(
228+
"choice",
229+
"Pick a vegetable",
230+
autocomplete=partial(food_autocomplete, food_type="vegetable"),
231+
)
232+
async def get_vegetable(self, ctx: discord.ApplicationContext, choice: str):
233+
await ctx.respond(f'You picked "{choice}"')
234+
235+
199236
bot.run("TOKEN")

examples/app_commands/slash_partial_autocomplete.py

Lines changed: 0 additions & 49 deletions
This file was deleted.

0 commit comments

Comments
 (0)