breqdev/flask-discord-interactions

Adding autocomplete interactions

rouven0 opened this issue · 1 comments

Not long ago discord released autocomplete for slash command options and I'd really appreciate seeing them implemented into this library.

Example use case
You have a database with about 1000 different Cities and want the user to select one. With autocomplete you can deliver matches fitting to what the user is typing into that option field.

A Solution I'd like
I'd appreciate a decorator based solutions like it's done with the Components. Maybe something like:

@discord.command(
    name="city-select"
    options=[
        Option(
            type=3, 
            name="country", 
            autocomplete=True
        ),
        Option(
            type=3, 
            name="city",
            autocomplete=True
        )
    ]
)
def city_select(ctx, country: str, city: str):
    return f"You selected {country}/{city}"
    

@discord.autocomplete(command="city-select",)
def city_select_autocomplete(country: Option, city: Option) -> list[Choices]:
    # In some cases we get more than one field delivered so we have to check whether they are focused or not
    # If there is only one option with autocomplete we can leave that check out
    
    if country and country.focused:
        return get_matching_countries(country.value)
    elif cty and city.focused:
        return get_matching_cities(city.value)

    

Alternatives I've considered
There are the default choices but those are limited to 25 per option. Autocomplete allows way more.

Further resources and context
https://devsnek.notion.site/devsnek/Application-Command-Option-Autocomplete-Interactions-dacc980320c948768cec5ae3a96a5886

https://discord.com/developers/docs/interactions/receiving-and-responding#interaction-response-object-autocomplete

Thanks for making me aware of this! I'll try to get this implemented by the end of the week.

Your API design looks good, but I might add a few things.

I'd prefer if developers didn't have to touch the options argument (discord.command(options=[...])) for most use cases. In addition to your suggestion, I might add a Autocomplete type to be used like

@discord.command()
def city_select(ctx, country: Autocomplete[str], city: Autocomplete[str]):
    return f"You selected {country}/{city}"

Since there's (usually) going to be a one-to-one mapping between commands and autocomplete functions, I might also add a convenience decorator along the lines of

...
@city_select.autocomplete()
def city_select_autocomplete(country: Option, city: Option):
    ...

to help catch typos.

Again, I'll try to get this done by the weekend. Feel free to let me know your thoughts on these ideas in the meantime.