3535import  traceback 
3636import  zlib 
3737from  collections  import  deque , namedtuple 
38+ from  typing  import  TYPE_CHECKING 
3839
3940import  aiohttp 
4041
@@ -208,6 +209,9 @@ def ack(self):
208209
209210
210211class  VoiceKeepAliveHandler (KeepAliveHandler ):
212+     if  TYPE_CHECKING :
213+         ws : DiscordVoiceWebSocket 
214+ 
211215    def  __init__ (self , * args , ** kwargs ):
212216        super ().__init__ (* args , ** kwargs )
213217        self .recent_ack_latencies  =  deque (maxlen = 20 )
@@ -216,7 +220,7 @@ def __init__(self, *args, **kwargs):
216220        self .behind_msg  =  "High socket latency, shard ID %s heartbeat is %.1fs behind" 
217221
218222    def  get_payload (self ):
219-         return  {"op" : self .ws .HEARTBEAT , "d" : int (time .time () *  1000 )}
223+         return  {"op" : self .ws .HEARTBEAT , "d" : { "t" :  int (time .time () *  1000 ),  "seq_ack" :  self . ws . seq_ack } }
220224
221225    def  ack (self ):
222226        ack_time  =  time .perf_counter ()
@@ -784,6 +788,7 @@ def __init__(self, socket, loop, *, hook=None):
784788        self ._close_code  =  None 
785789        self .secret_key  =  None 
786790        self .ssrc_map  =  {}
791+         self .seq_ack : int  =  - 1 
787792        if  hook :
788793            self ._hook  =  hook 
789794
@@ -824,7 +829,7 @@ async def identify(self):
824829    @classmethod  
825830    async  def  from_client (cls , client , * , resume = False , hook = None ):
826831        """Creates a voice websocket for the :class:`VoiceClient`.""" 
827-         gateway  =  f"wss://{ client .endpoint }  /?v=4 " 
832+         gateway  =  f"wss://{ client .endpoint }  /?v=8 " 
828833        http  =  client ._state .http 
829834        socket  =  await  http .ws_connect (gateway , compress = 15 )
830835        ws  =  cls (socket , loop = client .loop , hook = hook )
@@ -860,14 +865,22 @@ async def client_connect(self):
860865        await  self .send_as_json (payload )
861866
862867    async  def  speak (self , state = SpeakingState .voice ):
863-         payload  =  {"op" : self .SPEAKING , "d" : {"speaking" : int (state ), "delay" : 0 }}
868+         payload  =  {
869+             "op" : self .SPEAKING ,
870+             "d" : {
871+                 "speaking" : int (state ),
872+                 "delay" : 0 ,
873+             },
874+             "seq" : self .seq_ack ,
875+         }
864876
865877        await  self .send_as_json (payload )
866878
867879    async  def  received_message (self , msg ):
868880        _log .debug ("Voice websocket frame received: %s" , msg )
869881        op  =  msg ["op" ]
870882        data  =  msg .get ("d" )
883+         self .seq_ack  =  data .get ("seq" , self .seq_ack )
871884
872885        if  op  ==  self .READY :
873886            await  self .initial_connection (data )
0 commit comments