31
31
import time
32
32
import threading
33
33
import traceback
34
- from typing import TYPE_CHECKING , Any , Awaitable , Callable , Dict , Optional
35
34
import zlib
36
35
37
36
import aiohttp
38
37
39
38
from . import utils
40
- from .utils import MISSING
41
39
from .activity import BaseActivity
42
40
from .enums import SpeakingState
43
41
from .errors import ConnectionClosed , InvalidArgument
44
42
45
- if TYPE_CHECKING :
46
- from .state import ConnectionState
47
- from .voice_client import VoiceClient
48
-
49
43
_log = logging .getLogger (__name__ )
50
44
51
45
__all__ = (
@@ -63,12 +57,15 @@ def __init__(self, shard_id, *, resume=True):
63
57
self .resume = resume
64
58
self .op = 'RESUME' if resume else 'IDENTIFY'
65
59
60
+
66
61
class WebSocketClosure (Exception ):
67
62
"""An exception to make up for the fact that aiohttp doesn't signal closure."""
68
63
pass
69
64
65
+
70
66
EventListener = namedtuple ('EventListener' , 'predicate event result future' )
71
67
68
+
72
69
class GatewayRatelimiter :
73
70
def __init__ (self , count = 110 , per = 60.0 ):
74
71
# The default is 110 to give room for at least 10 heartbeats per minute
@@ -113,7 +110,7 @@ async def block(self):
113
110
114
111
class KeepAliveHandler (threading .Thread ):
115
112
def __init__ (self , * args , ** kwargs ):
116
- ws = kwargs .pop ('ws' )
113
+ ws = kwargs .pop ('ws' , None )
117
114
interval = kwargs .pop ('interval' , None )
118
115
shard_id = kwargs .pop ('shard_id' , None )
119
116
threading .Thread .__init__ (self , * args , ** kwargs )
@@ -193,6 +190,7 @@ def ack(self):
193
190
if self .latency > 10 :
194
191
_log .warning (self .behind_msg , self .shard_id , self .latency )
195
192
193
+
196
194
class VoiceKeepAliveHandler (KeepAliveHandler ):
197
195
def __init__ (self , * args , ** kwargs ):
198
196
super ().__init__ (* args , ** kwargs )
@@ -214,10 +212,12 @@ def ack(self):
214
212
self .latency = ack_time - self ._last_send
215
213
self .recent_ack_latencies .append (self .latency )
216
214
215
+
217
216
class DiscordClientWebSocketResponse (aiohttp .ClientWebSocketResponse ):
218
217
async def close (self , * , code : int = 4000 , message : bytes = b'' ) -> bool :
219
218
return await super ().close (code = code , message = message )
220
219
220
+
221
221
class DiscordWebSocket :
222
222
"""Implements a WebSocket for Discord's gateway v6.
223
223
@@ -255,24 +255,8 @@ class DiscordWebSocket:
255
255
gateway
256
256
The gateway we are currently connected to.
257
257
token
258
- The authentication token for disnake .
258
+ The authentication token for discord .
259
259
"""
260
- if TYPE_CHECKING :
261
- # TODO: figure all the types out and move this elsewhere
262
- token : str
263
- gateway : Any # ???
264
- call_hooks : Callable # ???
265
- shard_id : Optional [int ]
266
- shard_count : int
267
- session_id : Optional [int ]
268
- sequence : Optional [Any ] # ???
269
-
270
- _connection : ConnectionState
271
- _discord_parsers : Dict [str , Callable ] # ???
272
- _dispatch : Callable # ???
273
- _initial_identify : Any # ???
274
- _rate_limiter : GatewayRatelimiter
275
- _max_heartbeat_timeout : int
276
260
277
261
DISPATCH = 0
278
262
HEARTBEAT = 1
@@ -455,8 +439,6 @@ async def received_message(self, msg, /):
455
439
msg = utils ._from_json (msg )
456
440
457
441
_log .debug ('For Shard ID %s: WebSocket Event: %s' , self .shard_id , msg )
458
- self ._dispatch ('socket_response' , msg )
459
-
460
442
event = msg .get ('t' )
461
443
if event :
462
444
self ._dispatch ('socket_event_type' , event )
@@ -701,9 +683,6 @@ async def close(self, code=4000):
701
683
self ._close_code = code
702
684
await self .socket .close (code = code )
703
685
704
- # helper function
705
- async def _hook (* args , ** kwargs ):
706
- pass
707
686
708
687
class DiscordVoiceWebSocket :
709
688
"""Implements the websocket protocol for handling voice connections.
@@ -713,7 +692,7 @@ class DiscordVoiceWebSocket:
713
692
IDENTIFY
714
693
Send only. Starts a new voice session.
715
694
SELECT_PROTOCOL
716
- Send only. Tells disnake what encryption mode and how to connect for voice.
695
+ Send only. Tells discord what encryption mode and how to connect for voice.
717
696
READY
718
697
Receive only. Tells the websocket that the initial connection has completed.
719
698
HEARTBEAT
@@ -748,22 +727,18 @@ class DiscordVoiceWebSocket:
748
727
RESUMED = 9
749
728
CLIENT_CONNECT = 12
750
729
CLIENT_DISCONNECT = 13
751
-
752
- if TYPE_CHECKING :
753
- # TODO: same as in normal websocket gateway
754
- thread_id : int
755
- gateway : DiscordWebSocket
756
-
757
- _connection : VoiceClient
758
- _max_heartbeat_timeout : float
759
-
760
- def __init__ (self , socket , loop , * , hook = None ):
730
+
731
+ def __init__ (self , socket , loop , * , hook = None ):
761
732
self .ws = socket
762
733
self .loop = loop
763
- self ._keep_alive : VoiceKeepAliveHandler = MISSING
764
- self ._close_code = MISSING
765
- self .secret_key = MISSING
766
- self ._hook : Callable [..., Awaitable [Any ]] = hook or _hook
734
+ self ._keep_alive = None
735
+ self ._close_code = None
736
+ self .secret_key = None
737
+ if hook :
738
+ self ._hook = hook
739
+
740
+ async def _hook (self , * args ):
741
+ pass
767
742
768
743
async def send_as_json (self , data ):
769
744
_log .debug ('Sending voice websocket frame: %s.' , data )
0 commit comments