Browse Source

Add vws message hook

pull/6948/head
Imayhaveborkedit 4 years ago
committed by GitHub
parent
commit
8e08bd6af2
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 13
      discord/gateway.py

13
discord/gateway.py

@ -708,13 +708,18 @@ class DiscordVoiceWebSocket:
CLIENT_CONNECT = 12 CLIENT_CONNECT = 12
CLIENT_DISCONNECT = 13 CLIENT_DISCONNECT = 13
def __init__(self, socket, loop): def __init__(self, socket, loop, *, hook=None):
self.ws = socket self.ws = socket
self.loop = loop self.loop = loop
self._keep_alive = None self._keep_alive = None
self._close_code = None self._close_code = None
self.secret_key = None self.secret_key = None
if hook:
self._hook = hook
async def _hook(self, msg):
pass
async def send_as_json(self, data): async def send_as_json(self, data):
log.debug('Sending voice websocket frame: %s.', data) log.debug('Sending voice websocket frame: %s.', data)
await self.ws.send_str(utils.to_json(data)) await self.ws.send_str(utils.to_json(data))
@ -747,12 +752,12 @@ class DiscordVoiceWebSocket:
await self.send_as_json(payload) await self.send_as_json(payload)
@classmethod @classmethod
async def from_client(cls, client, *, resume=False): async def from_client(cls, client, *, resume=False, hook=None):
"""Creates a voice websocket for the :class:`VoiceClient`.""" """Creates a voice websocket for the :class:`VoiceClient`."""
gateway = 'wss://' + client.endpoint + '/?v=4' gateway = 'wss://' + client.endpoint + '/?v=4'
http = client._state.http http = client._state.http
socket = await http.ws_connect(gateway, compress=15) socket = await http.ws_connect(gateway, compress=15)
ws = cls(socket, loop=client.loop) ws = cls(socket, loop=client.loop, hook=hook)
ws.gateway = gateway ws.gateway = gateway
ws._connection = client ws._connection = client
ws._max_heartbeat_timeout = 60.0 ws._max_heartbeat_timeout = 60.0
@ -819,6 +824,8 @@ class DiscordVoiceWebSocket:
interval = data['heartbeat_interval'] / 1000.0 interval = data['heartbeat_interval'] / 1000.0
self._keep_alive = VoiceKeepAliveHandler(ws=self, interval=min(interval, 5.0)) self._keep_alive = VoiceKeepAliveHandler(ws=self, interval=min(interval, 5.0))
self._keep_alive.start() self._keep_alive.start()
await self._hook(self, msg)
async def initial_connection(self, data): async def initial_connection(self, data):
state = self._connection state = self._connection

Loading…
Cancel
Save