mirror of
https://github.com/Rapptz/discord.py.git
synced 2025-04-21 00:07:51 +00:00
Change autocomplete callbacks to only take two parameters
The old namespace parameter can be retrieved using the Interaction.namespace property instead, which leads to somewhat cleaner code.
This commit is contained in:
parent
d2f6b05c43
commit
98a31cf285
@ -112,8 +112,8 @@ if TYPE_CHECKING:
|
||||
]
|
||||
|
||||
AutocompleteCallback = Union[
|
||||
Callable[[GroupT, 'Interaction', ChoiceT, Namespace], Coro[List[Choice[ChoiceT]]]],
|
||||
Callable[['Interaction', ChoiceT, Namespace], Coro[List[Choice[ChoiceT]]]],
|
||||
Callable[[GroupT, 'Interaction', ChoiceT], Coro[List[Choice[ChoiceT]]]],
|
||||
Callable[['Interaction', ChoiceT], Coro[List[Choice[ChoiceT]]]],
|
||||
]
|
||||
else:
|
||||
CommandCallback = Callable[..., Coro[T]]
|
||||
@ -163,11 +163,11 @@ def _validate_auto_complete_callback(
|
||||
) -> AutocompleteCallback[GroupT, ChoiceT]:
|
||||
|
||||
requires_binding = is_inside_class(callback)
|
||||
required_parameters = 3 + requires_binding
|
||||
required_parameters = 2 + requires_binding
|
||||
callback.requires_binding = requires_binding
|
||||
params = inspect.signature(callback).parameters
|
||||
if len(params) < required_parameters:
|
||||
raise TypeError('autocomplete callback requires either 3 or 4 parameters to be passed')
|
||||
if len(params) != required_parameters:
|
||||
raise TypeError('autocomplete callback requires either 2 or 3 parameters to be passed')
|
||||
|
||||
return callback
|
||||
|
||||
@ -493,11 +493,11 @@ class Command(Generic[GroupT, P, T]):
|
||||
|
||||
if param.autocomplete.requires_binding:
|
||||
if self.binding is not None:
|
||||
choices = await param.autocomplete(self.binding, interaction, value, namespace)
|
||||
choices = await param.autocomplete(self.binding, interaction, value)
|
||||
else:
|
||||
raise TypeError('autocomplete parameter expected a bound self parameter but one was not provided')
|
||||
else:
|
||||
choices = await param.autocomplete(interaction, value, namespace)
|
||||
choices = await param.autocomplete(interaction, value)
|
||||
|
||||
if interaction.response.is_done():
|
||||
return
|
||||
@ -546,10 +546,13 @@ class Command(Generic[GroupT, P, T]):
|
||||
) -> Callable[[AutocompleteCallback[GroupT, ChoiceT]], AutocompleteCallback[GroupT, ChoiceT]]:
|
||||
"""A decorator that registers a coroutine as an autocomplete prompt for a parameter.
|
||||
|
||||
The coroutine callback must have 3 parameters, the :class:`~discord.Interaction`,
|
||||
the current value by the user (usually either a :class:`str`, :class:`int`, or :class:`float`,
|
||||
depending on the type of the parameter being marked as autocomplete), and then the
|
||||
:class:`Namespace` that represents possible values are partially filled in.
|
||||
The coroutine callback must have 2 parameters, the :class:`~discord.Interaction`,
|
||||
and the current value by the user (usually either a :class:`str`, :class:`int`, or :class:`float`,
|
||||
depending on the type of the parameter being marked as autocomplete).
|
||||
|
||||
To get the values from other parameters that may be filled in, accessing
|
||||
:attr:`.Interaction.namespace` will give a :class:`Namespace` object with those
|
||||
values.
|
||||
|
||||
The coroutine decorator **must** return a list of :class:`~discord.app_commands.Choice` objects.
|
||||
Only up to 25 objects are supported.
|
||||
@ -566,7 +569,6 @@ class Command(Generic[GroupT, P, T]):
|
||||
async def fruits_autocomplete(
|
||||
interaction: discord.Interaction,
|
||||
current: str,
|
||||
namespace: app_commands.Namespace
|
||||
) -> List[app_commands.Choice[str]]:
|
||||
fruits = ['Banana', 'Pineapple', 'Apple', 'Watermelon', 'Melon', 'Cherry']
|
||||
return [
|
||||
@ -1163,6 +1165,8 @@ def autocomplete(**parameters: AutocompleteCallback[GroupT, ChoiceT]) -> Callabl
|
||||
Autocomplete is only supported on types that have :class:`str`, :class:`int`, or :class:`float`
|
||||
values.
|
||||
|
||||
For more information, see the :meth:`Command.autocomplete` documentation.
|
||||
|
||||
Example:
|
||||
|
||||
.. code-block:: python3
|
||||
@ -1175,7 +1179,6 @@ def autocomplete(**parameters: AutocompleteCallback[GroupT, ChoiceT]) -> Callabl
|
||||
async def fruits_autocomplete(
|
||||
interaction: discord.Interaction,
|
||||
current: str,
|
||||
namespace: app_commands.Namespace
|
||||
) -> List[app_commands.Choice[str]]:
|
||||
fruits = ['Banana', 'Pineapple', 'Apple', 'Watermelon', 'Melon', 'Cherry']
|
||||
return [
|
||||
|
Loading…
x
Reference in New Issue
Block a user