Skip to content

Commit d2f7668

Browse files
Catalyst4222pre-commit-ci[bot]Damego
authored
refactor: add an ability to delay bot's token (#1216)
* fix: Set the websocket's token without referencing the httpclient * fix: correct the websocket's headers * ci: correct from checks. * feat: allow setting the token in .start * ci: correct from checks. * fix: bugs from merge/fastforward * Update interactions/client/bot.py Co-authored-by: Damego <damego.dev@gmail.com> * Apply suggestions from code review Co-authored-by: Damego <damego.dev@gmail.com> * Update interactions/client/bot.py * Update interactions/client/bot.py Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Damego <damego.dev@gmail.com>
1 parent b4154b8 commit d2f7668

File tree

1 file changed

+21
-5
lines changed

1 file changed

+21
-5
lines changed

interactions/client/bot.py

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ class Client:
7171

7272
def __init__(
7373
self,
74-
token: str,
74+
token: Optional[str] = None,
7575
cache_limits: Optional[Dict[type, int]] = None,
7676
intents: Intents = Intents.DEFAULT,
7777
shards: Optional[List[Tuple[int]]] = None,
@@ -189,11 +189,15 @@ def latency(self) -> float:
189189

190190
return self._websocket.latency * 1000
191191

192-
def start(self) -> None:
193-
"""Starts the client session."""
192+
def start(self, token: Optional[str] = None) -> None:
193+
"""
194+
Starts the client session.
195+
196+
:param Optional[str] token: The token of bot.
197+
"""
194198

195199
try:
196-
self._loop.run_until_complete(self._ready())
200+
self._loop.run_until_complete(self._ready(token=token))
197201
except (CancelledError, Exception) as e:
198202
self._loop.run_until_complete(self._logout())
199203
raise e from e
@@ -396,7 +400,7 @@ def __check_options(command, data):
396400

397401
return clean, _command
398402

399-
async def _ready(self) -> None:
403+
async def _ready(self, token: Optional[str] = None) -> None:
400404
"""
401405
Prepares the client with an internal "ready" check to ensure
402406
that all conditions have been met in a chronological order:
@@ -414,7 +418,19 @@ async def _ready(self) -> None:
414418
| |___ SYNCHRONIZE
415419
| |___ CALLBACK
416420
LOOP
421+
422+
:param Optional[str] token: The token of bot.
417423
"""
424+
if self._http and token and self._http is not token:
425+
raise RuntimeError("You cannot pass a token to the bot twice!")
426+
elif not (self._http or token):
427+
raise RuntimeError("No token was passed to the bot!")
428+
429+
if token:
430+
self._token = token
431+
self._http = token
432+
self._websocket._http = token # Update the websockets token if it wasn't set before
433+
418434
if isinstance(self._http, str):
419435
self._http = HTTPClient(self._http, self._cache)
420436

0 commit comments

Comments
 (0)