[Audio] Queue & misc cleanup (#2784)

* [Audio] Queue & misc cleanup
This commit is contained in:
aikaterna 2019-06-23 22:09:04 -07:00 committed by Michael H
parent 25ccc11dc4
commit 870b615364

View File

@ -126,7 +126,7 @@ class Audio(commands.Cog):
self._connect_task = self.bot.loop.create_task(self.attempt_connect()) 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 self._connection_aborted = False
max_retries = 5 max_retries = 5
retry_count = 0 retry_count = 0
@ -1335,7 +1335,7 @@ class Audio(commands.Cog):
(r, u) = await self.bot.wait_for( (r, u) = await self.bot.wait_for(
"reaction_add", "reaction_add",
check=ReactionPredicate.with_emojis(expected, message, ctx.author), check=ReactionPredicate.with_emojis(expected, message, ctx.author),
timeout=10.0, timeout=30.0,
) )
except asyncio.TimeoutError: except asyncio.TimeoutError:
return await self._clear_react(message, emoji) return await self._clear_react(message, emoji)
@ -1468,7 +1468,6 @@ class Audio(commands.Cog):
@commands.bot_has_permissions(embed_links=True) @commands.bot_has_permissions(embed_links=True)
async def play(self, ctx, *, query): async def play(self, ctx, *, query):
"""Play a URL or search for a track.""" """Play a URL or search for a track."""
guild_data = await self.config.guild(ctx.guild).all() guild_data = await self.config.guild(ctx.guild).all()
restrict = await self.config.restrict() restrict = await self.config.restrict()
if restrict: if restrict:
@ -2554,23 +2553,30 @@ class Audio(commands.Cog):
@commands.group(invoke_without_command=True) @commands.group(invoke_without_command=True)
@commands.guild_only() @commands.guild_only()
@commands.bot_has_permissions(embed_links=True, add_reactions=True) @commands.bot_has_permissions(embed_links=True, add_reactions=True)
async def queue(self, ctx, *, page="1"): async def queue(self, ctx, *, page: int = 1):
"""List the queue. """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 <search terms> to search the queue.
"""
if not self._player_check(ctx): if not self._player_check(ctx):
return await self._embed_msg(ctx, _("There's nothing in the queue.")) return await self._embed_msg(ctx, _("There's nothing in the queue."))
player = lavalink.get_player(ctx.guild.id) player = lavalink.get_player(ctx.guild.id)
if not player.queue: if not player.queue:
return await self._embed_msg(ctx, _("There's nothing in the 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) len_queue_pages = math.ceil(len(player.queue) / 10)
queue_page_list = [] queue_page_list = []
for page_num in range(1, len_queue_pages + 1): for page_num in range(1, len_queue_pages + 1):
@ -2578,7 +2584,7 @@ class Audio(commands.Cog):
queue_page_list.append(embed) queue_page_list.append(embed)
if page > len_queue_pages: if page > len_queue_pages:
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): async def _build_queue_page(self, ctx, player, page_num):
shuffle = await self.config.guild(ctx.guild).shuffle() shuffle = await self.config.guild(ctx.guild).shuffle()
@ -2676,18 +2682,6 @@ class Audio(commands.Cog):
embed.set_footer(text=text) embed.set_footer(text=text)
return embed 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): async def _build_queue_search_list(self, queue_list, search_words):
track_list = [] track_list = []
queue_idx = 0 queue_idx = 0
@ -2720,8 +2714,9 @@ class Audio(commands.Cog):
for i, track in enumerate( for i, track in enumerate(
search_list[search_idx_start:search_idx_end], start=search_idx_start search_list[search_idx_start:search_idx_end], start=search_idx_start
): ):
track_idx = i + 1 track_idx = i + 1
if command == "search": if type(track) is str:
local_path = await self.config.localpath() local_path = await self.config.localpath()
track_location = track.replace("localtrack:{}/localtracks/".format(local_path), "") track_location = track.replace("localtrack:{}/localtracks/".format(local_path), "")
track_match += "`{}.` **{}**\n".format(track_idx, track_location) track_match += "`{}.` **{}**\n".format(track_idx, track_location)
@ -2791,6 +2786,28 @@ class Audio(commands.Cog):
).format(removed_tracks=removed_tracks), ).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.command()
@commands.guild_only() @commands.guild_only()
@commands.bot_has_permissions(embed_links=True) @commands.bot_has_permissions(embed_links=True)
@ -3248,7 +3265,7 @@ class Audio(commands.Cog):
@commands.guild_only() @commands.guild_only()
@commands.bot_has_permissions(embed_links=True) @commands.bot_has_permissions(embed_links=True)
async def sing(self, ctx): async def sing(self, ctx):
"""Make Red sing one of her songs""" """Make Red sing one of her songs."""
ids = ( ids = (
"zGTkAVsrfg8", "zGTkAVsrfg8",
"cGMWL8cOeAU", "cGMWL8cOeAU",