Skip to content

Commit 79e252f

Browse files
committed
add additional example for discord.utils.basic_autocomplete
1 parent c49c397 commit 79e252f

File tree

1 file changed

+105
-2
lines changed

1 file changed

+105
-2
lines changed

examples/app_commands/slash_autocomplete.py

Lines changed: 105 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,94 @@
44

55
COLORS = ["red", "orange", "yellow", "green", "blue", "indigo", "violet"]
66

7+
LOTS_OF_COLORS = [
8+
"aliceblue",
9+
"antiquewhite",
10+
"aqua",
11+
"aquamarine",
12+
"azure",
13+
"beige",
14+
"bisque",
15+
"blueviolet",
16+
"brown",
17+
"burlywood",
18+
"cadetblue",
19+
"cornflowerblue",
20+
"cornsilk",
21+
"crimson",
22+
"cyan",
23+
"darkblue",
24+
"deepskyblue",
25+
"dimgray",
26+
"dimgrey",
27+
"dodgerblue",
28+
"firebrick",
29+
"floralwhite",
30+
"forestgreen",
31+
"fuchsia",
32+
"gainsboro",
33+
"ghostwhite",
34+
"gold",
35+
"goldenrod",
36+
"gray",
37+
"green",
38+
"greenyellow",
39+
"grey",
40+
"honeydew",
41+
"hotpink",
42+
"indianred",
43+
"indigo",
44+
"ivory",
45+
"khaki",
46+
"lavender",
47+
"lavenderblush",
48+
"lawngreen",
49+
"lightcoral",
50+
"maroon",
51+
"mediumaquamarine",
52+
"mediumblue",
53+
"mediumorchid",
54+
"midnightblue",
55+
"navajowhite",
56+
"navy",
57+
"oldlace",
58+
"olive",
59+
"olivedrab",
60+
"orange",
61+
"orangered",
62+
"orchid",
63+
"palegoldenrod",
64+
"palegreen",
65+
"plum",
66+
"powderblue",
67+
"purple",
68+
"red",
69+
"rosybrown",
70+
"royalblue",
71+
"saddlebrown",
72+
"sienna",
73+
"springgreen",
74+
"steelblue",
75+
"tan",
76+
"teal",
77+
"thistle",
78+
"tomato",
79+
"turquoise",
80+
"violet",
81+
"wheat",
82+
"white",
83+
"whitesmoke",
84+
"yellow",
85+
"yellowgreen",
86+
]
87+
88+
BASIC_ALLOWED = [123, 444, 55782] # this would be a list of discord user IDs
89+
90+
91+
# Simple example of using logic in a basic_autocomplete callback. In this case, we're only returning any results if the user's ID exists in the BASIC_ALLOWED list
92+
async def color_searcher(ctx: discord.AutocompleteContext):
93+
return [color for color in LOTS_OF_COLORS if ctx.interaction.user.id in BASIC_ALLOWED]
94+
795

896
async def get_colors(ctx: discord.AutocompleteContext):
997
return [color for color in COLORS if color.startswith(ctx.value.lower())]
@@ -29,6 +117,21 @@ async def get_animals(ctx: discord.AutocompleteContext):
29117
return ["rainbowfish"]
30118

31119

32-
@slash_command(name="ac_colors")
33-
async def autocomplete_example(ctx: discord.ApplicationContext, color: Option(str, "Pick a color!", autocomplete=get_colors), animal: Option(str, "Pick an animal!", autocomplete=get_animals)):
120+
@slash_command(name="ac_example", guild_ids=[...])
121+
async def autocomplete_example(
122+
ctx: discord.ApplicationContext,
123+
color: Option(str, "Pick a color!", autocomplete=get_colors),
124+
animal: Option(str, "Pick an animal!", autocomplete=get_animals),
125+
):
34126
await ctx.respond(f"You picked {color} for the color, which allowed you to choose {animal} for the animal.")
127+
128+
129+
@slash_command(name="ac_basic_example", guild_ids=[...])
130+
async def autocomplete_basic_example(
131+
ctx: discord.ApplicationContext,
132+
color: Option(str, "Pick a color from this big list", autocomplete=discord.utils.basic_autocomplete(color_searcher)),
133+
animal: Option(str, "Pick an animal from this small list", autocomplete=discord.utils.basic_autocomplete(["snail", "python", "cricket", "orca"])),
134+
):
135+
await ctx.respond(f"You picked {color} as your color, and {animal} as your animal!")
136+
137+
bot.run("TOKEN")

0 commit comments

Comments
 (0)