feat(commands): Add support for injected parameters autocompletions
Summary
Resolves #670
Checklist
- [x] If code changes were made, then they have been tested
- [x] I have updated the documentation to reflect the changes
- [x] I have formatted the code properly by running
task lint - [x] I have type-checked the code by running
task pyright
- [x] This PR fixes an issue
- [x] This PR adds something new (e.g. new method or parameters)
- [ ] This PR is a breaking change (e.g. methods or parameters removed/renamed)
- [ ] This PR is not a code change (e.g. documentation, README, ...)
I'm not entirely sure, but did you implement #669 as well, @ItsAleph?
Hmm, found another bug:
from typing import NamedTuple
import disnake
from disnake.ext import commands
class H(NamedTuple):
abc: int
@commands.register_injection
def create_h(inter: disnake.CommandInter, param: str) -> H:
return H(int(param))
@create_h.autocomplete("param")
async def autocomplete(inter: disnake.CommandInter, value: str):
return [value * 2, value * 3, value * 4]
class Misc(commands.Cog):
@commands.slash_command()
async def test_cog(self, inter: disnake.GuildCommandInteraction, stuff: H) -> None:
await inter.send(repr(stuff))
def setup(bot: commands.Bot):
bot.add_cog(Misc())
The autocomplete function is being called as autocomplete(cog, inter, input) instead of autocomplete(inter, input) since expand_params adds __slash_command__.
Traceback (most recent call last):
File "/home/vi/syncthing/development/discord/_meta/disnake/disnake/client.py", line 740, in _run_event
await coro(*args, **kwargs)
File "/home/vi/syncthing/development/discord/_meta/disnake/disnake/ext/commands/interaction_bot_base.py", line 1286, in on_application_command_autocomplete
await self.process_app_command_autocompletion(interaction)
File "/home/vi/syncthing/development/discord/_meta/disnake/disnake/ext/commands/interaction_bot_base.py", line 1204, in process_app_command_autocompletion
await slash_command._call_relevant_autocompleter(inter)
File "/home/vi/syncthing/development/discord/_meta/disnake/disnake/ext/commands/slash_core.py", line 629, in _call_relevant_autocompleter
choices = await call_autocompleter(focused_option.name, inter, focused_option.value)
File "/home/vi/syncthing/development/discord/_meta/disnake/disnake/ext/commands/slash_core.py", line 605, in _call_autocompleter
return await _call_autocompleter(self, param, inter, user_input)
File "/home/vi/syncthing/development/discord/_meta/disnake/disnake/ext/commands/slash_core.py", line 114, in _call_autocompleter
choices = autocomp(cog, inter, user_input)
TypeError: autocomplete() takes 2 positional arguments but 3 were given
Autocompletes were designed to always be used on the same command, however with injections they could be used in multiple commands, so these things need to be decoupled. Honestly not quite sure how, perhaps someone else has a smart idea.
Seems like EQ fixed it :O
https://github.com/DisnakeDev/disnake/pull/675#issuecomment-1250383408
@shiftinv this should be fixed as of https://github.com/DisnakeDev/disnake/pull/675/commits/5f9e404f6599524b89631ab25edf87e48b7f613c and https://github.com/DisnakeDev/disnake/pull/675/commits/260ee48aca861573b2f7f9267efe5b5800b5d0d7
@shiftinv would you please review too?