-
-
Notifications
You must be signed in to change notification settings - Fork 477
Implement discord.Guild.get_or_fetch
#1703
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
Changes from all commits
3562b23
5b0c2f2
5ad3f97
5da22ad
97e3f7c
137e3a7
4e22b71
b52b31f
f6f8fab
16110be
b8857a0
a160243
7245125
ee8732f
ec56c7b
1faa45c
9ae4278
d75257f
d9c5372
6af2205
7c2912e
be3f13c
be855bb
b455f61
0540992
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 | ||||
---|---|---|---|---|---|---|
|
@@ -37,6 +37,7 @@ | |||||
Optional, | ||||||
Sequence, | ||||||
Tuple, | ||||||
TypeVar, | ||||||
Union, | ||||||
overload, | ||||||
) | ||||||
|
@@ -895,6 +896,64 @@ def get_member(self, user_id: int, /) -> Member | None: | |||||
""" | ||||||
return self._members.get(user_id) | ||||||
|
||||||
_FETCHABLE = TypeVar( | ||||||
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. Usually type variables are defined at the very top of a Python file, is there any specific reason it's defined in the Guild class here? |
||||||
"_FETCHABLE", | ||||||
bound=Union[ | ||||||
VoiceChannel, | ||||||
TextChannel, | ||||||
ForumChannel, | ||||||
StageChannel, | ||||||
CategoryChannel, | ||||||
Thread, | ||||||
Member, | ||||||
], | ||||||
) | ||||||
|
||||||
async def get_or_fetch( | ||||||
self, object_type: Type[_FETCHABLE], object_id: int, / | ||||||
) -> _FETCHABLE | None: | ||||||
"""Shortcut method to get data from guild object if it's cached or fetch from api if it's not. | ||||||
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.
Suggested change
|
||||||
|
||||||
Parameters | ||||||
---------- | ||||||
object_type: :class:`Type[VoiceChannel, TextChannel, ForumChannel, StageChannel, CategoryChannel, Thread, Member]` | ||||||
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.
Suggested change
|
||||||
Type of object to fetch or get. | ||||||
|
||||||
object_id: :class:`int` | ||||||
ID of object to get. | ||||||
|
||||||
Returns | ||||||
------- | ||||||
|
||||||
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.
Suggested change
|
||||||
Optional[:class:`~Type[VoiceChannel, TextChannel, ForumChannel, StageChannel, CategoryChannel, Thread, Member]`] | ||||||
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.
Suggested change
|
||||||
The object of type that was specified or ``None`` if not found. | ||||||
|
||||||
Usage | ||||||
----- | ||||||
``await GUILD_OBJECT.get_or_fetch(OBJECT_TYPE, OBJECT_ID)`` | ||||||
Comment on lines
+931
to
+933
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. What's the point of this? It should be very obvious by reading the rest of the documentation for this method. Instead, include a proper example (you can look at other portions of the code to see how examples are showcased). |
||||||
""" | ||||||
|
||||||
def get_attr_name(t: object_type) -> str: | ||||||
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. What was the point of even including the main portion of the code in a nested function? |
||||||
if t is Member: | ||||||
return "member" | ||||||
elif t in [ | ||||||
VoiceChannel, | ||||||
TextChannel, | ||||||
ForumChannel, | ||||||
StageChannel, | ||||||
CategoryChannel, | ||||||
Thread, | ||||||
]: | ||||||
Comment on lines
+939
to
+946
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. The list of channel classes here should be a tuple because you do not need mutability here. |
||||||
return "channel" | ||||||
|
||||||
raise InvalidArgument( | ||||||
f"Class {object_type} cannot be used with discord.Guild.get_or_fetch()" | ||||||
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. You should use the name of the class ( |
||||||
) | ||||||
|
||||||
return await utils.get_or_fetch( | ||||||
obj=self, attr=get_attr_name(object_type), id=object_id, default=None | ||||||
) | ||||||
|
||||||
@property | ||||||
def premium_subscribers(self) -> list[Member]: | ||||||
"""List[:class:`Member`]: A list of members who have "boosted" this guild.""" | ||||||
|
Uh oh!
There was an error while loading. Please reload this page.