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,10 @@ 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  {
224+             "op" : self .ws .HEARTBEAT ,
225+             "d" : {"t" : int (time .time () *  1000 ), "seq_ack" : self .ws .seq_ack },
226+         }
220227
221228    def  ack (self ):
222229        ack_time  =  time .perf_counter ()
@@ -784,6 +791,7 @@ def __init__(self, socket, loop, *, hook=None):
784791        self ._close_code  =  None 
785792        self .secret_key  =  None 
786793        self .ssrc_map  =  {}
794+         self .seq_ack : int  =  - 1 
787795        if  hook :
788796            self ._hook  =  hook 
789797
@@ -804,6 +812,9 @@ async def resume(self):
804812                "token" : state .token ,
805813                "server_id" : str (state .server_id ),
806814                "session_id" : state .session_id ,
815+                 # this seq_ack will allow for us to do buffered resume, which is, receive the 
816+                 # lost voice packets while trying to resume the reconnection 
817+                 "seq_ack" : self .seq_ack ,
807818            },
808819        }
809820        await  self .send_as_json (payload )
@@ -824,7 +835,7 @@ async def identify(self):
824835    @classmethod  
825836    async  def  from_client (cls , client , * , resume = False , hook = None ):
826837        """Creates a voice websocket for the :class:`VoiceClient`.""" 
827-         gateway  =  f"wss://{ client .endpoint }  /?v=4 " 
838+         gateway  =  f"wss://{ client .endpoint }  /?v=8 " 
828839        http  =  client ._state .http 
829840        socket  =  await  http .ws_connect (gateway , compress = 15 )
830841        ws  =  cls (socket , loop = client .loop , hook = hook )
@@ -860,14 +871,21 @@ async def client_connect(self):
860871        await  self .send_as_json (payload )
861872
862873    async  def  speak (self , state = SpeakingState .voice ):
863-         payload  =  {"op" : self .SPEAKING , "d" : {"speaking" : int (state ), "delay" : 0 }}
874+         payload  =  {
875+             "op" : self .SPEAKING ,
876+             "d" : {
877+                 "speaking" : int (state ),
878+                 "delay" : 0 ,
879+             },
880+         }
864881
865882        await  self .send_as_json (payload )
866883
867884    async  def  received_message (self , msg ):
868885        _log .debug ("Voice websocket frame received: %s" , msg )
869886        op  =  msg ["op" ]
870887        data  =  msg .get ("d" )
888+         self .seq_ack  =  data .get ("seq" , self .seq_ack )
871889
872890        if  op  ==  self .READY :
873891            await  self .initial_connection (data )
0 commit comments