Skip to content

Commit

Permalink
Fixes, add QueueEndEvent
Browse files Browse the repository at this point in the history
  • Loading branch information
devoxin committed Feb 10, 2018
1 parent a3ddc42 commit 0a466fc
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 19 deletions.
7 changes: 7 additions & 0 deletions examples/music.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ async def track_hook(self, player, event):
c = self.bot.get_channel(c)
if c:
await c.send('Now playing: ' + player.current.title)
elif event == 'QueueEndEvent':
c = player.fetch('channel')
if c:
c = self.bot.get_channel(c)
if c:
await c.send('Queue ended! Why not queue more songs?')

@commands.command(aliases=['p'])
async def play(self, ctx, *, query):
Expand Down Expand Up @@ -53,6 +59,7 @@ async def play(self, ctx, *, query):
embed = discord.Embed(colour=ctx.guild.me.top_role.colour,
title="Playlist Enqueued!",
description=f"Imported {len(tracks)} tracks from the playlist :)")
await ctx.send(embed=embed)
else:
embed = discord.Embed(colour=ctx.guild.me.top_role.colour,
title="Track Enqueued",
Expand Down
14 changes: 5 additions & 9 deletions lavalink.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,18 +62,14 @@ async def _trigger_event(self, event: str, guild_id: str, reason: str=''):
player = self.bot.lavalink.players[int(guild_id)]

if player:
if event == 'TrackStartEvent':
for hook in self.hooks:
await hook(player, event)
for hook in self.hooks:
await hook(player, event)

elif event in ['TrackEndEvent', 'TrackExceptionEvent', 'TrackStuckEvent']:
if event in ['TrackEndEvent', 'TrackExceptionEvent', 'TrackStuckEvent']:
for hook in self.hooks:
await hook(player, event)
await player.on_track_end(reason)

else:
self.log('warn', 'Received unknown event type ' + event)

async def _update_state(self, data):
g = int(data['guildId'])

Expand Down Expand Up @@ -187,8 +183,7 @@ async def listen(self):
return self.log('debug', 'Received websocket message without op\n' + str(data))

if op == 'event':
print(data)
await self._lavalink._trigger_event(data['type'], data['guildId'], data.get('reason', ''))
await self._lavalink._trigger_event(data['type'], data['guildId'], data.get('reason', 'FINISHED'))
elif op == 'playerUpdate':
await self._lavalink._update_state(data)
except websockets.ConnectionClosed:
Expand Down Expand Up @@ -380,6 +375,7 @@ async def play(self):

if not self.queue:
await self.stop()
await self.bot.lavalink.client._trigger_event('QueueEndEvent', self.guild_id)
else:
if self.shuffle:
track = self.queue.pop(randrange(len(self.queue)))
Expand Down
12 changes: 4 additions & 8 deletions models/Client.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,18 +60,14 @@ async def _trigger_event(self, event: str, guild_id: str, reason: str=''):
player = self.bot.lavalink.players[int(guild_id)]

if player:
if event == 'TrackStartEvent':
for hook in self.hooks:
await hook(player, event)
for hook in self.hooks:
await hook(player, event)

elif event in ['TrackEndEvent', 'TrackExceptionEvent', 'TrackStuckEvent']:
if event in ['TrackEndEvent', 'TrackExceptionEvent', 'TrackStuckEvent']:
for hook in self.hooks:
await hook(player, event)
await player.on_track_end(reason)

else:
self.log('warn', 'Received unknown event type ' + event)

async def _update_state(self, data):
g = int(data['guildId'])

Expand Down Expand Up @@ -122,4 +118,4 @@ def _destroy(self):
def log(self, level, content):
lvl = resolve_log_level(level)
if lvl >= self.log_level:
print('[{}] [lavalink.py] [{}] {}'.format(datetime.utcnow().strftime('%H:%M:%S'), level, content))
print('[{}] [lavalink.py] [{}] {}'.format(datetime.utcnow().strftime('%H:%M:%S'), level, content))
1 change: 1 addition & 0 deletions models/Player.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ async def play(self):

if not self.queue:
await self.stop()
await self.bot.lavalink.client._trigger_event('QueueEndEvent', self.guild_id)
else:
if self.shuffle:
track = self.queue.pop(randrange(len(self.queue)))
Expand Down
3 changes: 1 addition & 2 deletions models/WebSocket.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,7 @@ async def listen(self):
return self.log('debug', 'Received websocket message without op\n' + str(data))

if op == 'event':
print(data)
await self._lavalink._trigger_event(data['type'], data['guildId'], data.get('reason', ''))
await self._lavalink._trigger_event(data['type'], data['guildId'], data.get('reason', 'FINISHED'))
elif op == 'playerUpdate':
await self._lavalink._update_state(data)
except websockets.ConnectionClosed:
Expand Down

0 comments on commit 0a466fc

Please sign in to comment.