-
-
Notifications
You must be signed in to change notification settings - Fork 465
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
(Somehow) Allow autocomplete methods to accept arguments #2668
Comments
If I understood you correctly, you want something like the following: def get_autocomplete(pet_type: str) -> Callable[[AutocompleteContext], Coroutine[Any, Any, list[OptionChoice]]]:
async def actual_autocomplete(ctx: AutocompleteContext) -> list[OptionChoice]:
print(pet_type)
... # handle
return actual_autocomplete
option: discord.Option(..., autocomplete=get_autocomplete("dog")) A function that takes the needed params and then returns the actual callback for the library to call. You can access the command's cog via Or ideally, you should rely on the used option's name, which you can get via |
Very cool, thank you so much for the response and the guidance. This does seem to work! Only one minor issue, but while I can reference class Pets:
def get_autocomplete(self, pet_type: str) -> Callable[[AutocompleteContext], Coroutine[Any, Any, list[OptionChoice]]]:
async def actual_autocomplete(self, ctx: AutocompleteContext) -> list[OptionChoice]:
print(pet_type)
... # handle
return actual_autocomplete
@commands.slash_command(description="Who's a good dog?")
async def vote_for_dog(
self,
ctx: discord.ApplicationContext,
dog: discord.Option(str, "Dog's Name", autocomplete=get_autocomplete('dog')
) -> None:
# ...
> TypeError: Pets.get_autocomplete() missing 1 required positional argument: 'self' Of course, omitting it causes the IDE to whine, and it cannot be added to the Edit: Or am I completely braindead and this is a |
I will make a pr that allows for partial to be used too |
The function I suggested should indeed be outside the class or a |
What is the feature request for?
The core library
The Problem
This stream of consciousness delves into parts of python I'm not fully familiar with, and is fairly contrived without testing, so apologies in advance for typos or incorrect concepts.
With autocompletes, I thought it might be useful to allow arguments to the autocomplete itself, which could generalize related searches with different parameters without cluttering the application with one-shot methods.
Let's assume the following basic example:
Now say we want to have commands that specifically only concern dogs (without adding a secondary option). Granted, we could always create a separate autocomplete method, referencing that in our new slash command:
And of course, we could move the logic of the autocomplete to a generalized search function and have the AC make the . But it would be even more cool for the autocomplete method itself to be more generic, taking in arguments from the
autocomplete=
arg in discord.Option. This would allow us to have something likeThe first hurdle is the autocomplete handler itself. It requires an AutocompleteContext, so of course we cannot execute the callback methods directly.
Two ideas I immediately had were
functools.partial
and lambdas. Neither of these work, and I suspect for the same reason. First, a visual on how these might look:As an aside: The lambda syntax came after a few different iterations. I always thought it was interesting that my
autocomplete
argument was the bare def. And once I put it in the lambda, it lost all context and complained thatdog_autocomplete
was an unresolved reference. I also couldn't immediately referenceself.dog_autocomplete
becauseself
was unresolved -- passing that into the lambda solved all the reference errors.Anyway, both approaches yielded the same outcome:
dog_autocomplete was never awaited
However, when I removed async from my autocomplete method(s), the lambda approach worked! Unfortunately, in my production context, this isn't viable, but is at least some bit of progress. There are workarounds to 'async lambdas', but I think there's a valid use case in having this Just Work within Pycord.
If anyone smarter than me could take a look, it'd simplify my classes quite a bit to have something like this available!
The Ideal Solution
Easily allow argument passing to autocomplete method references within
Option()
s, preferably without the complex syntax of lambdas.The text was updated successfully, but these errors were encountered: