@@ -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
+ CheckFunc = 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 , * , check : CheckFunc | 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,6 +1323,9 @@ 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
+ check: Optional[Callable[[:class:`.AutocompleteContext`, Any], Union[:class:`bool`, Awaitable[:class:`bool`]]]]
1327
+ Predicate callable (sync or async) used to filter the autocomplete options. This function should accept two arguments:
1328
+ the :class:`.AutocompleteContext` and an item from ``values``. If ``None`` is provided, a default check is used that includes items whose string representation starts with the user's input value, case-insensitive.
1323
1329
1324
1330
Returns
1325
1331
-------
@@ -1355,11 +1361,23 @@ async def autocomplete_callback(ctx: AutocompleteContext) -> V:
1355
1361
if asyncio .iscoroutine (_values ):
1356
1362
_values = await _values
1357
1363
1358
- def check (item : Any ) -> bool :
1359
- item = getattr (item , "name" , item )
1360
- return str (item ).lower ().startswith (str (ctx .value or "" ).lower ())
1364
+ if check is None :
1365
+
1366
+ def _check (ctx : AutocompleteContext , item : Any ) -> bool :
1367
+ item = getattr (item , "name" , item )
1368
+ return str (item ).lower ().startswith (str (ctx .value or "" ).lower ())
1369
+
1370
+ gen = (val for val in _values if _check (ctx , val ))
1371
+
1372
+ elif asyncio .iscoroutinefunction (check ):
1373
+ gen = (val for val in _values if await check (ctx , val ))
1374
+
1375
+ elif callable (check ):
1376
+ gen = (val for val in _values if check (ctx , val ))
1377
+
1378
+ else :
1379
+ raise TypeError ("``check`` must be callable." )
1361
1380
1362
- gen = (val for val in _values if check (val ))
1363
1381
return iter (itertools .islice (gen , 25 ))
1364
1382
1365
1383
return autocomplete_callback
0 commit comments