Skip to content

Commit

Permalink
Fix(commands.py and modal.py): playlist suffer overwrite
Browse files Browse the repository at this point in the history
  • Loading branch information
Mantouisyummy committed Aug 25, 2023
1 parent b9faf08 commit 749d493
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 19 deletions.
26 changes: 14 additions & 12 deletions cogs/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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)
Expand Down Expand Up @@ -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:
Expand Down
13 changes: 6 additions & 7 deletions core/modal.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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個)"))
await interaction.edit_original_response(embed=ErrorEmbed(title="你給的連結太多了或是歌單超出限制了! (最多25個)",description=f"目前歌單中的歌曲數量: {len(data[self.name]['tracks'])}"))

0 comments on commit 749d493

Please sign in to comment.