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>
This commit is contained in:
Draper 2020-10-27 17:21:20 +00:00 committed by GitHub
parent d421c1c240
commit a4a344d8eb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -667,6 +667,67 @@ class RedBase(
return self._color 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 get_embed_colour = get_embed_color
# start config migrations # start config migrations