Skip to content

Commit 5014ed0

Browse files
committed
Roll back the changes in gateway.py
1 parent d6127f8 commit 5014ed0

File tree

1 file changed

+19
-44
lines changed

1 file changed

+19
-44
lines changed

disnake/gateway.py

+19-44
Original file line numberDiff line numberDiff line change
@@ -31,21 +31,15 @@
3131
import time
3232
import threading
3333
import traceback
34-
from typing import TYPE_CHECKING, Any, Awaitable, Callable, Dict, Optional
3534
import zlib
3635

3736
import aiohttp
3837

3938
from . import utils
40-
from .utils import MISSING
4139
from .activity import BaseActivity
4240
from .enums import SpeakingState
4341
from .errors import ConnectionClosed, InvalidArgument
4442

45-
if TYPE_CHECKING:
46-
from .state import ConnectionState
47-
from .voice_client import VoiceClient
48-
4943
_log = logging.getLogger(__name__)
5044

5145
__all__ = (
@@ -63,12 +57,15 @@ def __init__(self, shard_id, *, resume=True):
6357
self.resume = resume
6458
self.op = 'RESUME' if resume else 'IDENTIFY'
6559

60+
6661
class WebSocketClosure(Exception):
6762
"""An exception to make up for the fact that aiohttp doesn't signal closure."""
6863
pass
6964

65+
7066
EventListener = namedtuple('EventListener', 'predicate event result future')
7167

68+
7269
class GatewayRatelimiter:
7370
def __init__(self, count=110, per=60.0):
7471
# The default is 110 to give room for at least 10 heartbeats per minute
@@ -113,7 +110,7 @@ async def block(self):
113110

114111
class KeepAliveHandler(threading.Thread):
115112
def __init__(self, *args, **kwargs):
116-
ws = kwargs.pop('ws')
113+
ws = kwargs.pop('ws', None)
117114
interval = kwargs.pop('interval', None)
118115
shard_id = kwargs.pop('shard_id', None)
119116
threading.Thread.__init__(self, *args, **kwargs)
@@ -193,6 +190,7 @@ def ack(self):
193190
if self.latency > 10:
194191
_log.warning(self.behind_msg, self.shard_id, self.latency)
195192

193+
196194
class VoiceKeepAliveHandler(KeepAliveHandler):
197195
def __init__(self, *args, **kwargs):
198196
super().__init__(*args, **kwargs)
@@ -214,10 +212,12 @@ def ack(self):
214212
self.latency = ack_time - self._last_send
215213
self.recent_ack_latencies.append(self.latency)
216214

215+
217216
class DiscordClientWebSocketResponse(aiohttp.ClientWebSocketResponse):
218217
async def close(self, *, code: int = 4000, message: bytes = b'') -> bool:
219218
return await super().close(code=code, message=message)
220219

220+
221221
class DiscordWebSocket:
222222
"""Implements a WebSocket for Discord's gateway v6.
223223
@@ -255,24 +255,8 @@ class DiscordWebSocket:
255255
gateway
256256
The gateway we are currently connected to.
257257
token
258-
The authentication token for disnake.
258+
The authentication token for discord.
259259
"""
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
276260

277261
DISPATCH = 0
278262
HEARTBEAT = 1
@@ -455,8 +439,6 @@ async def received_message(self, msg, /):
455439
msg = utils._from_json(msg)
456440

457441
_log.debug('For Shard ID %s: WebSocket Event: %s', self.shard_id, msg)
458-
self._dispatch('socket_response', msg)
459-
460442
event = msg.get('t')
461443
if event:
462444
self._dispatch('socket_event_type', event)
@@ -701,9 +683,6 @@ async def close(self, code=4000):
701683
self._close_code = code
702684
await self.socket.close(code=code)
703685

704-
# helper function
705-
async def _hook(*args, **kwargs):
706-
pass
707686

708687
class DiscordVoiceWebSocket:
709688
"""Implements the websocket protocol for handling voice connections.
@@ -713,7 +692,7 @@ class DiscordVoiceWebSocket:
713692
IDENTIFY
714693
Send only. Starts a new voice session.
715694
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.
717696
READY
718697
Receive only. Tells the websocket that the initial connection has completed.
719698
HEARTBEAT
@@ -748,22 +727,18 @@ class DiscordVoiceWebSocket:
748727
RESUMED = 9
749728
CLIENT_CONNECT = 12
750729
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):
761732
self.ws = socket
762733
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
767742

768743
async def send_as_json(self, data):
769744
_log.debug('Sending voice websocket frame: %s.', data)

0 commit comments

Comments
 (0)