Skip to content

Commit

Permalink
Update: fix tracks Keyerror and something
Browse files Browse the repository at this point in the history
  • Loading branch information
Mantouisyummy committed Aug 26, 2023
1 parent 69c000c commit 043ecbb
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 27 deletions.
26 changes: 11 additions & 15 deletions cogs/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import discord
import json
import uuid
import glob

from os import getpid, path
from discord import Option, OptionChoice, ButtonStyle, Embed, ApplicationContext, AutocompleteContext, OptionChoice
Expand Down Expand Up @@ -75,7 +74,7 @@ async def playlist_search(self, ctx: AutocompleteContext):
value = uuid.uuid5(uuid.NAMESPACE_DNS, title).hex
choices.append(
OptionChoice(
name=title, value=value
name=title + f" ({len(data[title]['tracks'])}首)", value=value
)
)

Expand Down Expand Up @@ -717,8 +716,12 @@ async def create(self, ctx: ApplicationContext, name:Option(
if path.isfile(f"./playlist/{ctx.author.id}.json"):
with open(f"./playlist/{ctx.author.id}.json", "r", encoding="utf-8") as f:
data = json.load(f)

if name not in data.keys():
data[name] = {"public": public}
data[name]["public"] = public
data[name]["loadType"] = "PLAYLIST_LOADED"
data[name]["playlistInfo"] = {"name": name, "selectedTrack": -1}
data[name]["tracks"] = []

with open(f"./playlist/{ctx.author.id}.json", "w", encoding="utf-8") as f:
json.dump(data, f, indent=4)
Expand All @@ -730,7 +733,7 @@ async def create(self, ctx: ApplicationContext, name:Option(
)
else:
with open(f"./playlist/{ctx.author.id}.json", "w", encoding="utf-8") as f:
f.write(json.dumps({name: {"public": public}}, indent=4))
f.write(json.dumps({name:{"public": public, "loadType": "PLAYLIST_LOADED", "playlistInfo": {"name": name, "selectedTrack": -1}, "tracks": []}}, indent=4, ensure_ascii=False))

await ctx.interaction.edit_original_response(
embed=SuccessEmbed(
Expand Down Expand Up @@ -775,16 +778,9 @@ async def public(self, ctx: ApplicationContext, playlist:Option(
)
)

if public is True:
await ctx.interaction.edit_original_response(
embed=SuccessEmbed(
f"已切換公開狀態為 `公開`"
)
)
else:
await ctx.interaction.edit_original_response(
await ctx.interaction.edit_original_response(
embed=SuccessEmbed(
f"已切換公開狀態為 `非公開`"
f"已切換公開狀態為 `{'公開' if public is True else '非公開'}`"
)
)

Expand Down Expand Up @@ -888,7 +884,7 @@ async def join(self, ctx: ApplicationContext, playlist:Option(
})


data[name].update({"loadType": "PLAYLIST_LOADED", "playlistInfo": {"name": playlist, "selectedTrack": -1}, "tracks": data[name]['tracks']})
data[name]["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 @@ -978,7 +974,7 @@ async def playlist_play(self, ctx: ApplicationContext, playlist:Option(
)):
await ctx.response.defer()
try:
await ctx.interaction.response.send_message(embed=LoadingEmbed(title="正在讀取中..."))
await ctx.interaction.edit_original_response(embed=LoadingEmbed(title="正在讀取中..."))

name = ""
name, id = await find_playlist(playlist=playlist, ctx=ctx, public=True)
Expand Down
20 changes: 10 additions & 10 deletions core/modal.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,15 @@ async def callback(self, interaction: Interaction) -> Optional[LoadResult]:
result = await self.bot.lavalink.get_tracks(query)
for track in result.tracks:
data[self.name]['tracks'].append({
'track': track.track,
'identifier': track.identifier,
'isSeekable': track.is_seekable,
'author': track.author,
'length': track.duration,
'isStream': track.stream,
'title': track.title,
'uri': f"https://www.youtube.com/watch?v={track.identifier}"
})
'track': track.track,
'identifier': track.identifier,
'isSeekable': track.is_seekable,
'author': track.author,
'length': track.duration,
'isStream': track.stream,
'title': track.title,
'uri': f"https://www.youtube.com/watch?v={track.identifier}"
})

await interaction.response.send_message(embed=LoadingEmbed(title="正在讀取中..."))

Expand All @@ -45,7 +45,7 @@ async def callback(self, interaction: Interaction) -> Optional[LoadResult]:
self.name = name
break

data[self.name].update({"loadType": "PLAYLIST_LOADED", "playlistInfo": {"name": self.name, "selectedTrack": -1}, "tracks": data[self.name]['tracks']})
data[self.name]["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)
Expand Down
13 changes: 11 additions & 2 deletions core/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import json

from os import path
from typing import Union, Iterable, Optional
from typing import Union, Iterable, Optional, Tuple

from discord import ApplicationContext, Message, Thread, TextChannel, Embed, NotFound, Colour, ButtonStyle, Interaction
from discord.abc import GuildChannel
Expand Down Expand Up @@ -78,7 +78,16 @@ def split_list(input_list, chunk_size) -> Iterable[list]:
yield input_list[num_sublists * chunk_size:]


async def find_playlist(playlist:str, ctx:ApplicationContext, public:bool):
async def find_playlist(playlist:str, ctx:ApplicationContext, public:bool) -> Union[Tuple[str, Optional[str]], None]:
"""
Find a playlist by uuid.
:param playlist: The uuid of the playlist.
:param ctx: The application context.
:param public: Flag indicating whether to search for public playlists.
:return: A tuple containing the title and ID of the found playlist if it exists and meets the criteria,
or None if the playlist doesn't exist or doesn't meet the criteria.
"""
title = ""
id = None
uid = 0
Expand Down

0 comments on commit 043ecbb

Please sign in to comment.