Skip to content
14 changes: 10 additions & 4 deletions discord/guild.py
Original file line number Diff line number Diff line change
Expand Up @@ -534,7 +534,7 @@ def _sync(self, data: GuildPayload) -> None:
if "channels" in data:
channels = data["channels"]
for c in channels:
factory, ch_type = _guild_channel_factory(c["type"])
factory, _ = _guild_channel_factory(c["type"])
if factory:
self._add_channel(factory(guild=self, data=c, state=self._state)) # type: ignore

Expand Down Expand Up @@ -1020,7 +1020,10 @@ def get_member_named(self, name: str, /) -> Member | None:

# do the actual lookup and return if found
# if it isn't found then we'll do a full name lookup below.
result = utils.find(lambda m: m.name == name[:-5] and discriminator == potential_discriminator, members)
result = utils.find(
lambda m: m.name == name[:-5] and discriminator == potential_discriminator,
members,
)
if result is not None:
return result

Expand Down Expand Up @@ -1885,7 +1888,7 @@ async def fetch_channels(self) -> Sequence[GuildChannel]:
data = await self._state.http.get_all_guild_channels(self.id)

def convert(d):
factory, ch_type = _guild_channel_factory(d["type"])
factory, _ = _guild_channel_factory(d["type"])
if factory is None:
raise InvalidData("Unknown channel type {type} for channel ID {id}.".format_map(d))

Expand Down Expand Up @@ -3312,7 +3315,10 @@ async def widget(self) -> Widget:
return Widget(state=self._state, data=data)

async def edit_widget(
self, *, enabled: bool | utils.Undefined = MISSING, channel: Snowflake | None | utils.Undefined = MISSING
self,
*,
enabled: bool | utils.Undefined = MISSING,
channel: Snowflake | None | utils.Undefined = MISSING,
) -> None:
"""|coro|

Expand Down
2 changes: 1 addition & 1 deletion discord/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -985,7 +985,7 @@ def parse_channel_update(self, data) -> None:
)

def parse_channel_create(self, data) -> None:
factory, ch_type = _channel_factory(data["type"])
factory, _ = _channel_factory(data["type"])
if factory is None:
_log.debug(
"CHANNEL_CREATE referencing an unknown channel type %s. Discarding.",
Expand Down
66 changes: 0 additions & 66 deletions discord/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,6 @@

from __future__ import annotations

from typing import (
Any,
)

from ..errors import HTTPException
from .public import (
MISSING,
UNICODE_EMOJIS,
Expand All @@ -56,7 +51,6 @@
"oauth_url",
"snowflake_time",
"find",
"get_or_fetch",
"utcnow",
"remove_markdown",
"escape_markdown",
Expand All @@ -71,63 +65,3 @@
"MISSING",
"UNICODE_EMOJIS",
)


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 as e:
getter = await getattr(obj, f"_fetch_{attr}")(id)
if getter is None:
raise ValueError(f"Could not find {attr} with id {id} on {obj}") from e
except (HTTPException, ValueError):
if default is not MISSING:
return default
else:
raise
return getter
Loading
Loading