From 749d49389cb9a99b07339c024fe8db92ca5b3d4d Mon Sep 17 00:00:00 2001 From: Mantou Date: Fri, 25 Aug 2023 11:50:42 +0800 Subject: [PATCH] Fix(commands.py and modal.py): playlist suffer overwrite --- cogs/commands.py | 26 ++++++++++++++------------ core/modal.py | 13 ++++++------- 2 files changed, 20 insertions(+), 19 deletions(-) diff --git a/cogs/commands.py b/cogs/commands.py index 398a9c1..7169d2a 100644 --- a/cogs/commands.py +++ b/cogs/commands.py @@ -853,13 +853,22 @@ async def join(self, ctx: ApplicationContext, playlist:Option( await ctx.send_modal(modal) else: - await ctx.interaction.response.send_message(embed=LoadingEmbed(title="正在讀取中...")) + await ctx.defer() + + await ctx.interaction.edit_original_response(embed=LoadingEmbed(title="正在讀取中...")) + + with open(f"./playlist/{ctx.user.id}.json", "r", encoding="utf-8") as f: + data = json.load(f) + + for key in data.keys(): + if uuid.uuid5(uuid.NAMESPACE_DNS, key).hex == playlist: + name = key + break - tracks = [] result: LoadResult = await self.bot.lavalink.get_tracks(query) for track in result.tracks: - tracks.append({ + data[name]['tracks'].append({ 'track': track.track, 'identifier': track.identifier, 'isSeekable': track.is_seekable, @@ -870,15 +879,8 @@ async def join(self, ctx: ApplicationContext, playlist:Option( 'uri': f"https://www.youtube.com/watch?v={track.identifier}" }) - with open(f"./playlist/{ctx.user.id}.json", "r", encoding="utf-8") as f: - data = json.load(f) - for key in data.keys(): - if uuid.uuid5(uuid.NAMESPACE_DNS, key).hex == playlist: - name = key - break - - data[name].update({"loadType": "PLAYLIST_LOADED", "playlistInfo": {"name": playlist, "selectedTrack": -1}, "tracks": tracks}) + data[name].update({"loadType": "PLAYLIST_LOADED", "playlistInfo": {"name": playlist, "selectedTrack": -1}, "tracks": data[name]['tracks']}) with open(f"./playlist/{ctx.user.id}.json", "w", encoding="utf-8") as f: json.dump(data, f, indent=4) @@ -936,7 +938,7 @@ async def delete(self, ctx: ApplicationContext, playlist:Option( )): await ctx.defer() - await ctx.interaction.response.send_message(embed=LoadingEmbed(title="正在讀取中...")) + await ctx.interaction.edit_original_response(embed=LoadingEmbed(title="正在讀取中...")) name = "" with open(f"./playlist/{ctx.interaction.user.id}.json", "r" ,encoding="utf-8") as f: diff --git a/core/modal.py b/core/modal.py index 979113d..4a689c8 100644 --- a/core/modal.py +++ b/core/modal.py @@ -21,11 +21,13 @@ def __init__(self, name:str, bot:Bot, *args, **kwargs) -> None: self.add_item(InputText(label="歌曲連結 (格式為第一首後按下Enter再貼連結,最多25個)", placeholder="請將連結貼至輸入框中", style=InputTextStyle.paragraph)) async def callback(self, interaction: Interaction) -> Optional[LoadResult]: - if not len(self.children[0].value.split("\n")) > 25: + with open(f"./playlist/{interaction.user.id}.json", "r", encoding="utf-8") as f: + data = json.load(f) + if not len(self.children[0].value.split("\n")) > 25 and not (len(data[self.name]['tracks']) + len(self.children[0].value.split("\n"))) > 25: for query in self.children[0].value.split("\n"): result = await self.bot.lavalink.get_tracks(query) for track in result.tracks: - self.tracks.append({ + data[self.name]['tracks'].append({ 'track': track.track, 'identifier': track.identifier, 'isSeekable': track.is_seekable, @@ -37,20 +39,17 @@ async def callback(self, interaction: Interaction) -> Optional[LoadResult]: }) await interaction.response.send_message(embed=LoadingEmbed(title="正在讀取中...")) - - with open(f"./playlist/{interaction.user.id}.json", "r", encoding="utf-8") as f: - data = json.load(f) for name in data.keys(): if uuid.uuid5(uuid.NAMESPACE_DNS, name).hex == self.name: self.name = name break - data[self.name].update({"loadType": "PLAYLIST_LOADED", "playlistInfo": {"name": self.name, "selectedTrack": -1}, "tracks": self.tracks}) + data[self.name].update({"loadType": "PLAYLIST_LOADED", "playlistInfo": {"name": self.name, "selectedTrack": -1}, "tracks": data[self.name]['tracks']}) with open(f"./playlist/{interaction.user.id}.json", "w", encoding="utf-8") as f: json.dump(data, f, indent=4) await interaction.edit_original_response(embed=SuccessEmbed(title="添加成功!")) else: - await interaction.edit_original_response(embed=ErrorEmbed(title="你給的連結太多了! (最多25個)")) \ No newline at end of file + await interaction.edit_original_response(embed=ErrorEmbed(title="你給的連結太多了或是歌單超出限制了! (最多25個)",description=f"目前歌單中的歌曲數量: {len(data[self.name]['tracks'])}")) \ No newline at end of file