Skip to content

Commit f84bef4

Browse files
sweetbruleepre-commit-ci[bot]JustaSqu1dplun1331
authored andcommitted
feat: Add optional filter parameter to utils.basic_autocomplete (Pycord-Development#2590)
Signed-off-by: Jeffrey Ruan <[email protected]> Signed-off-by: plun1331 <[email protected]> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: JustaSqu1d <[email protected]> Co-authored-by: plun1331 <[email protected]>
1 parent a6eb1f3 commit f84bef4

File tree

2 files changed

+40
-10
lines changed

2 files changed

+40
-10
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ These changes are available on the `master` branch, but have not yet been releas
2222
`tags`. ([#2520](https://github.com/Pycord-Development/pycord/pull/2520))
2323
- Added `Member.guild_banner` and `Member.display_banner` properties.
2424
([#2556](https://github.com/Pycord-Development/pycord/pull/2556))
25+
- Added optional `filter` parameter to `utils.basic_autocomplete()`.
26+
([#2590](https://github.com/Pycord-Development/pycord/pull/2590))
2527

2628
### Fixed
2729

discord/utils.py

Lines changed: 38 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1306,9 +1306,12 @@ def generate_snowflake(dt: datetime.datetime | None = None) -> int:
13061306
AV = Awaitable[V]
13071307
Values = Union[V, Callable[[AutocompleteContext], Union[V, AV]], AV]
13081308
AutocompleteFunc = Callable[[AutocompleteContext], AV]
1309+
FilterFunc = Callable[[AutocompleteContext, Any], Union[bool, Awaitable[bool]]]
13091310

13101311

1311-
def basic_autocomplete(values: Values) -> AutocompleteFunc:
1312+
def basic_autocomplete(
1313+
values: Values, *, filter: FilterFunc | None = None
1314+
) -> AutocompleteFunc:
13121315
"""A helper function to make a basic autocomplete for slash commands. This is a pretty standard autocomplete and
13131316
will return any options that start with the value from the user, case-insensitive. If the ``values`` parameter is
13141317
callable, it will be called with the AutocompleteContext.
@@ -1320,18 +1323,21 @@ def basic_autocomplete(values: Values) -> AutocompleteFunc:
13201323
values: Union[Union[Iterable[:class:`.OptionChoice`], Iterable[:class:`str`], Iterable[:class:`int`], Iterable[:class:`float`]], Callable[[:class:`.AutocompleteContext`], Union[Union[Iterable[:class:`str`], Iterable[:class:`int`], Iterable[:class:`float`]], Awaitable[Union[Iterable[:class:`str`], Iterable[:class:`int`], Iterable[:class:`float`]]]]], Awaitable[Union[Iterable[:class:`str`], Iterable[:class:`int`], Iterable[:class:`float`]]]]
13211324
Possible values for the option. Accepts an iterable of :class:`str`, a callable (sync or async) that takes a
13221325
single argument of :class:`.AutocompleteContext`, or a coroutine. Must resolve to an iterable of :class:`str`.
1326+
filter: Optional[Callable[[:class:`.AutocompleteContext`, Any], Union[:class:`bool`, Awaitable[:class:`bool`]]]]
1327+
An optional callable (sync or async) used to filter the autocomplete options. It accepts two arguments:
1328+
the :class:`.AutocompleteContext` and an item from ``values`` iteration treated as callback parameters. If ``None`` is provided, a default filter is used that includes items whose string representation starts with the user's input value, case-insensitive.
1329+
1330+
.. versionadded:: 2.7
13231331
13241332
Returns
13251333
-------
13261334
Callable[[:class:`.AutocompleteContext`], Awaitable[Union[Iterable[:class:`.OptionChoice`], Iterable[:class:`str`], Iterable[:class:`int`], Iterable[:class:`float`]]]]
13271335
A wrapped callback for the autocomplete.
13281336
1329-
Note
1330-
----
1331-
Autocomplete cannot be used for options that have specified choices.
1337+
Examples
1338+
--------
13321339
1333-
Example
1334-
-------
1340+
Basic usage:
13351341
13361342
.. code-block:: python3
13371343
@@ -1344,7 +1350,17 @@ async def autocomplete(ctx):
13441350
13451351
Option(str, "name", autocomplete=basic_autocomplete(autocomplete))
13461352
1353+
With filter parameter:
1354+
1355+
.. code-block:: python3
1356+
1357+
Option(str, "color", autocomplete=basic_autocomplete(("red", "green", "blue"), filter=lambda c, i: str(c.value or "") in i))
1358+
13471359
.. versionadded:: 2.0
1360+
1361+
Note
1362+
----
1363+
Autocomplete cannot be used for options that have specified choices.
13481364
"""
13491365

13501366
async def autocomplete_callback(ctx: AutocompleteContext) -> V:
@@ -1355,11 +1371,23 @@ async def autocomplete_callback(ctx: AutocompleteContext) -> V:
13551371
if asyncio.iscoroutine(_values):
13561372
_values = await _values
13571373

1358-
def check(item: Any) -> bool:
1359-
item = getattr(item, "name", item)
1360-
return str(item).lower().startswith(str(ctx.value or "").lower())
1374+
if filter is None:
1375+
1376+
def _filter(ctx: AutocompleteContext, item: Any) -> bool:
1377+
item = getattr(item, "name", item)
1378+
return str(item).lower().startswith(str(ctx.value or "").lower())
1379+
1380+
gen = (val for val in _values if _filter(ctx, val))
1381+
1382+
elif asyncio.iscoroutinefunction(filter):
1383+
gen = (val for val in _values if await filter(ctx, val))
1384+
1385+
elif callable(filter):
1386+
gen = (val for val in _values if filter(ctx, val))
1387+
1388+
else:
1389+
raise TypeError("``filter`` must be callable.")
13611390

1362-
gen = (val for val in _values if check(val))
13631391
return iter(itertools.islice(gen, 25))
13641392

13651393
return autocomplete_callback

0 commit comments

Comments
 (0)