Skip to content

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

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

Closed
4 tasks done
SirTurlock opened this issue Aug 5, 2024 · 1 comment

Comments

@SirTurlock
Copy link
Contributor

SirTurlock commented Aug 5, 2024

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

@SirTurlock SirTurlock changed the title [BUG] Incorrect heartbeat event causes VoiceGateway websocket to be closed with code 4020 [BUG] Incorrect heartbeat event payload causes VoiceGateway websocket to be closed with code 4020 Aug 5, 2024
@AstreaTSS
Copy link
Member

Why not PR your fix?

SirTurlock added a commit to SirTurlock/interactions.py that referenced this issue Aug 5, 2024
…t is sent

Fixes interactions-py#1726
VoiceGateway no longer accepts a string or a float in the heartbeat nonce and instead immediately disconnects the client with code 4020.
Fixed send_heartbeat to use an uint64 instead of a float for the nonce.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants