Skip to content

Commit c8a44c4

Browse files
committed
Add Client.user and fetch User on login
1 parent ae831c2 commit c8a44c4

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

twitchio/client.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,10 @@ class Client:
112112
When this is not provided, it will default to a :class:`twitchio.AiohttpAdapter` with default settings.
113113
114114
When requiring an adapter for use with EventSub, you must provide an adapter with the correct settings set.
115+
fetch_client_user: bool
116+
An optional bool indicating whether to fetch and cache the client/bot accounts own :class:`.User` object to use with
117+
:attr:`.user`.
118+
Defaults to ``True``. You must pass ``bot_id`` for this parameter to have any effect.
115119
"""
116120

117121
def __init__(
@@ -142,6 +146,10 @@ def __init__(
142146
self._adapter = adapter()
143147
self._adapter.client = self
144148

149+
# Own Client User. Set in login...
150+
self._fetch_self: bool = options.get("fetch_client_user", True)
151+
self._user: User | PartialUser | None = None
152+
145153
self._listeners: dict[str, set[Callable[..., Coroutine[Any, Any, None]]]] = defaultdict(set)
146154
self._wait_fors: dict[str, set[EventWaiter]] = defaultdict(set)
147155

@@ -192,6 +200,21 @@ def bot_id(self) -> str | None:
192200
"""
193201
return self._bot_id
194202

203+
@property
204+
def user(self) -> User | PartialUser | None:
205+
"""Property which returns the :class:`.User` or :class:`.PartialUser` associated with with the Client/Bot.
206+
207+
In most cases this will be a :class:`.User` object. Could be :class:`.PartialUser` when passing ``False`` to the
208+
``fetch_client_user`` keyword parameter of Client.
209+
210+
Could be ``None`` if no ``bot_id`` was passed to the Client constructor.
211+
212+
.. important::
213+
214+
If ``bot_id`` has not been passed to the constructor of :class:`.Client` this will return ``None``.
215+
"""
216+
return self._user
217+
195218
async def event_error(self, payload: EventErrorPayload) -> None:
196219
"""
197220
Event called when an error occurs in an event or event listener.
@@ -300,6 +323,11 @@ async def login(self, *, token: str | None = None, load_tokens: bool = True, sav
300323
async with self._http._token_lock:
301324
await self.load_tokens()
302325

326+
if self._bot_id:
327+
logger.debug("Fetching Clients self user for %r", self)
328+
partial = PartialUser(id=self._bot_id, http=self._http)
329+
self._user = partial if not self._fetch_self else await partial.user()
330+
303331
await self.setup_hook()
304332

305333
async def __aenter__(self) -> Self:

twitchio/types_/options.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ class ClientOptions(TypedDict, total=False):
3636
scopes: Scopes | None
3737
session: aiohttp.ClientSession | None
3838
adapter: NotRequired[BaseAdapter]
39+
fetch_client_user: NotRequired[bool]
3940

4041

4142
WaitPredicateT = Callable[..., Coroutine[Any, Any, bool]]

0 commit comments

Comments
 (0)