From 870b61536460f35a23a388cf3d1c819a31899fcc Mon Sep 17 00:00:00 2001 From: aikaterna <20862007+aikaterna@users.noreply.github.com> Date: Sun, 23 Jun 2019 22:09:04 -0700 Subject: [PATCH] [Audio] Queue & misc cleanup (#2784) * [Audio] Queue & misc cleanup --- redbot/cogs/audio/audio.py | 75 +++++++++++++++++++++++--------------- 1 file changed, 46 insertions(+), 29 deletions(-) diff --git a/redbot/cogs/audio/audio.py b/redbot/cogs/audio/audio.py index ae96b0f6b..b866793a3 100644 --- a/redbot/cogs/audio/audio.py +++ b/redbot/cogs/audio/audio.py @@ -126,7 +126,7 @@ class Audio(commands.Cog): self._connect_task = self.bot.loop.create_task(self.attempt_connect()) - async def attempt_connect(self, timeout: int = 30): + async def attempt_connect(self, timeout: int = 50): self._connection_aborted = False max_retries = 5 retry_count = 0 @@ -1335,7 +1335,7 @@ class Audio(commands.Cog): (r, u) = await self.bot.wait_for( "reaction_add", check=ReactionPredicate.with_emojis(expected, message, ctx.author), - timeout=10.0, + timeout=30.0, ) except asyncio.TimeoutError: return await self._clear_react(message, emoji) @@ -1468,7 +1468,6 @@ class Audio(commands.Cog): @commands.bot_has_permissions(embed_links=True) async def play(self, ctx, *, query): """Play a URL or search for a track.""" - guild_data = await self.config.guild(ctx.guild).all() restrict = await self.config.restrict() if restrict: @@ -2554,23 +2553,30 @@ class Audio(commands.Cog): @commands.group(invoke_without_command=True) @commands.guild_only() @commands.bot_has_permissions(embed_links=True, add_reactions=True) - async def queue(self, ctx, *, page="1"): - """List the queue. + async def queue(self, ctx, *, page: int = 1): + """List the songs in the queue.""" + + async def _queue_menu( + ctx: commands.Context, + pages: list, + controls: dict, + message: discord.Message, + page: int, + timeout: float, + emoji: str, + ): + if message: + await ctx.send_help(self.queue) + await message.delete() + return None + + QUEUE_CONTROLS = {"⬅": prev_page, "❌": close_menu, "➡": next_page, "ℹ": _queue_menu} - Use [p]queue search to search the queue. - """ if not self._player_check(ctx): return await self._embed_msg(ctx, _("There's nothing in the queue.")) player = lavalink.get_player(ctx.guild.id) if not player.queue: return await self._embed_msg(ctx, _("There's nothing in the queue.")) - if not page.isdigit(): - if page.startswith("search "): - return await self._queue_search(ctx=ctx, search_words=page.replace("search ", "")) - else: - return - else: - page = int(page) len_queue_pages = math.ceil(len(player.queue) / 10) queue_page_list = [] for page_num in range(1, len_queue_pages + 1): @@ -2578,7 +2584,7 @@ class Audio(commands.Cog): queue_page_list.append(embed) if page > len_queue_pages: page = len_queue_pages - await menu(ctx, queue_page_list, DEFAULT_CONTROLS, page=(page - 1)) + await menu(ctx, queue_page_list, QUEUE_CONTROLS, page=(page - 1)) async def _build_queue_page(self, ctx, player, page_num): shuffle = await self.config.guild(ctx.guild).shuffle() @@ -2676,18 +2682,6 @@ class Audio(commands.Cog): embed.set_footer(text=text) return embed - async def _queue_search(self, ctx, *, search_words): - player = lavalink.get_player(ctx.guild.id) - search_list = await self._build_queue_search_list(player.queue, search_words) - if not search_list: - return await self._embed_msg(ctx, _("No matches.")) - len_search_pages = math.ceil(len(search_list) / 10) - search_page_list = [] - for page_num in range(1, len_search_pages + 1): - embed = await self._build_queue_search_page(ctx, page_num, search_list) - search_page_list.append(embed) - await menu(ctx, search_page_list, DEFAULT_CONTROLS) - async def _build_queue_search_list(self, queue_list, search_words): track_list = [] queue_idx = 0 @@ -2720,8 +2714,9 @@ class Audio(commands.Cog): for i, track in enumerate( search_list[search_idx_start:search_idx_end], start=search_idx_start ): + track_idx = i + 1 - if command == "search": + if type(track) is str: local_path = await self.config.localpath() track_location = track.replace("localtrack:{}/localtracks/".format(local_path), "") track_match += "`{}.` **{}**\n".format(track_idx, track_location) @@ -2791,6 +2786,28 @@ class Audio(commands.Cog): ).format(removed_tracks=removed_tracks), ) + @queue.command(name="search") + @commands.guild_only() + async def _queue_search(self, ctx, *, search_words: str): + """Search the queue.""" + try: + player = lavalink.get_player(ctx.guild.id) + except KeyError: + return await self._embed_msg(ctx, _("There's nothing in the queue.")) + if not self._player_check(ctx) or not player.queue: + return await self._embed_msg(ctx, _("There's nothing in the queue.")) + + search_list = await self._build_queue_search_list(player.queue, search_words) + if not search_list: + return await self._embed_msg(ctx, _("No matches.")) + + len_search_pages = math.ceil(len(search_list) / 10) + search_page_list = [] + for page_num in range(1, len_search_pages + 1): + embed = await self._build_queue_search_page(ctx, page_num, search_list) + search_page_list.append(embed) + await menu(ctx, search_page_list, DEFAULT_CONTROLS) + @commands.command() @commands.guild_only() @commands.bot_has_permissions(embed_links=True) @@ -3248,7 +3265,7 @@ class Audio(commands.Cog): @commands.guild_only() @commands.bot_has_permissions(embed_links=True) async def sing(self, ctx): - """Make Red sing one of her songs""" + """Make Red sing one of her songs.""" ids = ( "zGTkAVsrfg8", "cGMWL8cOeAU",