-
-
Notifications
You must be signed in to change notification settings - Fork 475
feat: Implement better get_or_fetch #2776
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 33 commits
dce0206
b2b8884
8f9a257
9f6219c
4af5149
6f59fcd
c0689cc
b20822a
4a363f5
c3a7dd2
e614c47
0c0a042
466e41b
388a234
9737f69
656de14
461eab7
b6f8878
f2a7d72
5d68207
4ca87d8
9903fb7
a1ec6f9
47de080
4430825
010cc78
368a55d
76c56a8
811682d
a102d5d
528b84b
7e766be
f6b8e18
a5deffe
fff74c4
9e45627
0baae1d
a3aeb4b
2404d9c
23409a7
8f278f8
6d5be54
11fbbc9
590339a
267d574
dc45449
f22d8a2
cbb7951
fdbb7d3
5b35d95
b8a8628
ba5541e
5421a3f
50318c2
541e668
b20cffb
3e94a4f
7384d7c
b832b55
0ffaa4b
92d2987
b8c7f7f
53dc93c
6d89376
e2e8b3d
2abfba2
fb9e077
c60fbab
29863f4
4a5eca4
2ed0de6
0c99851
dffef1f
f81a338
cc273c2
0302fcd
2837e94
1149c0a
16fda2a
b70c666
198556d
91b3d68
cb38bd7
f13e86c
543dec8
da59485
fa0efba
b6ff5f3
7d12503
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -67,10 +67,18 @@ | |
|
||
if TYPE_CHECKING: | ||
from .abc import GuildChannel, PrivateChannel, Snowflake, SnowflakeTime | ||
from .channel import DMChannel | ||
from .channel import ( | ||
CategoryChannel, | ||
DMChannel, | ||
ForumChannel, | ||
StageChannel, | ||
TextChannel, | ||
VoiceChannel, | ||
) | ||
from .member import Member | ||
from .message import Message | ||
from .poll import Poll | ||
from .threads import Thread, ThreadMember | ||
from .voice_client import VoiceProtocol | ||
|
||
__all__ = ("Client",) | ||
|
@@ -1113,6 +1121,10 @@ def get_all_members(self) -> Generator[Member]: | |
for guild in self.guilds: | ||
yield from guild.members | ||
|
||
@utils.deprecated( | ||
instead="Client.get_or_fetch(User, id)", | ||
since="2.7", | ||
) | ||
async def get_or_fetch_user(self, id: int, /) -> User | None: | ||
Lumabots marked this conversation as resolved.
Show resolved
Hide resolved
|
||
"""|coro| | ||
|
@@ -1129,7 +1141,38 @@ async def get_or_fetch_user(self, id: int, /) -> User | None: | |
The user or ``None`` if not found. | ||
""" | ||
|
||
return await utils.get_or_fetch(obj=self, attr="user", id=id, default=None) | ||
return await utils.get_or_fetch(obj=self, attr=User, id=id, default=None) | ||
Lumabots marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
_FETCHABLE = TypeVar( | ||
"_FETCHABLE", | ||
bound="VoiceChannel | TextChannel | ForumChannel | StageChannel | CategoryChannel | Thread | User | Guild | GuildEmoji | AppEmoji", | ||
) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. import this from utils instead |
||
|
||
async def get_or_fetch( | ||
self: Client, | ||
object_type: type[_FETCHABLE], | ||
object_id: int, | ||
default: Any = MISSING, | ||
) -> _FETCHABLE | None: | ||
"""Shortcut method to get data from guild object either by returning the cached version, or if it does not exist, attempt to fetch it from the api. | ||
Parameters | ||
---------- | ||
object_type: Union[:class:`VoiceChannel`, :class:`TextChannel`, :class:`ForumChannel`, :class:`StageChannel`, :class:`CategoryChannel`, :class:`Thread`, :class:`User`, :class:`Guild`, :class:`GuildEmoji`, :class:`AppEmoji`] | ||
Type of object to fetch or get. | ||
object_id: :class:`int` | ||
ID of object to get. | ||
default : Any, optional | ||
A default to return instead of raising if fetch fails. | ||
Returns | ||
------- | ||
Optional[Union[:class:`VoiceChannel`, :class:`TextChannel`, :class:`ForumChannel`, :class:`StageChannel`, :class:`CategoryChannel`, :class:`Thread`, :class:`User`, :class:`Guild`, :class:`GuildEmoji`, :class:`AppEmoji`]] | ||
The object of type that was specified or ``None`` if not found. | ||
""" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use |
||
return await utils.get_or_fetch( | ||
obj=self, object_type=object_type, object_id=object_id, default=default | ||
) | ||
|
||
# listeners/waiters | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,6 +36,7 @@ | |
Optional, | ||
Sequence, | ||
Tuple, | ||
TypeVar, | ||
Union, | ||
overload, | ||
) | ||
|
@@ -863,6 +864,40 @@ def get_member(self, user_id: int, /) -> Member | None: | |
""" | ||
return self._members.get(user_id) | ||
|
||
_FETCHABLE = TypeVar( | ||
"_FETCHABLE", | ||
bound="VoiceChannel | TextChannel | ForumChannel | StageChannel | CategoryChannel | Thread | Member | GuildEmoji", | ||
) | ||
|
||
async def get_or_fetch( | ||
self: Guild, | ||
object_type: type[_FETCHABLE], | ||
object_id: int, | ||
default: Any = MISSING, | ||
) -> _FETCHABLE | None: | ||
"""Shortcut method to get data from guild object either by returning the cached version, or if it does not exist, attempt to fetch it from the api. | ||
|
||
Parameters | ||
---------- | ||
object_type: Union[:class:`VoiceChannel`, :class:`TextChannel`, :class:`ForumChannel`, :class:`StageChannel`, :class:`CategoryChannel`, :class:`Thread`, :class:`Member`, :class:`GuildEmoji`] | ||
Type of object to fetch or get. | ||
|
||
object_id: :class:`int` | ||
ID of object to get. | ||
|
||
default : Any, optional | ||
A default to return instead of raising if fetch fails. | ||
|
||
Returns | ||
------- | ||
|
||
Optional[Union[:class:`VoiceChannel`, :class:`TextChannel`, :class:`ForumChannel`, :class:`StageChannel`, :class:`CategoryChannel`, :class:`Thread`, :class:`Member`, :class:`GuildEmoji`]] | ||
The object of type that was specified or ``None`` if not found. | ||
""" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should i use copy doc even tho its not the same exact doc ? the guild cannot use user and bot cannot use member |
||
return await utils.get_or_fetch( | ||
obj=self, object_type=object_type, object_id=object_id, default=default | ||
) | ||
|
||
@property | ||
def premium_subscribers(self) -> list[Member]: | ||
"""A list of members who have "boosted" this guild.""" | ||
|
@@ -2664,6 +2699,26 @@ async def delete_sticker( | |
""" | ||
await self._state.http.delete_guild_sticker(self.id, sticker.id, reason) | ||
|
||
def get_emoji(self, emoji_id: int, /) -> GuildEmoji | None: | ||
"""Returns an emoji with the given ID. | ||
|
||
.. versionadded:: 2.7 | ||
|
||
Parameters | ||
---------- | ||
emoji_id: int | ||
The ID to search for. | ||
|
||
Returns | ||
------- | ||
Optional[:class:`Emoji`] | ||
The returned Emoji or ``None`` if not found. | ||
""" | ||
emoji = self._state.get_emoji(emoji_id) | ||
if emoji and emoji.guild == self: | ||
return emoji | ||
return None | ||
|
||
async def fetch_emojis(self) -> list[GuildEmoji]: | ||
r"""|coro| | ||
|
||
|
Uh oh!
There was an error while loading. Please reload this page.