From 74713b05eef4c1e325bda9655b4d85c5057fb04e Mon Sep 17 00:00:00 2001 From: iDutchy Date: Thu, 1 Oct 2020 01:24:30 +0000 Subject: [PATCH] add try_user --- README.rst | 1 + discord/client.py | 34 ++++++++++++++++++++++++++++++++++ docs/index.rst | 1 + 3 files changed, 36 insertions(+) diff --git a/README.rst b/README.rst index b207ccc7..c2a2ec9e 100644 --- a/README.rst +++ b/README.rst @@ -16,6 +16,7 @@ Custom Features - **Documentation URL:** https://enhanced-dpy.readthedocs.io/en/latest/index.html - Added ``Guild.bots`` / ``Guild.humans`` - Added ``Bot.owner`` / ``Bot.owners`` +- Added ``Client.try_user`` / ``Bot.try_user`` - Merged in ext-colors (https://github.com/MGardne8/DiscordPyColours) - Using Rapptz/discord.py/tree/neo-docs for documentation - Adding support for ``hex()`` to ``discord.Color`` diff --git a/discord/client.py b/discord/client.py index 1ddb09d6..64771a7e 100644 --- a/discord/client.py +++ b/discord/client.py @@ -1343,6 +1343,40 @@ class Client: data['rpc_origins'] = None return AppInfo(self._connection, data) + async def try_user(self, user_id): + """|coro| + + Retrieves a :class:`~discord.User` based on their ID. This can only + be used by bot accounts. + + .. note:: + + This will first attempt to get the user from the cache. + If that fails, it will make an API call. + For general usage, consider :meth:`get_user` instead. + + Parameters + ----------- + user_id: :class:`int` + The user's ID to fetch from. + + Raises + ------- + :exc:`.NotFound` + A user with this ID does not exist. + :exc:`.HTTPException` + Fetching the user failed. + + Returns + -------- + :class:`~discord.User` + The user you requested. + """ + user = self.get_user(user_id) + if user is None: + user = await self.fetch_user(user_id) + return user + async def fetch_user(self, user_id): """|coro| diff --git a/docs/index.rst b/docs/index.rst index a4e8065c..06f6ff5f 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -21,6 +21,7 @@ Custom Features - **Documentation URL:** https://enhanced-dpy.readthedocs.io/en/latest/index.html - Added ``Guild.bots`` / ``Guild.humans`` - Added ``Bot.owner`` / ``Bot.owners`` +- Added ``Client.try_user`` / ``Bot.try_user`` - Merged in ext-colors (https://github.com/MGardne8/DiscordPyColours) - Using Rapptz/discord.py/tree/neo-docs for documentation - Adding support for ``hex()`` to ``discord.Color``