Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(http): improve HTTPClient rate limiting behavior #1084

Draft
wants to merge 8 commits into
base: master
Choose a base branch
from
7 changes: 5 additions & 2 deletions disnake/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,7 @@ def __init__(
connector,
proxy=proxy,
proxy_auth=proxy_auth,
unsync_clock=assume_unsync_clock,
# unsync_clock=assume_unsync_clock,
loop=self.loop,
)

Expand Down Expand Up @@ -432,6 +432,7 @@ def __init__(
self._first_connect: asyncio.Event = asyncio.Event()
self._connection._get_websocket = self._get_websocket
self._connection._get_client = lambda: self
self._token: str | None = None
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we still support python <= 3.10, we can't use this hint or it will raise an error
self._token: Optional[str] would be more appropriate

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We actually can as long as there is a from __future__ import annotations at the top, but i agree, albeit for a different reason: it would look inconsistent with the rest of the codebase


if VoiceClient.warn_nacl:
VoiceClient.warn_nacl = False
Expand Down Expand Up @@ -862,7 +863,9 @@ async def login(self, token: str) -> None:
if not isinstance(token, str):
raise TypeError(f"token must be of type str, got {type(token).__name__} instead")

data = await self.http.static_login(token.strip())
self._token = token.strip()

data = await self.http.static_login(f"Bot {self._token}")
self._connection.user = ClientUser(state=self._connection, data=data)

async def connect(
Expand Down
9 changes: 9 additions & 0 deletions disnake/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"NoMoreItems",
"GatewayNotFound",
"HTTPException",
"Unauthorized",
"Forbidden",
"NotFound",
"DiscordServerError",
Expand Down Expand Up @@ -134,6 +135,14 @@ def __init__(
super().__init__(fmt.format(self.response, self.code, self.text))


class Unauthorized(HTTPException):
"""Exception that's raised for when status code 401 occurs.
Subclass of :exc:`HTTPException`
"""

pass


class Forbidden(HTTPException):
"""Exception that's raised for when status code 403 occurs.

Expand Down
2 changes: 1 addition & 1 deletion disnake/gateway.py
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,7 @@ async def from_client(
ws = cls(socket, loop=client.loop)

# dynamically add attributes needed
ws.token = client.http.token # type: ignore
ws.token = client._token # type: ignore
ws._connection = client._connection
ws._discord_parsers = client._connection.parsers
ws._dispatch = client.dispatch
Expand Down
Loading
Loading