|
| 1 | +from functools import partial |
| 2 | + |
1 | 3 | import discord
|
2 | 4 | from discord.commands import option
|
3 | 5 |
|
@@ -196,4 +198,39 @@ async def autocomplete_basic_example(
|
196 | 198 | await ctx.respond(f"You picked {color} as your color, and {animal} as your animal!")
|
197 | 199 |
|
198 | 200 |
|
| 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 | + |
199 | 236 | bot.run("TOKEN")
|
0 commit comments