diff --git a/wavelink/__init__.py b/wavelink/__init__.py index 814a93b1..f1bdff5f 100644 --- a/wavelink/__init__.py +++ b/wavelink/__init__.py @@ -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 * diff --git a/wavelink/events.py b/wavelink/events.py index c7ba371d..85c7c0fa 100644 --- a/wavelink/events.py +++ b/wavelink/events.py @@ -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') @@ -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 diff --git a/wavelink/node.py b/wavelink/node.py index ec511b41..7bf4659d 100644 --- a/wavelink/node.py +++ b/wavelink/node.py @@ -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 @@ -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 diff --git a/wavelink/websocket.py b/wavelink/websocket.py index 3ac2e1a7..5048440e 100644 --- a/wavelink/websocket.py +++ b/wavelink/websocket.py @@ -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: