|
| 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 | +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 = [...] # this would normally be a list of discord user IDs for the purpose of this example |
| 89 | + |
| 90 | + |
| 91 | +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 | + """ |
| 96 | + return [color for color in LOTS_OF_COLORS if ctx.interaction.user.id in BASIC_ALLOWED] |
| 97 | + |
| 98 | + |
| 99 | +async def get_colors(ctx: discord.AutocompleteContext): |
| 100 | + """Returns a list of colors that begin with the characters entered so far.""" |
| 101 | + return [color for color in COLORS if color.startswith(ctx.value.lower())] |
| 102 | + |
| 103 | + |
| 104 | +async def get_animals(ctx: discord.AutocompleteContext): |
| 105 | + """Returns a list of animals that are (mostly) the color selected for the "color" option.""" |
| 106 | + picked_color = ctx.options["color"] |
| 107 | + if picked_color == "red": |
| 108 | + return ["cardinal", "ladybug"] |
| 109 | + elif picked_color == "orange": |
| 110 | + return ["clownfish", "tiger"] |
| 111 | + elif picked_color == "yellow": |
| 112 | + return ["goldfinch", "banana slug"] |
| 113 | + elif picked_color == "green": |
| 114 | + return ["tree frog", "python"] |
| 115 | + elif picked_color == "blue": |
| 116 | + return ["blue jay", "blue whale"] |
| 117 | + elif picked_color == "indigo": |
| 118 | + return ["eastern indigo snake"] # needs to return an iterable even if only one item |
| 119 | + elif picked_color == "violet": |
| 120 | + return ["purple emperor butterfly", "orchid dottyback"] |
| 121 | + else: |
| 122 | + return ["rainbowfish"] |
| 123 | + |
| 124 | + |
| 125 | +@bot.slash_command(name="ac_example") |
| 126 | +async def autocomplete_example( |
| 127 | + ctx: discord.ApplicationContext, |
| 128 | + color: Option(str, "Pick a color!", autocomplete=get_colors), |
| 129 | + animal: Option(str, "Pick an animal!", autocomplete=get_animals), |
| 130 | +): |
| 131 | + """This demonstrates using the ctx.options parameter to to create slash command options that are dependent on the values entered for other options.""" |
| 132 | + await ctx.respond(f"You picked {color} for the color, which allowed you to choose {animal} for the animal.") |
| 133 | + |
| 134 | + |
| 135 | +@bot.slash_command(name="ac_basic_example") |
| 136 | +async def autocomplete_basic_example( |
| 137 | + ctx: discord.ApplicationContext, |
| 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 |
| 140 | +): |
| 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.""" |
| 146 | + await ctx.respond(f"You picked {color} as your color, and {animal} as your animal!") |
| 147 | + |
| 148 | +bot.run("TOKEN") |
0 commit comments