@@ -147,6 +147,7 @@ def __init__(
147
147
148
148
self ._login_called : bool = False
149
149
self ._has_closed : bool = False
150
+ self ._dump_tokens : bool = True
150
151
151
152
# Websockets for EventSub
152
153
self ._websockets : dict [str , dict [str , Websocket ]] = defaultdict (dict )
@@ -256,7 +257,7 @@ async def setup_hook(self) -> None:
256
257
"""
257
258
...
258
259
259
- async def login (self , * , token : str | None = None ) -> None :
260
+ async def login (self , * , token : str | None = None , load_tokens : bool = True , dump_tokens : bool = True ) -> None :
260
261
"""Method to login the client and generate or store an app token.
261
262
262
263
This method is called automatically when using :meth:`~.start`.
@@ -273,11 +274,18 @@ async def login(self, *, token: str | None = None) -> None:
273
274
----------
274
275
token: str | None
275
276
An optional app token to use instead of generating one automatically.
277
+ load_tokens: bool
278
+ Optional bool which indicates whether the :class:`Client` should call :meth:`.load_tokens` during
279
+ login automatically. Defaults to ``True``.
280
+ dump_tokens: bool
281
+ Optional bool which inicates whether the :class:`Client` should call :meth:`.dump_tokens` during the
282
+ :meth:`.close` automatically. Defaults to ``True``.
276
283
"""
277
284
if self ._login_called :
278
285
return
279
286
280
287
self ._login_called = True
288
+ self ._dump_tokens = dump_tokens
281
289
282
290
if not token :
283
291
payload : ClientCredentialsPayload = await self ._http .client_credentials_token ()
@@ -288,8 +296,9 @@ async def login(self, *, token: str | None = None) -> None:
288
296
289
297
self ._http ._app_token = token
290
298
291
- async with self ._http ._token_lock :
292
- await self .load_tokens ()
299
+ if load_tokens :
300
+ async with self ._http ._token_lock :
301
+ await self .load_tokens ()
293
302
294
303
await self .setup_hook ()
295
304
@@ -299,7 +308,14 @@ async def __aenter__(self) -> Self:
299
308
async def __aexit__ (self , * _ : Any ) -> None :
300
309
await self .close ()
301
310
302
- async def start (self , token : str | None = None , * , with_adapter : bool = True ) -> None :
311
+ async def start (
312
+ self ,
313
+ token : str | None = None ,
314
+ * ,
315
+ with_adapter : bool = True ,
316
+ load_tokens : bool = True ,
317
+ dump_tokens : bool = True ,
318
+ ) -> None :
303
319
"""|coro|
304
320
305
321
Method to login and run the `Client` asynchronously on an already running event loop.
@@ -317,7 +333,12 @@ async def start(self, token: str | None = None, *, with_adapter: bool = True) ->
317
333
An optional app token to use instead of generating one automatically.
318
334
with_adapter: bool
319
335
Whether to start and run a web adapter. Defaults to `True`. See: ... for more information.
320
-
336
+ load_tokens: bool
337
+ Optional bool which indicates whether the :class:`Client` should call :meth:`.load_tokens` during
338
+ :meth:`.login` automatically. Defaults to ``True``.
339
+ dump_tokens: bool
340
+ Optional bool which inicates whether the :class:`Client` should call :meth:`.dump_tokens` during the
341
+ :meth:`.close` automatically. Defaults to ``True``.
321
342
322
343
Examples
323
344
--------
@@ -335,7 +356,7 @@ async def main() -> None:
335
356
await client.start()
336
357
"""
337
358
self .__waiter .clear ()
338
- await self .login (token = token )
359
+ await self .login (token = token , load_tokens = load_tokens , dump_tokens = dump_tokens )
339
360
340
361
if with_adapter :
341
362
await self ._adapter .run ()
@@ -350,7 +371,14 @@ async def main() -> None:
350
371
self ._ready_event .clear ()
351
372
await self .close ()
352
373
353
- def run (self , token : str | None = None , * , with_adapter : bool = True ) -> None :
374
+ def run (
375
+ self ,
376
+ token : str | None = None ,
377
+ * ,
378
+ with_adapter : bool = True ,
379
+ load_tokens : bool = True ,
380
+ dump_tokens : bool = True ,
381
+ ) -> None :
354
382
"""Method to login the client and create a continuously running event loop.
355
383
356
384
The behaviour of this method is similar to :meth:`~.start` but instead of being used in an already running
@@ -374,7 +402,12 @@ def run(self, token: str | None = None, *, with_adapter: bool = True) -> None:
374
402
An optional app token to use instead of generating one automatically.
375
403
with_adapter: bool
376
404
Whether to start and run a web adapter. Defaults to `True`. See: ... for more information.
377
-
405
+ load_tokens: bool
406
+ Optional bool which indicates whether the :class:`Client` should call :meth:`.load_tokens` during
407
+ :meth:`.login` automatically. Defaults to ``True``.
408
+ dump_tokens: bool
409
+ Optional bool which inicates whether the :class:`Client` should call :meth:`.dump_tokens` during the
410
+ :meth:`.close` automatically. Defaults to ``True``.
378
411
379
412
Examples
380
413
--------
@@ -387,7 +420,7 @@ def run(self, token: str | None = None, *, with_adapter: bool = True) -> None:
387
420
388
421
async def run () -> None :
389
422
async with self :
390
- await self .start (token = token , with_adapter = with_adapter )
423
+ await self .start (token = token , with_adapter = with_adapter , load_tokens = load_tokens , dump_tokens = dump_tokens )
391
424
392
425
try :
393
426
asyncio .run (run ())
@@ -429,8 +462,9 @@ async def close(self) -> None:
429
462
for socket in sockets :
430
463
await socket .close ()
431
464
432
- async with self ._http ._token_lock :
433
- await self .dump_tokens ()
465
+ if self ._dump_tokens :
466
+ async with self ._http ._token_lock :
467
+ await self .dump_tokens ()
434
468
435
469
self ._http .cleanup ()
436
470
self .__waiter .set ()
@@ -586,8 +620,9 @@ async def load_tokens(self, path: str | None = None, /) -> None:
586
620
587
621
.. note::
588
622
589
- This method is always called by the client during :meth:`~.login` but **before**
590
- :meth:`~.setup_hook`.
623
+ This method is called by the client during :meth:`~.login` but **before**
624
+ :meth:`~.setup_hook` when the ``load_tokens`` keyword-argument
625
+ is ``True`` in either, :meth:`.run`, :meth:`.start` or :meth:`.login` (Default).
591
626
592
627
You can override this method to implement your own token loading logic into the client, such as from a database.
593
628
@@ -627,7 +662,8 @@ async def dump_tokens(self, path: str | None = None, /) -> None:
627
662
628
663
.. note::
629
664
630
- This method is always called by the client when it is gracefully closed.
665
+ This method is called by the client when it is gracefully closed and the ``dump_tokens`` keyword-argument
666
+ is ``True`` in either, :meth:`.run`, :meth:`.start` or :meth:`.login` (Default).
631
667
632
668
.. note::
633
669
0 commit comments