[Audio] Add ability to skip to a specified track by number (#2584)

* add skip to track functionality

* formatting

* Change some strings

* missing return, variable naming

* add variable names for translations

* change handling of skipping 1 track

* minor semantic adjustments

skiptotrack -> skip_to_track
This commit is contained in:
NIXC 2019-05-07 23:34:38 -06:00 committed by Will
parent 65b88c09fb
commit 96a91b9d0e

View File

@ -2899,8 +2899,8 @@ class Audio(commands.Cog):
@commands.command() @commands.command()
@commands.guild_only() @commands.guild_only()
async def skip(self, ctx): async def skip(self, ctx, skip_to_track: int = None):
"""Skip to the next track.""" """Skip to the next track, or to a given track number."""
if not self._player_check(ctx): if not self._player_check(ctx):
return await self._embed_msg(ctx, _("Nothing playing.")) return await self._embed_msg(ctx, _("Nothing playing."))
player = lavalink.get_player(ctx.guild.id) player = lavalink.get_player(ctx.guild.id)
@ -2917,6 +2917,10 @@ class Audio(commands.Cog):
return await self._embed_msg(ctx, _("You need the DJ role to skip tracks.")) return await self._embed_msg(ctx, _("You need the DJ role to skip tracks."))
if vote_enabled: if vote_enabled:
if not await self._can_instaskip(ctx, ctx.author): if not await self._can_instaskip(ctx, ctx.author):
if skip_to_track is not None:
return await self._embed_msg(
ctx, _("Can't skip to a specific track in vote mode without the DJ role.")
)
if ctx.author.id in self.skip_votes[ctx.message.guild]: if ctx.author.id in self.skip_votes[ctx.message.guild]:
self.skip_votes[ctx.message.guild].remove(ctx.author.id) self.skip_votes[ctx.message.guild].remove(ctx.author.id)
reply = _("I removed your vote to skip.") reply = _("I removed your vote to skip.")
@ -2949,9 +2953,9 @@ class Audio(commands.Cog):
) )
return await self._embed_msg(ctx, reply) return await self._embed_msg(ctx, reply)
else: else:
return await self._skip_action(ctx) return await self._skip_action(ctx, skip_to_track)
else: else:
return await self._skip_action(ctx) return await self._skip_action(ctx, skip_to_track)
async def _can_instaskip(self, ctx, member): async def _can_instaskip(self, ctx, member):
mod_role = await ctx.bot.db.guild(ctx.guild).mod_role() mod_role = await ctx.bot.db.guild(ctx.guild).mod_role()
@ -3010,7 +3014,7 @@ class Audio(commands.Cog):
else: else:
return False return False
async def _skip_action(self, ctx): async def _skip_action(self, ctx, skip_to_track: int = None):
player = lavalink.get_player(ctx.guild.id) player = lavalink.get_player(ctx.guild.id)
if not player.queue: if not player.queue:
try: try:
@ -3035,23 +3039,57 @@ class Audio(commands.Cog):
) )
) )
return await ctx.send(embed=embed) return await ctx.send(embed=embed)
queue_to_append = []
if "localtracks" in player.current.uri: if skip_to_track is not None and skip_to_track != 1:
if not player.current.title == "Unknown title": if skip_to_track < 1:
description = "**{} - {}**\n{}".format( return await self._embed_msg(
player.current.author, ctx, _("Track number must be equal to or greater than 1.")
player.current.title,
player.current.uri.replace("localtracks/", ""),
) )
else: elif skip_to_track > len(player.queue):
description = "{}".format(player.current.uri.replace("localtracks/", "")) return await self._embed_msg(
else: ctx,
description = "**[{}]({})**".format(player.current.title, player.current.uri) _(
"There are only {queuelen} songs currently queued.".format(
queuelen=len(player.queue)
)
),
)
elif player.shuffle:
return await self._embed_msg(
ctx, _("Can't skip to a track while shuffle is enabled.")
)
nexttrack = player.queue[min(skip_to_track - 1, len(player.queue) - 1)]
embed = discord.Embed( embed = discord.Embed(
colour=await ctx.embed_colour(), title=_("Track Skipped"), description=description colour=await ctx.embed_colour(),
title=_("{skip_to_track} Tracks Skipped".format(skip_to_track=skip_to_track)),
) )
await ctx.send(embed=embed) await ctx.send(embed=embed)
await player.skip() if player.repeat:
queue_to_append = player.queue[0 : min(skip_to_track - 1, len(player.queue) - 1)]
player.queue = player.queue[
min(skip_to_track - 1, len(player.queue) - 1) : len(player.queue)
]
else:
embed = discord.Embed(
colour=await ctx.embed_colour(),
title=_("Track Skipped"),
description=await self._get_description(player.current),
)
await ctx.send(embed=embed)
await player.play()
player.queue += queue_to_append
async def _get_description(self, track):
if "localtracks" in track.uri:
if not track.title == "Unknown title":
return "**{} - {}**\n{}".format(
track.author, track.title, track.uri.replace("localtracks/", "")
)
else:
return "{}".format(track.uri.replace("localtracks/", ""))
else:
return "**[{}]({})**".format(track.title, track.uri)
@commands.command() @commands.command()
@commands.guild_only() @commands.guild_only()