From a4a344d8ebee1e5d1a94539419676ed509a08e87 Mon Sep 17 00:00:00 2001 From: Draper <27962761+Drapersniper@users.noreply.github.com> Date: Tue, 27 Oct 2020 17:21:20 +0000 Subject: [PATCH] Add `bot.get_or_fetch_user/member` methods (#4403) * add 2 get_or_fetch methods * style * local tox didnt scream and blow up * ewh extra space is ugly * Jack meant this * I think jack wanted this. * Nope Jack desired this * aaaaaaaaaaaaa Jack just say you are reviewing it come on * add get_or_fetch_member * Update redbot/core/bot.py Co-authored-by: jack1142 <6032823+jack1142@users.noreply.github.com> * Wubba Lubba Dub Dub * woa!! this one was really HARD! (sweats) * fiiiiire Co-authored-by: jack1142 <6032823+jack1142@users.noreply.github.com> --- redbot/core/bot.py | 61 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) diff --git a/redbot/core/bot.py b/redbot/core/bot.py index b4534b73d..a0c41034e 100644 --- a/redbot/core/bot.py +++ b/redbot/core/bot.py @@ -667,6 +667,67 @@ class RedBase( return self._color + async def get_or_fetch_user(self, user_id: int) -> discord.User: + """ + Retrieves a `discord.User` based on their ID. + You do not have to share any guilds + with the user to get this information, however many operations + do require that you do. + + .. warning:: + + This method may make an API call if the user is not found in the bot cache. For general usage, consider ``bot.get_user`` instead. + + Parameters + ----------- + user_id: int + The ID of the user that should be retrieved. + + Raises + ------- + Errors + Please refer to `discord.Client.fetch_user`. + + Returns + -------- + discord.User + The user you requested. + """ + + if (user := self.get_user(user_id)) is not None: + return user + return await self.fetch_user(user_id) + + async def get_or_fetch_member(self, guild: discord.Guild, member_id: int) -> discord.Member: + """ + Retrieves a `discord.Member` from a guild and a member ID. + + .. warning:: + + This method may make an API call if the user is not found in the bot cache. For general usage, consider ``discord.Guild.get_member`` instead. + + Parameters + ----------- + guild: discord.Guild + The guild which the member should be retrieved from. + member_id: int + The ID of the member that should be retrieved. + + Raises + ------- + Errors + Please refer to `discord.Guild.fetch_member`. + + Returns + -------- + discord.Member + The user you requested. + """ + + if (member := guild.get_member(member_id)) is not None: + return member + return await guild.fetch_member(member_id) + get_embed_colour = get_embed_color # start config migrations