Skip to content

Commit ea4b656

Browse files
authored
Implements a new get_or_fetch feature (#42)
* Change discord.py -> Pycord in DiscordException * Implement new get_or_fetch utility function * Fix
1 parent aaebeba commit ea4b656

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

discord/errors.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@
5656

5757

5858
class DiscordException(Exception):
59-
"""Base exception class for pycord
59+
"""Base exception class for Pycord
6060
6161
Ideally speaking, this could be caught to handle any exceptions raised from this library.
6262
"""

discord/utils.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,8 @@
6161
import types
6262
import warnings
6363

64-
from .errors import InvalidArgument
64+
from .errors import InvalidArgument, NotFound
65+
from .guild import Guild
6566

6667
try:
6768
import orjson
@@ -448,6 +449,15 @@ def get(iterable: Iterable[T], **attrs: Any) -> Optional[T]:
448449
return elem
449450
return None
450451

452+
async def get_or_fetch(guild: Guild, type: str, id: int):
453+
getter = getattr(guild, f'get_{type}')(id)
454+
if getter is None:
455+
try:
456+
getter = await getattr(guild, f'fetch_{type}')(id)
457+
except NotFound:
458+
raise NotFound(404, 'HTTP request operation failed.')
459+
return getter
460+
451461

452462
def _unique(iterable: Iterable[T]) -> List[T]:
453463
return [x for x in dict.fromkeys(iterable)]

0 commit comments

Comments
 (0)