Skip to content

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

Closed
@SirTurlock

Description

@SirTurlock

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

  • I have searched the open issues for duplicates.
  • I have shown the entire traceback, if possible.
  • I have removed my token from display, if visible.
  • I have attempted to debug this myself, and I believe this issue is with the library

Additional Information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions