Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,8 @@ These changes are available on the `master` branch, but have not yet been releas
([#2781](https://github.com/Pycord-Development/pycord/pull/2781))
- Fixed `VoiceClient` crashing randomly while receiving audio
([#2800](https://github.com/Pycord-Development/pycord/pull/2800))
- Fixed `VoiceClient.connect` failing to do initial connection.
([#2812](https://github.com/Pycord-Development/pycord/pull/2812))

### Changed

Expand Down
22 changes: 19 additions & 3 deletions discord/gateway.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import traceback
import zlib
from collections import deque, namedtuple
from typing import TYPE_CHECKING

import aiohttp

Expand Down Expand Up @@ -208,6 +209,9 @@ def ack(self):


class VoiceKeepAliveHandler(KeepAliveHandler):
if TYPE_CHECKING:
ws: DiscordVoiceWebSocket

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.recent_ack_latencies = deque(maxlen=20)
Expand All @@ -216,7 +220,10 @@ def __init__(self, *args, **kwargs):
self.behind_msg = "High socket latency, shard ID %s heartbeat is %.1fs behind"

def get_payload(self):
return {"op": self.ws.HEARTBEAT, "d": int(time.time() * 1000)}
return {
"op": self.ws.HEARTBEAT,
"d": {"t": int(time.time() * 1000), "seq_ack": self.ws.seq_ack},
}

def ack(self):
ack_time = time.perf_counter()
Expand Down Expand Up @@ -784,6 +791,7 @@ def __init__(self, socket, loop, *, hook=None):
self._close_code = None
self.secret_key = None
self.ssrc_map = {}
self.seq_ack: int = -1
if hook:
self._hook = hook

Expand Down Expand Up @@ -824,7 +832,7 @@ async def identify(self):
@classmethod
async def from_client(cls, client, *, resume=False, hook=None):
"""Creates a voice websocket for the :class:`VoiceClient`."""
gateway = f"wss://{client.endpoint}/?v=4"
gateway = f"wss://{client.endpoint}/?v=8"
http = client._state.http
socket = await http.ws_connect(gateway, compress=15)
ws = cls(socket, loop=client.loop, hook=hook)
Expand Down Expand Up @@ -860,14 +868,22 @@ async def client_connect(self):
await self.send_as_json(payload)

async def speak(self, state=SpeakingState.voice):
payload = {"op": self.SPEAKING, "d": {"speaking": int(state), "delay": 0}}
payload = {
"op": self.SPEAKING,
"d": {
"speaking": int(state),
"delay": 0,
},
"seq": self.seq_ack,
}

await self.send_as_json(payload)

async def received_message(self, msg):
_log.debug("Voice websocket frame received: %s", msg)
op = msg["op"]
data = msg.get("d")
self.seq_ack = data.get("seq", self.seq_ack)

if op == self.READY:
await self.initial_connection(data)
Expand Down
2 changes: 1 addition & 1 deletion discord/voice_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ async def on_voice_server_update(self, data: VoiceServerUpdatePayload) -> None:
)
return

self.endpoint, _, _ = endpoint.rpartition(":")
self.endpoint = endpoint
if self.endpoint.startswith("wss://"):
# Just in case, strip it off since we're going to add it later
self.endpoint = self.endpoint[6:]
Expand Down
Loading