Skip to content

Commit 9163c9e

Browse files
committed
add comments/docstrings
1 parent 79e252f commit 9163c9e

File tree

1 file changed

+15
-4
lines changed

1 file changed

+15
-4
lines changed

examples/app_commands/slash_autocomplete.py

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,19 +85,24 @@
8585
"yellowgreen",
8686
]
8787

88-
BASIC_ALLOWED = [123, 444, 55782] # this would be a list of discord user IDs
88+
BASIC_ALLOWED = [...] # this would normally be a list of discord user IDs for the purpose of this example
8989

9090

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
9291
async def color_searcher(ctx: discord.AutocompleteContext):
92+
"""Returns a list of matching colors from the LOTS_OF_COLORS list
93+
In this example, we've added logic to only display any results in the returned list if the user's ID exists in the BASIC_ALLOWED list.
94+
This is to demonstrate passing a callback in the discord.utils.basic_autocomplete function.
95+
"""
9396
return [color for color in LOTS_OF_COLORS if ctx.interaction.user.id in BASIC_ALLOWED]
9497

9598

9699
async def get_colors(ctx: discord.AutocompleteContext):
100+
"""Returns a list of colors that begin with the characters entered so far."""
97101
return [color for color in COLORS if color.startswith(ctx.value.lower())]
98102

99103

100104
async def get_animals(ctx: discord.AutocompleteContext):
105+
"""Returns an animal that is (mostly) the color selected for the "color" option."""
101106
picked_color = ctx.options["color"]
102107
if picked_color == "red":
103108
return ["cardinal", "ladybug"]
@@ -123,15 +128,21 @@ async def autocomplete_example(
123128
color: Option(str, "Pick a color!", autocomplete=get_colors),
124129
animal: Option(str, "Pick an animal!", autocomplete=get_animals),
125130
):
131+
"""This demonstrates using the ctx.options parameter to to create slash command options that are dependent on the values entered for other options."""
126132
await ctx.respond(f"You picked {color} for the color, which allowed you to choose {animal} for the animal.")
127133

128134

129135
@slash_command(name="ac_basic_example", guild_ids=[...])
130136
async def autocomplete_basic_example(
131137
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"])),
138+
color: Option(str, "Pick a color from this big list", autocomplete=discord.utils.basic_autocomplete(color_searcher)), # Demonstrates passing a callback to discord.utils.basic_autocomplete
139+
animal: Option(str, "Pick an animal from this small list", autocomplete=discord.utils.basic_autocomplete(["snail", "python", "cricket", "orca"])), # Demonstrates passing a static iterable discord.utils.basic_autocomplete
134140
):
141+
"""This demonstrates using the discord.utils.basic_autocomplete helper function.
142+
For the `color` option, a callback is passed, where additional logic can be added to determine which values are returned.
143+
For the `animal` option, a static iterable is passed.
144+
While a small amount of values for `animal` are used in this example, iterables of any length can be passed to discord.utils.basic_autocomplete
145+
Note that the basic_autocomplete function itself will still only return a maximum of 25 items."""
135146
await ctx.respond(f"You picked {color} as your color, and {animal} as your animal!")
136147

137148
bot.run("TOKEN")

0 commit comments

Comments
 (0)