diff --git a/cogs/commands.py b/cogs/commands.py index c84dfa8..f449e68 100644 --- a/cogs/commands.py +++ b/cogs/commands.py @@ -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 @@ -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 ) ) @@ -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) @@ -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( @@ -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 '非公開'}`" ) ) @@ -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) @@ -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) diff --git a/core/modal.py b/core/modal.py index 4a689c8..5be88f2 100644 --- a/core/modal.py +++ b/core/modal.py @@ -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="正在讀取中...")) @@ -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) diff --git a/core/utils.py b/core/utils.py index f6f1077..b7f61ed 100644 --- a/core/utils.py +++ b/core/utils.py @@ -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 @@ -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