Skip to content

Commit

Permalink
🚚 Add partial autocomplete example
Browse files Browse the repository at this point in the history
  • Loading branch information
Paillat-dev committed Dec 13, 2024
1 parent 7308d08 commit ebb1904
Showing 1 changed file with 55 additions and 0 deletions.
55 changes: 55 additions & 0 deletions examples/app_commands/slash_partial_autocomplete.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
from functools import partial
from os import getenv

from dotenv import load_dotenv

import discord
from discord.ext import commands

load_dotenv()

bot = discord.Bot()

fruits = ["Apple", "Banana", "Orange"]
vegetables = ["Carrot", "Lettuce", "Potato"]


async def food_autocomplete(
ctx: discord.AutocompleteContext, food_type: str
) -> list[discord.OptionChoice]:
items = fruits if food_type == "fruit" else vegetables
return [
discord.OptionChoice(name=item)
for item in items
if ctx.value.lower() in item.lower()
]


class FoodCog(commands.Cog):
@commands.slash_command(name="fruit")
async def get_fruit(
self,
ctx: discord.ApplicationContext,
choice: discord.Option(
str,
"Pick a fruit",
autocomplete=partial(food_autocomplete, food_type="fruit"),
),
):
await ctx.respond(f"You picked: {choice}")

@commands.slash_command(name="vegetable")
async def get_vegetable(
self,
ctx: discord.ApplicationContext,
choice: discord.Option(
str,
"Pick a vegetable",
autocomplete=partial(food_autocomplete, food_type="vegetable"),
),
):
await ctx.respond(f"You picked: {choice}")


bot.add_cog(FoodCog())
bot.run(getenv("TOKEN"))

0 comments on commit ebb1904

Please sign in to comment.