Added VoiceClient.latency and VoiceClient.average_latency

This also implements the heartbeating a bit more consistent to the
official Discord client.
This commit is contained in:
Fwf
2020-01-25 16:24:44 +00:00
committed by Rapptz
parent 1b0e806245
commit fa34d357a1
2 changed files with 39 additions and 5 deletions

View File

@ -57,7 +57,6 @@ try:
except ImportError:
has_nacl = False
log = logging.getLogger(__name__)
class VoiceClient:
@ -207,6 +206,22 @@ class VoiceClient:
self._handshake_complete.set()
@property
def latency(self):
""":class:`float`: Latency between a HEARTBEAT and a HEARTBEAT_ACK in seconds.
This could be referred to as the Discord Voice WebSocket latency and is
an analogue of user's voice latencies as seen in the Discord client.
"""
ws = self.ws
return float("inf") if not ws else ws.latency
@property
def average_latency(self):
""":class:`float`: Average of most recent 20 HEARTBEAT latencies in seconds."""
ws = self.ws
return float("inf") if not ws else ws.average_latency
async def connect(self, *, reconnect=True, _tries=0, do_handshake=True):
log.info('Connecting to voice...')
try:
@ -342,7 +357,6 @@ class VoiceClient:
return header + box.encrypt(bytes(data), bytes(nonce)).ciphertext + nonce[:4]
def play(self, source, *, after=None):
"""Plays an :class:`AudioSource`.