From dd61b669b069d8f98a6437251d57c623ff7b6651 Mon Sep 17 00:00:00 2001 From: Jakub Kuczys Date: Wed, 10 Jul 2024 16:13:23 +0200 Subject: [PATCH] Use YouTube source plugin over the deprecated built-in source (#6373) Co-authored-by: aikaterna <20862007+aikaterna@users.noreply.github.com> --- redbot/cogs/audio/core/commands/audioset.py | 14 ++++++++++ redbot/cogs/audio/manager.py | 31 ++++++++++++++++++--- 2 files changed, 41 insertions(+), 4 deletions(-) diff --git a/redbot/cogs/audio/core/commands/audioset.py b/redbot/cogs/audio/core/commands/audioset.py index 57194fd649e..f3dfb0837d6 100644 --- a/redbot/cogs/audio/core/commands/audioset.py +++ b/redbot/cogs/audio/core/commands/audioset.py @@ -1150,6 +1150,20 @@ async def command_audioset_settings(self, ctx: commands.Context): if is_owner: msg += _("Localtracks path: [{localpath}]\n").format(**global_data) + if ( + is_owner + and not global_data["use_external_lavalink"] + and self.managed_node_controller.plugins + ): + plugins = self.managed_node_controller.plugins + msg += f"\n---{_('Lavalink Plugin Versions')}---" + plugin_name_max_len = 0 + for plugin_name, __ in plugins.items(): + plugin_name_max_len = max(plugin_name_max_len, len(plugin_name)) + for plugin_name, plugin_version in plugins.items(): + key = f"{plugin_name}:".ljust(plugin_name_max_len + 5) + msg += f"\n{key} [{plugin_version}]" + await self.send_embed_msg(ctx, description=box(msg, lang="ini")) @command_audioset.command(name="logs") diff --git a/redbot/cogs/audio/manager.py b/redbot/cogs/audio/manager.py index 7e7d0375a6d..7023d10f33c 100644 --- a/redbot/cogs/audio/manager.py +++ b/redbot/cogs/audio/manager.py @@ -49,7 +49,11 @@ _ = Translator("Audio", pathlib.Path(__file__)) log = getLogger("red.Audio.manager") -_FAILED_TO_START: Final[Pattern] = re.compile(rb"Web server failed to start\. (.*)") +_LL_READY_LOG: Final[bytes] = b"Lavalink is ready to accept connections." +_LL_PLUGIN_LOG: Final[Pattern[bytes]] = re.compile( + rb"Found plugin '(?P.+)' version (?P\S+)$", re.MULTILINE +) +_FAILED_TO_START: Final[Pattern[bytes]] = re.compile(rb"Web server failed to start\. (.*)") # Version regexes # @@ -295,11 +299,14 @@ def __ge__(self, other: object) -> bool: class ServerManager: JAR_VERSION: Final[str] = LavalinkVersion(3, 7, 11, red=3) + YT_PLUGIN_VERSION: Final[str] = "1.4.0" + LAVALINK_DOWNLOAD_URL: Final[str] = ( "https://github.com/Cog-Creators/Lavalink-Jars/releases/download/" f"{JAR_VERSION}/" "Lavalink.jar" ) + YT_PLUGIN_REPOSITORY: Final[str] = "https://maven.lavalink.dev/releases" _java_available: ClassVar[Optional[bool]] = None _java_version: ClassVar[Optional[Tuple[int, int]]] = None @@ -323,6 +330,7 @@ def __init__(self, config: Config, cog: "Audio", timeout: Optional[int] = None) self.cog = cog self._args = [] self._pipe_task = None + self.plugins: dict[str, str] = {} @property def lavalink_download_dir(self) -> pathlib.Path: @@ -422,7 +430,20 @@ async def _start(self, java_path: str) -> None: async def process_settings(self): data = change_dict_naming_convention(await self._config.yaml.all()) - with open(self.lavalink_app_yml, "w") as f: + ll_config = data["lavalink"] + sources = ll_config["server"]["sources"] + plugins = ll_config.setdefault("plugins", []) + + enable_yt_plugin = sources["youtube"] + if enable_yt_plugin: + sources["youtube"] = False + yt_plugin = { + "dependency": f"dev.lavalink.youtube:youtube-plugin:{self.YT_PLUGIN_VERSION}", + "repository": self.YT_PLUGIN_REPOSITORY, + } + plugins.append(yt_plugin) + + with open(self.lavalink_app_yml, "w", encoding="utf-8") as f: yaml.safe_dump(data, f) async def _get_jar_args(self) -> Tuple[List[str], Optional[str]]: @@ -518,12 +539,14 @@ async def _wait_for_launcher(self) -> None: log.info("Waiting for Managed Lavalink node to be ready") for i in itertools.cycle(range(50)): line = await self._proc.stdout.readline() - if b"Lavalink is ready to accept connections." in line: + if _LL_READY_LOG in line: self.ready.set() log.info("Managed Lavalink node is ready to receive requests.") self._pipe_task = asyncio.create_task(self._pipe_output()) break - if _FAILED_TO_START.search(line): + if match := _LL_PLUGIN_LOG.search(line): + self.plugins[match["name"].decode()] = match["version"].decode() + elif _FAILED_TO_START.search(line): raise ManagedLavalinkStartFailure( f"Lavalink failed to start: {line.decode().strip()}" )