Skip to content

Commit

Permalink
Add new event WebsocketClosed which fixes a bug where disconnecting…
Browse files Browse the repository at this point in the history
… from a voice channel would raise an error.

Closes #37
  • Loading branch information
EvieePy committed Feb 5, 2020
1 parent 94c5728 commit 36de6a8
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 5 deletions.
2 changes: 1 addition & 1 deletion wavelink/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
__author__ = 'EvieePy'
__license__ = 'MIT'
__copyright__ = 'Copyright 2019-2020 (c) PythonistaGuild'
__version__ = '0.4.01'
__version__ = '0.4.02'

from .client import Client
from .errors import *
Expand Down
18 changes: 16 additions & 2 deletions wavelink/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@
from .player import Track


__all__ = ('WavelinkEvent', 'TrackEnd', 'TrackException', 'TrackStuck', 'TrackStart')
__all__ = ('WavelinkEvent', 'TrackEnd', 'TrackException', 'TrackStuck', 'TrackStart', 'WebsocketClosed')


class WavelinkEvent:
"""Base Wavelink event class. All events derive from this class."""
"""Base Wavelink event class. Most events derive from this class."""

__slots__ = ('player', 'track')

Expand Down Expand Up @@ -108,3 +108,17 @@ def __init__(self, player: Player, track: Track):

def __str__(self):
return 'TrackStart'


class WebsocketClosed:
"""Event dispatched when a player disconnects from a Guild.
:ivar reason: The reason the event was dispatched.
:ivar code: The websocket reason code.
:ivar guild_id: The guild ID associated with the disconnect.
"""

def __init__(self, reason: str, code: int, guild_id: int):
self.reason = reason
self.code = code
self.guild_id = guild_id
4 changes: 3 additions & 1 deletion wavelink/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
from urllib.parse import quote

from .errors import *
from .events import WebsocketClosed
from .player import Player, Track, TrackPlaylist
from .websocket import WebSocket

Expand Down Expand Up @@ -149,7 +150,8 @@ def get_player(self, guild_id: int) -> Optional[Player]:
async def on_event(self, event):
"""Function which dispatches events when triggered on the Node."""
__log__.info(f'NODE | Event dispatched:: <{str(event)}> ({self.__repr__()})')
await event.player.hook(event)
if not isinstance(event, WebsocketClosed):
await event.player.hook(event)

if not self.hook:
return
Expand Down
4 changes: 3 additions & 1 deletion wavelink/websocket.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,13 +152,15 @@ async def process_data(self, data: str):
except KeyError:
pass

def _get_event(self, name: str, data) -> Union[TrackEnd, TrackException, TrackStuck]:
def _get_event(self, name: str, data) -> Union[TrackEnd, TrackException, TrackStuck, WebsocketClosed]:
if name == 'TrackEndEvent':
return TrackEnd(data['player'], data.get('track', None), data.get('reason', None))
elif name == 'TrackExceptionEvent':
return TrackException(data['player'], data['track'], data['error'])
elif name == 'TrackStuckEvent':
return TrackStuck(data['player'], data['track'], int(data['threshold']))
elif name == 'WebSocketClosedEvent':
return WebsocketClosed(data['reason'], data['code'], data['guildId'])

async def _send(self, **data):
if self.is_connected:
Expand Down

0 comments on commit 36de6a8

Please sign in to comment.