From 305894f4e05263fcc62f754ca0959fae6391200d Mon Sep 17 00:00:00 2001 From: Choco <94597336+ChocoMeow@users.noreply.github.com> Date: Wed, 8 Mar 2023 10:38:28 +0800 Subject: [PATCH] Add playtop, swap and move commands and related back-end --- cogs/basic.py | 68 ++++++++++++++++++++++++++++++++++++++++++---- langs/CH.json | 3 +- langs/DE.json | 3 +- langs/EN.json | 3 +- langs/JP.json | 3 +- langs/KO.json | 3 +- voicelink/queue.py | 13 +++++++++ 7 files changed, 85 insertions(+), 11 deletions(-) diff --git a/cogs/basic.py b/cogs/basic.py index b97abd6..eeaef08 100644 --- a/cogs/basic.py +++ b/cogs/basic.py @@ -222,6 +222,40 @@ class Basic(commands.Cog): if not player.is_playing: await player.do_next() + @commands.hybrid_command(name="playtop", aliases=get_aliases("playtop")) + @app_commands.describe( + query="Input the name of the song.", + ) + @commands.dynamic_cooldown(cooldown_check, commands.BucketType.guild) + async def playtop(self, ctx: commands.Context, query: str): + "Adds a song with the given url or query on the top of the queue." + player: voicelink.Player = ctx.guild.voice_client + if not player: + player = await connect_channel(ctx) + + if ctx.author not in player.channel.members: + if not ctx.author.guild_permissions.manage_guild: + return await ctx.send(player.get_msg('notInChannel').format(ctx.author.mention, player.channel.mention), ephemeral=True) + + tracks = await player.get_tracks(query, requester=ctx.author) + if not tracks: + return await ctx.send(player.get_msg('noTrackFound')) + + try: + if isinstance(tracks, voicelink.Playlist): + index = await player.add_track(tracks.tracks, at_font=True) + await ctx.send(player.get_msg('playlistLoad').format(tracks.name, index)) + else: + position = await player.add_track(tracks[0], at_font=True) + await ctx.send((f"`{player.get_msg('live')}`" if tracks[0].is_stream else "") + (player.get_msg('trackLoad_pos').format(tracks[0].title, tracks[0].author, tracks[0].formatLength, position) if position >= 1 and player.is_playing else player.get_msg('trackLoad').format(tracks[0].title, tracks[0].author, tracks[0].formatLength))) + + except voicelink.QueueFull as e: + await ctx.send(e) + + finally: + if not player.is_playing: + await player.do_next() + @commands.hybrid_command(name="pause", aliases=get_aliases("pause")) @commands.dynamic_cooldown(cooldown_check, commands.BucketType.guild) async def pause(self, ctx: commands.Context): @@ -640,13 +674,35 @@ class Basic(commands.Cog): player.shuffle_votes.clear() await ctx.send(player.get_msg('shuffled')) - @commands.hybrid_command(name="move", aliases=get_aliases("move")) + @commands.hybrid_command(name="swap", aliases=get_aliases("swap")) @app_commands.describe( - position1="The track to move. Example: 2", - position2="The new position to move the track to. Exmaple: 1" + position1="The track to swap. Example: 2", + position2="The track to swap with position1. Exmaple: 1" ) @commands.dynamic_cooldown(cooldown_check, commands.BucketType.guild) - async def move(self, ctx: commands.Context, position1: int, position2: int): + async def swap(self, ctx: commands.Context, position1: int, position2: int): + "Swaps the specified song to the specified song." + player: voicelink.Player = ctx.guild.voice_client + if not player: + return await ctx.send(get_lang(ctx.guild.id, 'noPlayer'), ephemeral=True) + + if ctx.author not in player.channel.members: + if not ctx.author.guild_permissions.manage_guild: + return await ctx.send(player.get_msg('notInChannel').format(ctx.author.mention, player.channel.mention), ephemeral=True) + + if not await player.is_privileged(ctx.author): + return await ctx.send(player.get_msg('missingPerms_pos'), ephemeral=True) + + track1, track2 = player.queue.swap(position1, position2) + await ctx.send(player.get_msg('swapped').format(track1.title, track2.title)) + + @commands.hybrid_command(name="move", aliases=get_aliases("move")) + @app_commands.describe( + target="The track to move. Example: 2", + to="The new position to move the track to. Exmaple: 1" + ) + @commands.dynamic_cooldown(cooldown_check, commands.BucketType.guild) + async def move(self, ctx: commands.Context, target: int, to: int): "Moves the specified song to the specified position." player: voicelink.Player = ctx.guild.voice_client if not player: @@ -659,8 +715,8 @@ class Basic(commands.Cog): if not await player.is_privileged(ctx.author): return await ctx.send(player.get_msg('missingPerms_pos'), ephemeral=True) - player.queue.swap(position1, position2) - await ctx.send(player.get_msg('moved').format(position1, position2)) + moved_track = player.queue.move(target, to) + await ctx.send(player.get_msg('moved').format(moved_track.title, to)) @commands.hybrid_command(name="lyrics", aliases=get_aliases("lyrics")) @app_commands.describe(name="Searches for your query and displays the reutned lyrics.") diff --git a/langs/CH.json b/langs/CH.json index c48302a..aed6bcc 100644 --- a/langs/CH.json +++ b/langs/CH.json @@ -171,7 +171,8 @@ "forward": "將播放器轉發到 **{0}**", "rewind": "將播放器倒回至 **{0}**", "replay": "重播當前歌曲。", - "moved": "{0} 和 {1} 被交換", + "swapped": "`{0}` 和 `{1}` 被交換", + "moved": "將 `{0}` 移動到 `{1}`", "autoplay": "現在是自動播放模式 **{0}**", "notdj": "你不是 DJ。 當前 DJ 是 {0}。", diff --git a/langs/DE.json b/langs/DE.json index dabf09f..632fa67 100644 --- a/langs/DE.json +++ b/langs/DE.json @@ -171,7 +171,8 @@ "forward": "Leite den Spieler an **{0}** weiter", "rewind": "Spulen Sie den Player auf **{0}** zurück", "replay": "Wiederholen des aktuellen Songs.", - "moved": "{0} und {1} sind vertauscht", + "swapped": "`{0}` und `{1}` sind vertauscht", + "moved": "`{0}` nach `{1}` verschoben", "autoplay": "Der Autoplay-Modus ist jetzt **{0}**", "notdj": "Du bist kein DJ. Der aktuelle DJ ist {0}.", diff --git a/langs/EN.json b/langs/EN.json index 4520b59..c37bb5c 100644 --- a/langs/EN.json +++ b/langs/EN.json @@ -171,7 +171,8 @@ "forward": "Forward the player to **{0}**", "rewind": "Rewind the player to **{0}**", "replay": "Replaying the current song.", - "moved": "{0} and {1} are Swapped", + "swapped": "`{0}` and `{1}` are swapped", + "moved": "Moved `{0}` to `{1}`", "autoplay": "Autoplay mode is now **{0}**", "notdj": "You are not DJ. The current DJ is {0}.", diff --git a/langs/JP.json b/langs/JP.json index ba094b4..64be2b5 100644 --- a/langs/JP.json +++ b/langs/JP.json @@ -171,7 +171,8 @@ "forward": "プレーヤーを **{0}** に転送します", "rewind": "プレーヤーを **{0}** に巻き戻します", "replay": "現在の曲を再生します。", - "moved": "{0}と{1}は入れ替えられます", + "swapped": "`{0}` と `{1}`は入れ替えられます", + "moved": "`{0}` を `{1}` に移動しました", "autoplay": "自動再生モードが **{0}** になりました", "notdj": "あなたはDJではありません。 現在のDJは{0}です。", diff --git a/langs/KO.json b/langs/KO.json index 0699637..68271fa 100644 --- a/langs/KO.json +++ b/langs/KO.json @@ -171,7 +171,8 @@ "forward": "플레이어를 **{0}**로 전달", "rewind": "플레이어를 **{0}**으로 되감기", "replay": "현재 노래를 재생합니다.", - "moved": "{0} 및 {1}이(가) 바뀌었습니다.", + "swapped": "`{0}` 및 `{1}`이(가) 바뀌었습니다.", + "moved": "`{0}`을(를) `{1}`(으)로 이동했습니다.", "autoplay": "자동재생 모드는 이제 **{0}**입니다.", "notdj": "당신은 DJ가 아닙니다. 현재 DJ는 {0}입니다.", diff --git a/voicelink/queue.py b/voicelink/queue.py index 876a851..375474b 100644 --- a/voicelink/queue.py +++ b/voicelink/queue.py @@ -122,9 +122,22 @@ class Queue: try: pos = self._position - 1 self._queue[pos + num1], self._queue[pos + num2] = self._queue[pos + num2], self._queue[pos + num1] + return self._queue[pos + num1], self._queue[pos + num2] except IndexError: raise OutofList(self.get_msg("voicelinkOutofList")) + def move(self, target:int, to:int): + if not 0 < target <= self.count or not 0 < to: + raise OutofList(self.get_msg("voicelinkOutofList")) + + try: + moveItem = self._queue[self._position + target - 1] + self._queue.remove(moveItem) + self.put_at_index(to, moveItem) + return moveItem + except: + raise OutofList(self.get_msg("voicelinkOutofList")) + def remove(self, index:int, index2:int, member: Member = None): pos = self._position - 1 if not index2: