Skip to content

Commit

Permalink
Implements a new get_or_fetch feature (Pycord-Development#42)
Browse files Browse the repository at this point in the history
* Change discord.py -> Pycord in DiscordException

* Implement new get_or_fetch utility function

* Fix
  • Loading branch information
xFGhoul authored Aug 31, 2021
1 parent aaebeba commit ea4b656
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
2 changes: 1 addition & 1 deletion discord/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
"""
Expand Down
12 changes: 11 additions & 1 deletion discord/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@
import types
import warnings

from .errors import InvalidArgument
from .errors import InvalidArgument, NotFound
from .guild import Guild

try:
import orjson
Expand Down Expand Up @@ -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)]
Expand Down

0 comments on commit ea4b656

Please sign in to comment.