3131import sys
3232import traceback
3333from types import TracebackType
34- from typing import TYPE_CHECKING , Any , Callable , Coroutine , Generator , Sequence , TypeVar
34+ from typing import (
35+ TYPE_CHECKING ,
36+ Any ,
37+ Callable ,
38+ Coroutine ,
39+ Generator ,
40+ Sequence ,
41+ TypeVar ,
42+ )
3543
3644import aiohttp
3745
6169from .threads import Thread
6270from .ui .view import View
6371from .user import ClientUser , User
64- from .utils import MISSING
72+ from .utils import _D , _FETCHABLE , MISSING
6573from .voice_client import VoiceClient
6674from .webhook import Webhook
6775from .widget import Widget
6876
6977if TYPE_CHECKING :
7078 from .abc import GuildChannel , PrivateChannel , Snowflake , SnowflakeTime
71- from .channel import DMChannel
79+ from .channel import (
80+ CategoryChannel ,
81+ DMChannel ,
82+ ForumChannel ,
83+ StageChannel ,
84+ TextChannel ,
85+ VoiceChannel ,
86+ )
7287 from .interactions import Interaction
7388 from .member import Member
7489 from .message import Message
7590 from .poll import Poll
7691 from .soundboard import SoundboardSound
92+ from .threads import Thread , ThreadMember
7793 from .ui .item import Item
7894 from .voice_client import VoiceProtocol
7995
@@ -1165,7 +1181,12 @@ def get_all_members(self) -> Generator[Member]:
11651181 for guild in self .guilds :
11661182 yield from guild .members
11671183
1168- async def get_or_fetch_user (self , id : int , / ) -> User | None :
1184+ @utils .deprecated (
1185+ instead = "Client.get_or_fetch(User, id)" ,
1186+ since = "2.7" ,
1187+ removed = "3.0" ,
1188+ )
1189+ async def get_or_fetch_user (self , id : int , / ) -> User | None : # TODO: Remove in 3.0
11691190 """|coro|
11701191
11711192 Looks up a user in the user cache or fetches if not found.
@@ -1181,7 +1202,49 @@ async def get_or_fetch_user(self, id: int, /) -> User | None:
11811202 The user or ``None`` if not found.
11821203 """
11831204
1184- return await utils .get_or_fetch (obj = self , attr = "user" , id = id , default = None )
1205+ return await self .get_or_fetch (object_type = User , object_id = id , default = None )
1206+
1207+ async def get_or_fetch (
1208+ self : Client ,
1209+ object_type : type [_FETCHABLE ],
1210+ object_id : int | None ,
1211+ default : _D = None ,
1212+ ) -> _FETCHABLE | _D | None :
1213+ """
1214+ Shortcut method to get data from an object either by returning the cached version, or if it does not exist, attempting to fetch it from the API.
1215+
1216+ Parameters
1217+ ----------
1218+ object_type: Type[:class:`VoiceChannel` | :class:`TextChannel` | :class:`ForumChannel` | :class:`StageChannel` | :class:`CategoryChannel` | :class:`Thread` | :class:`User` | :class:`Guild` | :class:`GuildEmoji` | :class:`AppEmoji`]
1219+ Type of object to fetch or get.
1220+
1221+ object_id: :class:`int` | :data:`None`
1222+ ID of object to get. If :data:`None`, returns `default` if provided, else :data:`None`.
1223+
1224+ default: Any | :data:`None`
1225+ A default to return instead of raising if fetch fails.
1226+
1227+ Returns
1228+ -------
1229+ :class:`VoiceChannel` | :class:`TextChannel` | :class:`ForumChannel` | :class:`StageChannel` | :class:`CategoryChannel` | :class:`Thread` | :class:`User` | :class:`Guild` | :class:`GuildEmoji` | :class:`AppEmoji` | :data:`None`
1230+ The object if found, or `default` if provided when not found.
1231+
1232+ Raises
1233+ ------
1234+ :exc:`TypeError`
1235+ Raised when required parameters are missing or invalid types are provided.
1236+ :exc:`InvalidArgument`
1237+ Raised when an unsupported or incompatible object type is used.
1238+ """
1239+ try :
1240+ return await utils .get_or_fetch (
1241+ obj = self ,
1242+ object_type = object_type ,
1243+ object_id = object_id ,
1244+ default = default ,
1245+ )
1246+ except (HTTPException , ValueError , InvalidData ):
1247+ return default
11851248
11861249 # listeners/waiters
11871250
0 commit comments