Skip to content

Commit

Permalink
Several bug fixes
Browse files Browse the repository at this point in the history
  - Fix some typehints in REST
  - Fix logging message in bot
  - If we are expecting a BINARY, but get something different, we now error out
  • Loading branch information
davfsa committed Sep 8, 2020
1 parent 4846fef commit 0f39b7f
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 13 deletions.
6 changes: 3 additions & 3 deletions hikari/api/rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -783,8 +783,8 @@ async def create_message(
@abc.abstractmethod
async def edit_message(
self,
channel: typing.Union[snowflakes.SnowflakeishOr[channels.TextChannel]],
message: typing.Union[snowflakes.SnowflakeishOr[messages_.Message]],
channel: snowflakes.SnowflakeishOr[channels.TextChannel],
message: snowflakes.SnowflakeishOr[messages_.Message],
content: undefined.UndefinedOr[typing.Any] = undefined.UNDEFINED,
*,
embed: undefined.UndefinedNoneOr[embeds_.Embed] = undefined.UNDEFINED,
Expand Down Expand Up @@ -1981,7 +1981,7 @@ async def create_guild_category(
async def reposition_channels(
self,
guild: snowflakes.SnowflakeishOr[guilds.PartialGuild],
positions: typing.Mapping[int, typing.Union[snowflakes.SnowflakeishOr[channels.GuildChannel]]],
positions: typing.Mapping[int, snowflakes.SnowflakeishOr[channels.GuildChannel]],
) -> None:
...

Expand Down
11 changes: 8 additions & 3 deletions hikari/impl/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -665,9 +665,14 @@ async def start(

if requirements.session_start_limit.remaining < len(shard_ids) and not ignore_session_start_limit:
_LOGGER.critical(
"would have started %s session(s), but you only have %s remaining until %s. Starting more sessions "
"than you are allowed to start may result in your token being reset. To skip this message, "
"use bot.run(..., ignore_session_start_limit=True) or bot.start(..., ignore_session_start_limit=True)"
"would have started %s session%s, but you only have %s session%s remaining until %s. Starting more "
"sessions than you are allowed to start may result in your token being reset. To skip this message, "
"use bot.run(..., ignore_session_start_limit=True) or bot.start(..., ignore_session_start_limit=True)",
len(shard_ids),
"s" if len(shard_ids) != 1 else "",
requirements.session_start_limit.remaining,
"s" if requirements.session_start_limit.remaining != 1 else "",
requirements.session_start_limit.reset_at,
)
raise errors.GatewayError("Attempted to start more sessions than were allowed in the given time-window")

Expand Down
6 changes: 3 additions & 3 deletions hikari/impl/rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -1004,8 +1004,8 @@ async def create_message(

async def edit_message(
self,
channel: typing.Union[snowflakes.SnowflakeishOr[channels.TextChannel]],
message: typing.Union[snowflakes.SnowflakeishOr[messages_.Message]],
channel: snowflakes.SnowflakeishOr[channels.TextChannel],
message: snowflakes.SnowflakeishOr[messages_.Message],
content: undefined.UndefinedOr[typing.Any] = undefined.UNDEFINED,
*,
embed: undefined.UndefinedNoneOr[embeds_.Embed] = undefined.UNDEFINED,
Expand Down Expand Up @@ -1902,7 +1902,7 @@ async def _create_guild_channel(
async def reposition_channels(
self,
guild: snowflakes.SnowflakeishOr[guilds.PartialGuild],
positions: typing.Mapping[int, typing.Union[snowflakes.SnowflakeishOr[channels.GuildChannel]]],
positions: typing.Mapping[int, snowflakes.SnowflakeishOr[channels.GuildChannel]],
) -> None:
route = routes.POST_GUILD_CHANNELS.compile(guild=guild)
body = [{"id": str(int(channel)), "position": pos} for pos, channel in positions.items()]
Expand Down
14 changes: 10 additions & 4 deletions hikari/impl/shard.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,9 +149,9 @@ async def _receive_and_check(self, timeout: typing.Optional[float], /) -> str:
self._logger.error("connection closed with code %s (%s)", close_code, reason)

can_reconnect = close_code < 4000 or close_code in (
errors.ShardCloseCode.UNKNOWN_ERROR,
errors.ShardCloseCode.DECODE_ERROR,
errors.ShardCloseCode.INVALID_SEQ,
errors.ShardCloseCode.UNKNOWN_ERROR,
errors.ShardCloseCode.SESSION_TIMEOUT,
errors.ShardCloseCode.RATE_LIMITED,
)
Expand All @@ -162,13 +162,18 @@ async def _receive_and_check(self, timeout: typing.Optional[float], /) -> str:
elif message.type == aiohttp.WSMsgType.CLOSING or message.type == aiohttp.WSMsgType.CLOSED:
raise asyncio.CancelledError("Socket closed")

elif len(buff) != 0 and message.type != aiohttp.WSMsgType.BINARY:
raise errors.GatewayError(f"Unexpected message type received {message.type.name}, expected BINARY")

elif message.type == aiohttp.WSMsgType.BINARY:
buff += message.data
buff.extend(message.data)

if buff.endswith(b"\x00\x00\xff\xff"):
return self._zlib.decompress(buff).decode("utf-8")

elif message.type == aiohttp.WSMsgType.TEXT:
return message.data # type: ignore

else:
# Assume exception for now.
ex = self.exception()
Expand All @@ -192,8 +197,8 @@ async def connect(
*,
debug: bool,
http_config: config.HTTPSettings,
logger: logging.Logger,
proxy_config: config.ProxySettings,
logger: logging.Logger,
url: str,
) -> typing.AsyncGenerator[_V6GatewayTransport, None]:
"""Generate a single-use websocket connection.
Expand Down Expand Up @@ -810,9 +815,10 @@ async def _run_once(self) -> bool:
)
return True
return False
finally:

finally:
heartbeat_task.cancel()

finally:
self._ws = None
if dispatch_disconnect:
Expand Down

0 comments on commit 0f39b7f

Please sign in to comment.