@@ -1306,9 +1306,12 @@ def generate_snowflake(dt: datetime.datetime | None = None) -> int:
1306
1306
AV = Awaitable [V ]
1307
1307
Values = Union [V , Callable [[AutocompleteContext ], Union [V , AV ]], AV ]
1308
1308
AutocompleteFunc = Callable [[AutocompleteContext ], AV ]
1309
+ FilterFunc = Callable [[AutocompleteContext , Any ], Union [bool , Awaitable [bool ]]]
1309
1310
1310
1311
1311
- def basic_autocomplete (values : Values ) -> AutocompleteFunc :
1312
+ def basic_autocomplete (
1313
+ values : Values , * , filter : FilterFunc | None = None
1314
+ ) -> AutocompleteFunc :
1312
1315
"""A helper function to make a basic autocomplete for slash commands. This is a pretty standard autocomplete and
1313
1316
will return any options that start with the value from the user, case-insensitive. If the ``values`` parameter is
1314
1317
callable, it will be called with the AutocompleteContext.
@@ -1320,18 +1323,21 @@ def basic_autocomplete(values: Values) -> AutocompleteFunc:
1320
1323
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`]]]]
1321
1324
Possible values for the option. Accepts an iterable of :class:`str`, a callable (sync or async) that takes a
1322
1325
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
1323
1331
1324
1332
Returns
1325
1333
-------
1326
1334
Callable[[:class:`.AutocompleteContext`], Awaitable[Union[Iterable[:class:`.OptionChoice`], Iterable[:class:`str`], Iterable[:class:`int`], Iterable[:class:`float`]]]]
1327
1335
A wrapped callback for the autocomplete.
1328
1336
1329
- Note
1330
- ----
1331
- Autocomplete cannot be used for options that have specified choices.
1337
+ Examples
1338
+ --------
1332
1339
1333
- Example
1334
- -------
1340
+ Basic usage:
1335
1341
1336
1342
.. code-block:: python3
1337
1343
@@ -1344,7 +1350,17 @@ async def autocomplete(ctx):
1344
1350
1345
1351
Option(str, "name", autocomplete=basic_autocomplete(autocomplete))
1346
1352
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
+
1347
1359
.. versionadded:: 2.0
1360
+
1361
+ Note
1362
+ ----
1363
+ Autocomplete cannot be used for options that have specified choices.
1348
1364
"""
1349
1365
1350
1366
async def autocomplete_callback (ctx : AutocompleteContext ) -> V :
@@ -1355,11 +1371,23 @@ async def autocomplete_callback(ctx: AutocompleteContext) -> V:
1355
1371
if asyncio .iscoroutine (_values ):
1356
1372
_values = await _values
1357
1373
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." )
1361
1390
1362
- gen = (val for val in _values if check (val ))
1363
1391
return iter (itertools .islice (gen , 25 ))
1364
1392
1365
1393
return autocomplete_callback
0 commit comments