Add default_search_platform option for customizable search platform

This commit is contained in:
Choco
2025-09-29 21:15:23 +08:00
parent f6722e538a
commit 3831c87f9b
7 changed files with 60 additions and 21 deletions

View File

@@ -214,7 +214,7 @@ class Basic(commands.Cog):
for search_type in voicelink.SearchType
])
@commands.dynamic_cooldown(cooldown_check, commands.BucketType.guild)
async def search(self, ctx: commands.Context, *, query: str, platform: str = voicelink.SearchType.YOUTUBE.name):
async def search(self, ctx: commands.Context, *, query: str, platform: str = Config().search_platform.name):
"Searches your query and displays the results."
player: voicelink.Player = ctx.guild.voice_client
if not player:
@@ -226,7 +226,7 @@ class Basic(commands.Cog):
if url(query):
return await send_localized_message(ctx, "noLinkSupport", ephemeral=True)
search_type: voicelink.SearchType = voicelink.SearchType.match(platform) or voicelink.SearchType.YOUTUBE
search_type: voicelink.SearchType = voicelink.SearchType.match(platform) or Config().search_platform
tracks = await player.get_tracks(query=query, requester=ctx.author, search_type=search_type)
if not tracks:
return await send_localized_message(ctx, "noTrackFound")

View File

@@ -40,6 +40,7 @@
"bot_access_user": [],
"embed_color":"0xb3b3b3",
"default_max_queue": 1000,
"default_search_platform": "youtube",
"lyrics_platform": "lrclib",
"ipc_client": {
"host": "127.0.0.1",

View File

@@ -33,6 +33,8 @@ from typing import (
Optional
)
from .enums import SearchType
load_dotenv()
class Config:
@@ -82,6 +84,7 @@ class Config:
self.invite_link: str = "https://discord.gg/wRCgB7vBQv"
self.nodes: Dict[str, Dict[str, Union[str, int, bool]]] = settings.get("nodes", {})
self.max_queue: int = settings.get("default_max_queue", 1000)
self.search_platform: SearchType = SearchType.match(settings.get("default_search_platform")) or SearchType.YOUTUBE
self.bot_prefix: str = settings.get("prefix", "")
self.activity: List[Dict[str, str]] = settings.get("activity", [{"listen": "/help"}])
self.logging: Dict[Union[str, Dict[str, Union[str, bool]]]] = settings.get("logging", {})

View File

@@ -37,22 +37,43 @@ class LoopType(Enum):
QUEUE = auto()
class SearchType(Enum):
"""The enum for the different search types for Voicelink.
"""Enum representing different search types for Voicelink.
SearchType.YOUTUBE searches using regular Youtube,
which is best for all scenarios.
Each search type corresponds to a specific platform that can be used
for retrieving audio or video content. The options available are:
SearchType.YOUTUBE_MUSIC searches using YouTube Music,
which is best for getting audio-only results.
SearchType.SPOTIFY searches using Spotify,
which is an alternative to YouTube or YouTube Music.
SearchType.SOUNDCLOUD searches using SoundCloud,
which is an alternative to YouTube or YouTube Music.
SearchType.APPLE_MUSIC searches using Apple Music,
which is an alternative to YouTube or YouTube Music.
- SearchType.YOUTUBE:
Searches using regular YouTube, ideal for all scenarios.
- SearchType.YOUTUBE_MUSIC:
Searches using YouTube Music, best for obtaining audio-only results.
- SearchType.SPOTIFY:
Searches using Spotify, a viable alternative to YouTube and YouTube Music.
- SearchType.SOUNDCLOUD:
Searches using SoundCloud, another alternative to YouTube and YouTube Music.
- SearchType.APPLE_MUSIC:
Searches using Apple Music, offering a different option for audio content.
- SearchType.DEEZER:
Searches using Deezer, providing another music streaming alternative.
- SearchType.YANDEX_MUSIC:
Searches using Yandex Music, catering to users in the Yandex ecosystem.
- SearchType.VK_MUSIC:
Searches using VK Music, popular in certain regions for music streaming.
- SearchType.TIDAL:
Searches using Tidal, known for high-fidelity music streaming.
- SearchType.QOBUZ:
Searches using Qobuz, recognized for high-resolution audio.
- SearchType.JIOSAAVN:
Searches using JioSaavn, a popular platform in India for music streaming.
"""
YOUTUBE = "ytsearch"
@@ -60,6 +81,12 @@ class SearchType(Enum):
SPOTIFY = "spsearch"
SOUNDCLOUD = "scsearch"
APPLE_MUSIC = "amsearch"
DEEZER = "dzsearch"
YANDEX_MUSIC = "ymsearch"
VK_MUSIC = "vksearch"
TIDAL = "tdsearch"
QOBUZ = "qbsearch"
JIOSAAVN = "jssearch"
def __str__(self) -> str:
return self.value

View File

@@ -22,7 +22,6 @@ SOFTWARE.
"""
import re
import discord
from typing import Optional
from tldextract import extract
@@ -65,8 +64,11 @@ class Track:
track_id: str = None,
info: dict,
requester: Member,
search_type: SearchType = SearchType.YOUTUBE,
search_type: SearchType = None,
):
if not search_type:
search_type = Config().search_platform
self._track_id: Optional[str] = track_id
self.info: dict = info

View File

@@ -532,13 +532,16 @@ class Player(VoiceProtocol):
query: str,
*,
requester: Member,
search_type: SearchType = SearchType.YOUTUBE
search_type: SearchType = None
) -> Union[List[Track], Playlist]:
"""Fetches tracks from the node's REST api to parse into Lavalink.
You can also pass in a discord.py Context object to get a
Context object on any track you search.
"""
if not search_type:
search_type = Config().search_platform
return await self._node.get_tracks(query, requester=requester, search_type=search_type)
async def connect(self, *, timeout: float, reconnect: bool, self_deaf: bool = True, self_mute: bool = False):

View File

@@ -51,6 +51,7 @@ from .objects import Playlist, Track
from .utils import ExponentialBackoff, NodeStats, NodeInfo, Ping
from .enums import RequestMethod
from .ratelimit import YTRatelimit, YTToken, STRATEGY
from .config import Config
if TYPE_CHECKING:
from .player import Player
@@ -353,7 +354,7 @@ class Node:
query: str,
*,
requester: Member,
search_type: SearchType = SearchType.YOUTUBE
search_type: SearchType = None
) -> Union[List[Track], Playlist]:
"""
Fetches tracks from the node's REST api to parse into Lavalink.
@@ -361,7 +362,9 @@ class Node:
You can also pass in a discord.py Context object to get a
Context object on any track you search.
"""
if not search_type:
search_type = Config().search_platform
if not URL_REGEX.match(query) and ':' not in query:
query = f"{search_type}:{query}"