Skip to content

Conversation

@LordOfPolls
Copy link
Contributor

About

Adds support for globally defined autocompletes. By default the client will check the command for an autocomplete callback if one cannot be found; this allows the client to fallback to one defined globally. As an added bonus this allows many commands to share a single callback.

@slash_command("test")
@slash_option("colour", "The colour to use", autocomplete=True, opt_type=interactions.OptionType.STRING, required=True)
@slash_option("text", "some text", autocomplete=True, opt_type=interactions.OptionType.STRING, required=True)
async def test(ctx: interactions.SlashContext, colour: str, text: str):
    embed = interactions.Embed(f"{text} {colour.title()}", color=get_colour(colour))
    await ctx.send(embeds=embed)


@global_autocomplete("colour")
async def colour_autocomplete(ctx: interactions.AutocompleteContext):
    colours = list((BrandColours.__members__ | FlatUIColours.__members__ | MaterialColours.__members__).keys())

    if not ctx.input_text:
        colours = colours[:25]
    else:
        results = process.extract(ctx.input_text, colours, limit=25)
        colour_match = sorted([result for result in results if result[1] > 50], key=lambda x: x[1], reverse=True)
        colours = [colour[0] for colour in colour_match]

    await ctx.send([{"name": colour.title(), "value": colour} for colour in colours])


@test.autocomplete("text")
async def text_autocomplete(ctx: interactions.AutocompleteContext):
    await ctx.send([{"name": c, "value": c} for c in ["colour", "color", "shade", "hue"]])

Checklist

  • The pre-commit code linter has been run over all edited files to ensure the code is linted.
  • I've ensured the change(s) work on 3.8.6 and higher.
  • I have added the versionadded, versionchanged and deprecated to any new or changed user-facing function I committed.

Pull-Request specification

I've made this pull request: (check all that apply)

  • For the documentation
  • To add a new feature
  • As a general enhancement
  • As a refactor of the library/the library's code
  • To fix an existing bug
  • To resolve #ISSUENUMBER

This is:

  • A breaking change

@LordOfPolls LordOfPolls added the enhancement New feature or request label Feb 8, 2023
@codecov-commenter
Copy link

codecov-commenter commented Feb 8, 2023

Codecov Report

Base: 48.37% // Head: 48.26% // Decreases project coverage by -0.12% ⚠️

Coverage data is based on head (d4e1989) compared to base (1380d9c).
Patch coverage: 17.30% of modified lines in pull request are covered.

📣 This organization is not using Codecov’s GitHub App Integration. We recommend you install it so Codecov can continue to function properly for your repositories. Learn more

Additional details and impacted files
@@            Coverage Diff             @@
##              5.x    #1273      +/-   ##
==========================================
- Coverage   48.37%   48.26%   -0.12%     
==========================================
  Files         137      137              
  Lines       13671    13720      +49     
==========================================
+ Hits         6614     6622       +8     
- Misses       7057     7098      +41     
Impacted Files Coverage Δ
interactions/__init__.py 100.00% <ø> (ø)
interactions/models/__init__.py 100.00% <ø> (ø)
interactions/models/internal/__init__.py 100.00% <ø> (ø)
main.py 0.00% <0.00%> (ø)
interactions/models/internal/extension.py 36.92% <20.00%> (-0.68%) ⬇️
...teractions/models/internal/application_commands.py 32.14% <36.36%> (+0.09%) ⬆️
interactions/client/client.py 30.75% <57.14%> (+0.16%) ⬆️

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

☔ View full report at Codecov.
📢 Do you have feedback about the report comment? Let us know in this issue.

@LordOfPolls LordOfPolls merged commit 876b84c into 5.x Feb 9, 2023
@LordOfPolls LordOfPolls deleted the global-autocomplete branch February 9, 2023 09:58
LordOfPolls added a commit that referenced this pull request Mar 13, 2023
* feat: add global autocomplete

* refactor: linter pass

* fix: remove cheeky breakpoint
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants