|
27 | 27 | import inspect
|
28 | 28 | import logging
|
29 | 29 | from enum import Enum
|
30 |
| -from typing import TYPE_CHECKING, Literal, Optional, Type, Union |
| 30 | +from typing import TYPE_CHECKING, Any, Callable, Literal, Optional, Type, Union |
31 | 31 |
|
32 | 32 | from ..abc import GuildChannel, Mentionable
|
33 | 33 | from ..channel import (
|
@@ -272,6 +272,7 @@ def __init__(
|
272 | 272 | )
|
273 | 273 | self.default = kwargs.pop("default", None)
|
274 | 274 |
|
| 275 | + self._autocomplete: Callable[[Any], Any, Any] | None = None |
275 | 276 | self.autocomplete = kwargs.pop("autocomplete", None)
|
276 | 277 | if len(enum_choices) > 25:
|
277 | 278 | self.choices: list[OptionChoice] = []
|
@@ -390,6 +391,27 @@ def to_dict(self) -> dict:
|
390 | 391 | def __repr__(self):
|
391 | 392 | return f"<discord.commands.{self.__class__.__name__} name={self.name}>"
|
392 | 393 |
|
| 394 | + @property |
| 395 | + def autocomplete(self) -> Callable[[Any], Any, Any] | None: |
| 396 | + return self._autocomplete |
| 397 | + |
| 398 | + @autocomplete.setter |
| 399 | + def autocomplete(self, value: Callable[[Any], Any, Any] | None) -> None: |
| 400 | + self._autocomplete = value |
| 401 | + # this is done here so it does not have to be computed every time the autocomplete is invoked |
| 402 | + if self._autocomplete is not None: |
| 403 | + self._autocomplete._is_instance_method = ( |
| 404 | + sum( |
| 405 | + 1 |
| 406 | + for param in inspect.signature( |
| 407 | + self.autocomplete |
| 408 | + ).parameters.values() |
| 409 | + if param.default == param.empty |
| 410 | + and param.kind not in (param.VAR_POSITIONAL, param.VAR_KEYWORD) |
| 411 | + ) |
| 412 | + == 2 |
| 413 | + ) |
| 414 | + |
393 | 415 |
|
394 | 416 | class OptionChoice:
|
395 | 417 | """
|
|
0 commit comments