From 2c16543e0cfa4d230d2e2fcce7194e947ce362c4 Mon Sep 17 00:00:00 2001 From: Choco <94597336+ChocoMeow@users.noreply.github.com> Date: Tue, 4 Mar 2025 12:01:37 +0800 Subject: [PATCH] Fixed track source --- cogs/basic.py | 11 ++++++----- function.py | 4 ++-- settings Example.json | 6 +++--- voicelink/enums.py | 15 +++++++++++++++ 4 files changed, 26 insertions(+), 10 deletions(-) diff --git a/cogs/basic.py b/cogs/basic.py index fa885dd..49740cd 100644 --- a/cogs/basic.py +++ b/cogs/basic.py @@ -67,7 +67,7 @@ async def nowplay(ctx: commands.Context, player: voicelink.Player): icon = ":red_circle:" if track.is_stream else (":pause_button:" if player.is_paused else ":arrow_forward:") embed.add_field(name="\u2800", value=f"{icon} {pbar} **[{ctime(player.position)}/{track.formatted_length}]**", inline=False) - return await send(ctx, embed, view=LinkView(texts[2].format(track.source), track.emoji, track.uri)) + return await send(ctx, embed, view=LinkView(texts[2].format(track.source.title()), track.emoji, track.uri)) class Basic(commands.Cog): def __init__(self, bot: commands.Bot) -> None: @@ -86,7 +86,7 @@ class Basic(commands.Cog): return [app_commands.Choice(name=c.capitalize(), value=c) for c in self.bot.cogs if c not in ["Nodes", "Task"] and current in c] async def play_autocomplete(self, interaction: discord.Interaction, current: str) -> list: - if voicelink.pool.URL_REGEX.match(current): return [app_commands.Choice(name=current, value=current)] + if voicelink.pool.URL_REGEX.match(current): return [] if current: node = voicelink.NodePool.get_node() @@ -216,7 +216,7 @@ class Basic(commands.Cog): platform="Select the platform you want to search." ) @app_commands.choices(platform=[ - app_commands.Choice(name=search_type.name.replace("_", " ").title(), value=search_type.name) + app_commands.Choice(name=search_type.display_name, value=search_type.name) for search_type in SearchType ]) @commands.dynamic_cooldown(cooldown_check, commands.BucketType.guild) @@ -232,13 +232,14 @@ class Basic(commands.Cog): if url(query): return await send(ctx, "noLinkSupport", ephemeral=True) - tracks = await player.get_tracks(query=query, requester=ctx.author, search_type=SearchType[platform] if platform in SearchType.__members__ else SearchType.YOUTUBE) + search_type: SearchType = SearchType.match(platform) or SearchType.YOUTUBE + tracks = await player.get_tracks(query=query, requester=ctx.author, search_type=search_type) if not tracks: return await send(ctx, "noTrackFound") texts = await get_lang(ctx.guild.id, "searchTitle", "searchDesc", "live", "trackLoad_pos", "trackLoad", "searchWait", "searchSuccess") query_track = "\n".join(f"`{index}.` `[{track.formatted_length}]` **{track.title[:35]}**" for index, track in enumerate(tracks[0:10], start=1)) - embed = discord.Embed(title=texts[0].format(query), description=texts[1].format(get_source(platform, "emoji"), platform, len(tracks[0:10]), query_track), color=settings.embed_color) + embed = discord.Embed(title=texts[0].format(query), description=texts[1].format(get_source(search_type.display_name, "emoji"), search_type.display_name, len(tracks[0:10]), query_track), color=settings.embed_color) view = SearchView(tracks=tracks[0:10], texts=[texts[5], texts[6]]) view.response = await send(ctx, embed, view=view, ephemeral=True) diff --git a/function.py b/function.py index 8d8ec79..2b9d9cd 100644 --- a/function.py +++ b/function.py @@ -131,8 +131,8 @@ def format_time(number:str) -> int: return (int(num.tm_hour) * 3600 + int(num.tm_min) * 60 + int(num.tm_sec)) * 1000 def get_source(source: str, type: str) -> str: - source_settings: dict = settings.sources_settings.get(source.lower(), settings.sources_settings.get("others")) - return source_settings.get(type, ("🔗" if type == "emoji" else settings.embed_color)) + source_settings: dict[str, str] = settings.sources_settings.get(source.lower().replace(" ", ""), settings.sources_settings.get("others")) + return source_settings.get(type) def cooldown_check(ctx: commands.Context) -> Optional[commands.Cooldown]: if ctx.author.id in settings.bot_access_user: diff --git a/settings Example.json b/settings Example.json index 875626c..3c195a5 100644 --- a/settings Example.json +++ b/settings Example.json @@ -55,7 +55,7 @@ "emoji": "<:youtube:826661982760992778>", "color": "0xFF0000" }, - "youtube music": { + "youtubemusic": { "emoji": "<:youtube:826661982760992778>", "color": "0xFF0000" }, @@ -79,7 +79,7 @@ "emoji": "<:vimeo:864694001919721473>", "color": "0x1ABCEA" }, - "apple": { + "applemusic": { "emoji": "<:applemusic:994844332374884413>", "color": "0xE298C4" }, @@ -92,7 +92,7 @@ "color": "0x74ECE9" }, "others": { - "emoji": "🌎", + "emoji": "🔗", "color": "0xb3b3b3" } }, diff --git a/voicelink/enums.py b/voicelink/enums.py index 9fb9578..58a0bc8 100644 --- a/voicelink/enums.py +++ b/voicelink/enums.py @@ -65,6 +65,21 @@ class SearchType(Enum): def __str__(self) -> str: return self.value + + @classmethod + def match(cls, value: str): + """find an enum based on a search string.""" + normalized_value = value.lower().replace("_", "").replace(" ", "") + + for member in cls: + normalized_name = member.name.lower().replace("_", "") + if member.value == value or normalized_name == normalized_value: + return member + return None + + @property + def display_name(self) -> str: + return self.name.replace("_", " ").title() class RequestMethod(Enum): """The enum for the different request methods in Voicelink