diff --git a/discord/errors.py b/discord/errors.py index 852c0462b7..418e4b5abe 100644 --- a/discord/errors.py +++ b/discord/errors.py @@ -56,7 +56,7 @@ class DiscordException(Exception): - """Base exception class for pycord + """Base exception class for Pycord Ideally speaking, this could be caught to handle any exceptions raised from this library. """ diff --git a/discord/utils.py b/discord/utils.py index 4360b77a10..19320f6de8 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,15 @@ def get(iterable: Iterable[T], **attrs: Any) -> Optional[T]: return elem return None +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: + raise NotFound(404, 'HTTP request operation failed.') + return getter + def _unique(iterable: Iterable[T]) -> List[T]: return [x for x in dict.fromkeys(iterable)]