From 6a715d87dd2989a6fe7f88722d23aff81c8fc252 Mon Sep 17 00:00:00 2001 From: aikaterna <20862007+aikaterna@users.noreply.github.com> Date: Thu, 8 Mar 2018 17:50:54 -0800 Subject: [PATCH] [V3 Audio] Add prev command, bugfixes for audiostats, search (#1405) * Update audiostats to display days in timer * Add prev command * Update search button to play enqueued song --- redbot/cogs/audio/audio.py | 65 ++++++++++++++++++++++++++++---------- 1 file changed, 49 insertions(+), 16 deletions(-) diff --git a/redbot/cogs/audio/audio.py b/redbot/cogs/audio/audio.py index db0ce2772..3e3ec263a 100644 --- a/redbot/cogs/audio/audio.py +++ b/redbot/cogs/audio/audio.py @@ -72,6 +72,14 @@ class Audio: except IndexError: pass + if event == 'TrackStartEvent': + playing_song = player.fetch('playing_song') + requester = player.fetch('requester') + player.store('prev_song', playing_song) + player.store('prev_requester', requester) + player.store('playing_song', self.bot.lavalink.players.get(player.fetch('guild')).current.uri) + player.store('requester', self.bot.get_user(self.bot.lavalink.players.get(player.fetch('guild')).current.requester)) + if event == 'TrackStartEvent' and notify: c = player.fetch('channel') if c: @@ -166,7 +174,7 @@ class Audio: player = v connect_start = v.fetch('connect') try: - connect_dur = self._dynamic_time((datetime.datetime.utcnow() - connect_start).seconds) + connect_dur = self._dynamic_time(int((datetime.datetime.utcnow() - connect_start).total_seconds())) except TypeError: connect_dur = 0 try: @@ -204,7 +212,10 @@ class Audio: player = self.bot.lavalink.players.get(ctx.guild.id) await player.disconnect() player.queue.clear() - player.store('song', None) + player.store('connect', None) + player.store('prev_requester', None) + player.store('prev_song', None) + player.store('playing_song', None) player.store('requester', None) await self.bot.lavalink.client._trigger_event("QueueEndEvent", ctx.guild.id) @@ -372,6 +383,34 @@ class Audio: if not player.is_playing: await player.play() + @commands.command() + async def prev(self, ctx): + """Skips to the start of the previously played track.""" + player = self.bot.lavalink.players.get(ctx.guild.id) + shuffle = await self.config.guild(ctx.guild).shuffle() + + if not ctx.author.voice or (player.is_connected and ctx.author.voice.channel.id != int(player.channel_id)): + return await self._embed_msg(ctx, 'You must be in the voice channel to skip the music.') + + if shuffle: + return await self._embed_msg(ctx, 'Turn shuffle off to use this command.') + + if player.current is None: + return await self._embed_msg(ctx, 'The player is stopped.') + + if player.fetch('prev_song') is None: + return await self._embed_msg(ctx, 'No previous track.') + else: + last_track = await self.bot.lavalink.client.get_tracks(player.fetch('prev_song')) + player.add(player.fetch('prev_requester').id, last_track[0]) + queue_len = len(self.bot.lavalink.players.get(ctx.guild.id).queue) + bump_song = self.bot.lavalink.players.get(ctx.guild.id).queue[queue_len - 1] + player.queue.insert(0, bump_song) + player.queue.pop(queue_len) + await player.skip() + embed = discord.Embed(colour=ctx.guild.me.top_role.colour, title='Replaying Track', description='**[{}]({})**'.format(player.current.title, player.current.uri)) + await ctx.send(embed=embed) + @commands.command(aliases=['q']) async def queue(self, ctx, page: int = 1): """Lists the queue.""" @@ -565,6 +604,8 @@ class Audio: queue_total_duration = lavalink.Utils.format_time(queue_duration) if not shuffle: embed.set_footer(text='{} until track playback'.format(queue_total_duration)) + if not player.is_playing: + await player.play() return await ctx.send(embed=embed) @commands.command() @@ -642,7 +683,9 @@ class Audio: await self._embed_msg(ctx, 'Stopping...') player.queue.clear() await player.stop() - player.store('song', None) + player.store('prev_requester', None) + player.store('prev_song', None) + player.store('playing_song', None) player.store('requester', None) await self.bot.lavalink.client._trigger_event("QueueEndEvent", ctx.guild.id) @@ -743,21 +786,11 @@ class Audio: d, h = divmod(h, 24) if d > 0: - msg = "{0}d" - if h > 0 and m > 0 and s > 0: - msg += " {1}h" - elif m > 0 and d == 0 and h == 0 and s == 0: - pass - elif s > 0 and d == 0 and h == 0 and m == 0: - pass - elif d == 0 and m == 0 and h == 0 and s == 0: - pass - else: - msg += " {1}h" + msg = "{0}d {1}h" elif d == 0 and h > 0: - msg = "{1}h 0m" if m == 0 else "{1}h {2}m" + msg = "{1}h {2}m" elif d == 0 and h == 0 and m > 0: - msg = "{2}m 0s" if s == 0 else "{2}m {3}s" + msg = "{2}m {3}s" elif d == 0 and h == 0 and m == 0 and s > 0: msg = "{3}s" return msg.format(d, h, m, s)