@@ -2027,6 +2027,36 @@ def fetch_members(
2027
2027
2028
2028
return MemberIterator (self , limit = limit , after = after )
2029
2029
2030
+ async def search_members (self , query : str , * , limit : int = 1000 ) -> list [Member ]:
2031
+ """Search for guild members whose usernames or nicknames start with the query string. Unlike :meth:`fetch_members`, this does not require :meth:`Intents.members`.
2032
+
2033
+ .. note::
2034
+
2035
+ This method is an API call. For general usage, consider filtering :attr:`members` instead.
2036
+
2037
+ .. versionadded:: 2.6
2038
+
2039
+ Parameters
2040
+ ----------
2041
+ query: :class:`str`
2042
+ Searches for usernames and nicknames that start with this string, case-insensitive.
2043
+ limit: :class:`int`
2044
+ The maximum number of members to retrieve, up to 1000.
2045
+
2046
+ Returns
2047
+ -------
2048
+ List[:class:`Member`]
2049
+ The list of members that have matched the query.
2050
+
2051
+ Raises
2052
+ ------
2053
+ HTTPException
2054
+ Getting the members failed.
2055
+ """
2056
+
2057
+ data = await self ._state .http .search_members (self .id , query , limit )
2058
+ return [Member (data = m , guild = self , state = self ._state ) for m in data ]
2059
+
2030
2060
async def fetch_member (self , member_id : int , / ) -> Member :
2031
2061
"""|coro|
2032
2062
@@ -3354,7 +3384,7 @@ async def query_members(
3354
3384
self ,
3355
3385
query : str | None = None ,
3356
3386
* ,
3357
- limit : int = 5 ,
3387
+ limit : int | None = 5 ,
3358
3388
user_ids : list [int ] | None = None ,
3359
3389
presences : bool = False ,
3360
3390
cache : bool = True ,
@@ -3372,22 +3402,22 @@ async def query_members(
3372
3402
----------
3373
3403
query: Optional[:class:`str`]
3374
3404
The string that the username's start with.
3375
- limit: :class:`int`
3376
- The maximum number of members to send back. This must be
3377
- a number between 5 and 100.
3378
- presences: :class:`bool`
3405
+ user_ids: Optional[List[:class:`int`]]
3406
+ List of user IDs to search for. If the user ID is not in the guild then it won't be returned.
3407
+
3408
+ .. versionadded:: 1.4
3409
+ limit: Optional[:class:`int`]
3410
+ The maximum number of members to send back. If no query is passed, passing ``None`` returns all members.
3411
+ If a ``query`` or ``user_ids`` is passed, must be between 1 and 100. Defaults to 5.
3412
+ presences: Optional[:class:`bool`]
3379
3413
Whether to request for presences to be provided. This defaults
3380
3414
to ``False``.
3381
3415
3382
3416
.. versionadded:: 1.6
3383
3417
3384
3418
cache: :class:`bool`
3385
3419
Whether to cache the members internally. This makes operations
3386
- such as :meth:`get_member` work for those that matched.
3387
- user_ids: Optional[List[:class:`int`]]
3388
- List of user IDs to search for. If the user ID is not in the guild then it won't be returned.
3389
-
3390
- .. versionadded:: 1.4
3420
+ such as :meth:`get_member` work for those that matched. Defaults to ``True``.
3391
3421
3392
3422
Returns
3393
3423
-------
@@ -3407,20 +3437,25 @@ async def query_members(
3407
3437
if presences and not self ._state ._intents .presences :
3408
3438
raise ClientException ("Intents.presences must be enabled to use this." )
3409
3439
3410
- if query is None :
3411
- if query == "" :
3412
- raise ValueError ("Cannot pass empty query string." )
3440
+ if not limit or limit > 100 or limit < 1 :
3441
+ if query or user_ids :
3442
+ raise ValueError (
3443
+ "limit must be between 1 and 100 when using query or user_ids"
3444
+ )
3445
+ if not limit :
3446
+ query = ""
3447
+ limit = 0
3413
3448
3449
+ if query is None :
3414
3450
if user_ids is None :
3415
- raise ValueError ("Must pass either query or user_ids" )
3451
+ raise ValueError ("Must pass query or user_ids, or set limit to None " )
3416
3452
3417
3453
if user_ids is not None and query is not None :
3418
3454
raise ValueError ("Cannot pass both query and user_ids" )
3419
3455
3420
3456
if user_ids is not None and not user_ids :
3421
3457
raise ValueError ("user_ids must contain at least 1 value" )
3422
3458
3423
- limit = min (100 , limit or 5 )
3424
3459
return await self ._state .query_members (
3425
3460
self ,
3426
3461
query = query ,
0 commit comments