From 4b9a5654e4820dcd280723da5cc54149de1447ff Mon Sep 17 00:00:00 2001 From: xFGhoul Date: Mon, 30 Aug 2021 10:25:30 -0700 Subject: [PATCH 1/3] Change discord.py -> Pycord in DiscordException --- discord/errors.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/discord/errors.py b/discord/errors.py index bc2398d558..418e4b5abe 100644 --- a/discord/errors.py +++ b/discord/errors.py @@ -56,7 +56,7 @@ class DiscordException(Exception): - """Base exception class for discord.py + """Base exception class for Pycord Ideally speaking, this could be caught to handle any exceptions raised from this library. """ From b8567463c4762ba5659515ff4614b081edff8bdb Mon Sep 17 00:00:00 2001 From: xFGhoul Date: Tue, 31 Aug 2021 01:16:07 -0700 Subject: [PATCH 2/3] Implement new get_or_fetch utility function --- discord/utils.py | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/discord/utils.py b/discord/utils.py index 4360b77a10..c094898d40 100644 --- a/discord/utils.py +++ b/discord/utils.py @@ -61,7 +61,8 @@ import types import warnings -from .errors import InvalidArgument +from .errors import InvalidArgument, NotFound +from .guild import Guild try: import orjson @@ -448,6 +449,16 @@ def get(iterable: Iterable[T], **attrs: Any) -> Optional[T]: return elem return None +async def get_or_fetch(self, guild: Guild, type: str, id: int): + try: + getter = getattr(guild, f'get_{type}')(id) + except AttributeError: + try: + getter = await getattr(guild, f'fetch_{type}')(id) + except NotFound: + raise NotFound(404, 'HTTP request operation failed.') + return getter + def _unique(iterable: Iterable[T]) -> List[T]: return [x for x in dict.fromkeys(iterable)] From 11d9325d4a4cc120856935a0bb67ad4c7e596d5f Mon Sep 17 00:00:00 2001 From: xFGhoul Date: Tue, 31 Aug 2021 01:35:05 -0700 Subject: [PATCH 3/3] Fix --- discord/utils.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/discord/utils.py b/discord/utils.py index c094898d40..19320f6de8 100644 --- a/discord/utils.py +++ b/discord/utils.py @@ -449,10 +449,9 @@ def get(iterable: Iterable[T], **attrs: Any) -> Optional[T]: return elem return None -async def get_or_fetch(self, guild: Guild, type: str, id: int): - try: - getter = getattr(guild, f'get_{type}')(id) - except AttributeError: +async def get_or_fetch(guild: Guild, type: str, id: int): + getter = getattr(guild, f'get_{type}')(id) + if getter is None: try: getter = await getattr(guild, f'fetch_{type}')(id) except NotFound: