"""
The MIT License (MIT)
Copyright (c) 2015-2021 Rapptz
Copyright (c) 2021-present Pycord Development
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
"""
from __future__ import annotations
from typing import (
Any,
AsyncIterator,
Iterator,
Mapping,
Protocol,
TypeVar,
Union,
)
from ..errors import HTTPException
from .public import (
basic_autocomplete,
generate_snowflake,
utcnow,
find,
snowflake_time,
oauth_url,
Undefined,
MISSING,
format_dt,
escape_mentions,
raw_mentions,
raw_channel_mentions,
raw_role_mentions,
remove_markdown,
escape_markdown,
)
DISCORD_EPOCH = 1420070400000
__all__ = (
"oauth_url",
"snowflake_time",
"find",
"get_or_fetch",
"utcnow",
"remove_markdown",
"escape_markdown",
"escape_mentions",
"raw_mentions",
"raw_channel_mentions",
"raw_role_mentions",
"format_dt",
"generate_snowflake",
"basic_autocomplete",
"Undefined",
"MISSING",
)
[docs]
async def get_or_fetch(obj, attr: str, id: int, *, default: Any = MISSING) -> Any:
"""|coro|
Attempts to get an attribute from the object in cache. If it fails, it will attempt to fetch it.
If the fetch also fails, an error will be raised.
Parameters
----------
obj: Any
The object to use the get or fetch methods in
attr: :class:`str`
The attribute to get or fetch. Note the object must have both a ``get_`` and ``fetch_`` method for this attribute.
id: :class:`int`
The ID of the object
default: Any
The default value to return if the object is not found, instead of raising an error.
Returns
-------
Any
The object found or the default value.
Raises
------
:exc:`AttributeError`
The object is missing a ``get_`` or ``fetch_`` method
:exc:`NotFound`
Invalid ID for the object
:exc:`HTTPException`
An error occurred fetching the object
:exc:`Forbidden`
You do not have permission to fetch the object
Examples
--------
Getting a guild from a guild ID: ::
guild = await utils.get_or_fetch(client, "guild", guild_id)
Getting a channel from the guild. If the channel is not found, return None: ::
channel = await utils.get_or_fetch(guild, "channel", channel_id, default=None)
"""
getter = getattr(obj, f"get_{attr}")(id)
if getter is None:
try:
getter = await getattr(obj, f"fetch_{attr}")(id)
except AttributeError:
getter = await getattr(obj, f"_fetch_{attr}")(id)
if getter is None:
raise ValueError(f"Could not find {attr} with id {id} on {obj}")
except (HTTPException, ValueError):
if default is not MISSING:
return default
else:
raise
return getter