interactions-py / interactions.py

A highly extensible, easy to use, and feature complete bot framework for Discord
https://interactions-py.github.io/interactions.py/
MIT License
833 stars 185 forks source link

[BUG] Incorrect heartbeat event payload causes VoiceGateway websocket to be closed with code 4020 #1726

Closed SirTurlock closed 2 weeks ago

SirTurlock commented 1 month ago

Library Version

5.13.1

Describe the Bug

In the past few days I have started receiving an exception whenever the bot has connected to a voice channel causing the voice (playback) to abruptly cut out. The exception is/was: interactions.client.errors.VoiceWebSocketClosed: The Websocket closed with code: 4020 - Unknown Error The root cause of this problem was identified on the Discord Developer server here (for more info, etc.).

Apparently there has been a silent change in the Discord backend because it no longer accepts a string or a float for the d parameter of the payload in case of a heartbeat event. Simply changing the way the random sequence is generated from random.uniform(0.0, 1.0) to random.getrandbits(64) in interactions/api/voice/voice_gateway.py fixes the issue. Fixed version:

    async def send_heartbeat(self) -> None:
        await self.send_json({"op": OP.HEARTBEAT, "d": random.getrandbits(64)})
        self.logger.debug("❤ Voice Connection is sending Heartbeat")

Steps to Reproduce

Connect to a voice channel. As soon as a heartbeat is sent the ws will close with code 4020.

Expected Results

Working voice functionality.

Minimal Reproducible Code

No response

Traceback

Task exception was never retrieved
future: <Task finished name='Task-88' coro=<ActiveVoiceState._ws_connect() done, defined at U:\src\sbdev\.venv\Lib\site-packages\interactions\models\internal\active_voice_state.py:105> exception=VoiceWebSocketClosed('The Websocket closed with code: 4020 - Unknown Error')>
Traceback (most recent call last):
  File "U:\src\sbdev\.venv\Lib\site-packages\interactions\models\internal\active_voice_state.py", line 109, in _ws_connect
    await self.ws.run()
  File "U:\src\sbdev\.venv\Lib\site-packages\interactions\api\voice\voice_gateway.py", line 85, in run
    msg = await receiving
          ^^^^^^^^^^^^^^^
  File "U:\src\sbdev\.venv\Lib\site-packages\interactions\api\voice\voice_gateway.py", line 121, in receive
    raise VoiceWebSocketClosed(resp.data)
interactions.client.errors.VoiceWebSocketClosed: The Websocket closed with code: 4020 - Unknown Error

Checklist

Additional Information

No response

AstreaTSS commented 1 month ago

Why not PR your fix?