Skip to content

Commit beee733

Browse files
committed
add autocomplete example
1 parent e00b272 commit beee733

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import discord
2+
from discord.commands import slash_command, Option
3+
bot = discord.Bot()
4+
5+
COLORS = ["red", "orange", "yellow", "green", "blue", "indigo", "violet"]
6+
7+
8+
async def get_colors(ctx: discord.AutocompleteContext):
9+
return [color for color in COLORS if color.startswith(ctx.value.lower())]
10+
11+
12+
async def get_animals(ctx: discord.AutocompleteContext):
13+
picked_color = ctx.options["color"]
14+
if picked_color == "red":
15+
return ["cardinal", "ladybug"]
16+
elif picked_color == "orange":
17+
return ["clownfish", "tiger"]
18+
elif picked_color == "yellow":
19+
return ["goldfinch", "banana slug"]
20+
elif picked_color == "green":
21+
return ["tree frog", "python"]
22+
elif picked_color == "blue":
23+
return ["blue jay", "blue whale"]
24+
elif picked_color == "indigo":
25+
return ["eastern indigo snake"] # needs to return an iterable even if only one item
26+
elif picked_color == "violet":
27+
return ["purple emperor butterfly", "orchid dottyback"]
28+
else:
29+
return ["rainbowfish"]
30+
31+
32+
@slash_command(name="ac_colors")
33+
async def autocomplete_basic_example(ctx: discord.ApplicationContext, color: Option(str, "Pick a color!", autocomplete=get_colors), animal: Option(str, "Pick an animal!", autocomplete=get_animals)):
34+
await ctx.respond(f"You picked {color} for the color, which allowed you to choose {animal} for the animal.")

0 commit comments

Comments
 (0)