app_commands: cleaner autocomplete registration? #8459
Answered
by
Soheab
codeofdusk
asked this question in
Q&A
-
Hello, I have a cog that looks (roughly) like this: class Colours2(commands.Cog):
ID_RED = 1
ID_BLUE = 2
@app_commands.command()
async def describe(self, interaction, colour: int):
if colour == ID_RED:
await interaction.response.send_message("The colour of blood", ephemeral=True)
elif colour == ID_BLUE:
await interaction.response.send_message("The colour of the sky", ephemeral=True)
else:
await interaction.response.send_message("No clue", ephemeral=True)
@describe.autocomplete("colour")
async def describe_autocomplete(
self, interaction: discord.Interaction, current: str
) -> List[app_commands.Choice[str]]:
return self._autocomplete_colour_id(current)
@app_commands.command()
async def red(self, interaction, colour: int):
if colour == ID_RED:
await interaction.response.send_message("This colour is red!", ephemeral=True)
else:
await interaction.response.send_message("This colour is not red!", ephemeral=True)
@red.autocomplete("colour")
async def red_autocomplete(
self, interaction: discord.Interaction, current: str
) -> List[app_commands.Choice[str]]:
return self._autocomplete_colour_id(current)
def _autocomplete_colour_id(self, current: str):
# Do some filesystem/network magic to populate the autocomplete choices...
return [
app_commands.Choice(name="Red", value=ID_RED),
app_commands.Choice(name="Blue", value=ID_BLUE),
] The |
Beta Was this translation helpful? Give feedback.
Answered by
Soheab
Sep 11, 2022
Replies: 1 comment 3 replies
-
Example: async def my_autocomplete(...):
...
@app_commands.command()
@app_commands.autocomplete(colour=my_autocomplete)
async def foo(
self,
interaction: Interaction,
colour: str
):
...
@app_commands.command()
@app_commands.autocomplete(color=my_autocomplete)
async def bar(
self,
interaction: Interaction,
color: int
):
...
@foo.autocomplete("colour")
@bar.autocomplete("color")
async def my_autocomplete(...):
... Apologies if I'm completely wrong on that. |
Beta Was this translation helpful? Give feedback.
3 replies
Answer selected by
codeofdusk
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example:
@command.autocomplete(...)
decorators on one autocomplete