mirror of
https://github.com/Rapptz/discord.py.git
synced 2025-06-07 20:28:38 +00:00
Add autocomplete method to Transformers
Co-authored-by: Danny <Rapptz@users.noreply.github.com>
This commit is contained in:
parent
86de926678
commit
151948a09a
@ -167,12 +167,20 @@ def validate_context_menu_name(name: str) -> str:
|
|||||||
|
|
||||||
|
|
||||||
def _validate_auto_complete_callback(
|
def _validate_auto_complete_callback(
|
||||||
callback: AutocompleteCallback[GroupT, ChoiceT]
|
callback: AutocompleteCallback[GroupT, ChoiceT],
|
||||||
|
skip_binding: bool = False,
|
||||||
) -> AutocompleteCallback[GroupT, ChoiceT]:
|
) -> AutocompleteCallback[GroupT, ChoiceT]:
|
||||||
|
|
||||||
requires_binding = is_inside_class(callback)
|
binding = getattr(callback, '__self__', None)
|
||||||
required_parameters = 2 + requires_binding
|
if binding is not None:
|
||||||
|
callback = callback.__func__
|
||||||
|
|
||||||
|
requires_binding = (binding is None and is_inside_class(callback)) or skip_binding
|
||||||
|
|
||||||
callback.requires_binding = requires_binding
|
callback.requires_binding = requires_binding
|
||||||
|
callback.binding = binding
|
||||||
|
|
||||||
|
required_parameters = 2 + requires_binding
|
||||||
params = inspect.signature(callback).parameters
|
params = inspect.signature(callback).parameters
|
||||||
if len(params) != required_parameters:
|
if len(params) != required_parameters:
|
||||||
raise TypeError('autocomplete callback requires either 2 or 3 parameters to be passed')
|
raise TypeError('autocomplete callback requires either 2 or 3 parameters to be passed')
|
||||||
@ -581,8 +589,9 @@ class Command(Generic[GroupT, P, T]):
|
|||||||
raise CommandSignatureMismatch(self)
|
raise CommandSignatureMismatch(self)
|
||||||
|
|
||||||
if param.autocomplete.requires_binding:
|
if param.autocomplete.requires_binding:
|
||||||
if self.binding is not None:
|
binding = param.autocomplete.binding or self.binding
|
||||||
choices = await param.autocomplete(self.binding, interaction, value)
|
if binding is not None:
|
||||||
|
choices = await param.autocomplete(binding, interaction, value)
|
||||||
else:
|
else:
|
||||||
raise TypeError('autocomplete parameter expected a bound self parameter but one was not provided')
|
raise TypeError('autocomplete parameter expected a bound self parameter but one was not provided')
|
||||||
else:
|
else:
|
||||||
|
@ -61,6 +61,8 @@ __all__ = (
|
|||||||
)
|
)
|
||||||
|
|
||||||
T = TypeVar('T')
|
T = TypeVar('T')
|
||||||
|
FuncT = TypeVar('FuncT', bound=Callable[..., Any])
|
||||||
|
ChoiceT = TypeVar('ChoiceT', str, int, float, Union[str, int, float])
|
||||||
NoneType = type(None)
|
NoneType = type(None)
|
||||||
|
|
||||||
if TYPE_CHECKING:
|
if TYPE_CHECKING:
|
||||||
@ -249,6 +251,35 @@ class Transformer:
|
|||||||
"""
|
"""
|
||||||
raise NotImplementedError('Derived classes need to implement this.')
|
raise NotImplementedError('Derived classes need to implement this.')
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
async def autocomplete(
|
||||||
|
cls, interaction: Interaction, value: Union[int, float, str]
|
||||||
|
) -> List[Choice[Union[int, float, str]]]:
|
||||||
|
"""|coro|
|
||||||
|
|
||||||
|
An autocomplete prompt handler to be automatically used by options using this transformer.
|
||||||
|
|
||||||
|
.. note::
|
||||||
|
|
||||||
|
Autocomplete is only supported for options with a :meth:`~discord.app_commands.Transformer.type`
|
||||||
|
of :attr:`~discord.AppCommandOptionType.string`, :attr:`~discord.AppCommandOptionType.integer`,
|
||||||
|
or :attr:`~discord.AppCommandOptionType.number`.
|
||||||
|
|
||||||
|
Parameters
|
||||||
|
-----------
|
||||||
|
interaction: :class:`~discord.Interaction`
|
||||||
|
The autocomplete interaction being handled.
|
||||||
|
value: Union[:class:`str`, :class:`int`, :class:`float`]
|
||||||
|
The current value entered by the user.
|
||||||
|
|
||||||
|
Returns
|
||||||
|
--------
|
||||||
|
List[:class:`~discord.app_commands.Choice`]
|
||||||
|
A list of choices to be displayed to the user, a maximum of 25.
|
||||||
|
|
||||||
|
"""
|
||||||
|
raise NotImplementedError('Derived classes can implement this.')
|
||||||
|
|
||||||
|
|
||||||
class _TransformMetadata:
|
class _TransformMetadata:
|
||||||
__discord_app_commands_transform__: ClassVar[bool] = True
|
__discord_app_commands_transform__: ClassVar[bool] = True
|
||||||
@ -679,4 +710,9 @@ def annotation_to_parameter(annotation: Any, parameter: inspect.Parameter) -> Co
|
|||||||
if parameter.kind in (parameter.POSITIONAL_ONLY, parameter.VAR_KEYWORD, parameter.VAR_POSITIONAL):
|
if parameter.kind in (parameter.POSITIONAL_ONLY, parameter.VAR_KEYWORD, parameter.VAR_POSITIONAL):
|
||||||
raise TypeError(f'unsupported parameter kind in callback: {parameter.kind!s}')
|
raise TypeError(f'unsupported parameter kind in callback: {parameter.kind!s}')
|
||||||
|
|
||||||
|
if inner.autocomplete is not Transformer.autocomplete:
|
||||||
|
from .commands import _validate_auto_complete_callback
|
||||||
|
|
||||||
|
result.autocomplete = _validate_auto_complete_callback(inner.autocomplete, skip_binding=True)
|
||||||
|
|
||||||
return result
|
return result
|
||||||
|
Loading…
x
Reference in New Issue
Block a user