Add try_user to get a user from cache or from the gateway.

This commit is contained in:
Arthur Jovart
2021-08-28 20:40:10 +02:00
parent 45d498c1b7
commit 33d109228a

View File

@@ -827,6 +827,37 @@ class Client:
""" """
return self._connection.get_user(id) return self._connection.get_user(id)
async def try_user(self, id: int, /) -> Optional[User]:
"""|coro|
Returns a user with the given ID. If not from cache, the user will be requested from the API.
You do not have to share any guilds with the user to get this information from the API,
however many operations do require that you do.
.. note::
This method is an API call. If you have :attr:`discord.Intents.members` and member cache enabled, consider :meth:`get_user` instead.
Parameters
-----------
id: :class:`int`
The ID to search for.
Returns
--------
Optional[:class:`~discord.User`]
The user or ``None`` if not found.
"""
maybe_user = self.get_user(id)
if maybe_user is not None:
return maybe_user
else:
try:
return await self.fetch_user(id)
except NotFound:
return None
def get_emoji(self, id: int, /) -> Optional[Emoji]: def get_emoji(self, id: int, /) -> Optional[Emoji]:
"""Returns an emoji with the given ID. """Returns an emoji with the given ID.