Skip to content

Commit 7f3abce

Browse files
committed
check lavalink version and auto update if need
add auto_update config for lavalink self node
1 parent f3fc8c0 commit 7f3abce

File tree

5 files changed

+134
-15
lines changed

5 files changed

+134
-15
lines changed

nameless.toml

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,5 @@ guilds = []
1111
name = "default"
1212
uri = "http://localhost:2333"
1313
password = "youshallnotpass"
14-
is_default = true
15-
autostart = true
14+
auto_start = true
15+
auto_update = true

nameless/command/music.py

+17-8
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
from ..custom.player import CustomPlayer, TrackDropdown
1414
from ..custom.player.settings import sponsorblock_make
1515
from ..custom.player.settings.filters import filter_make
16-
from ..custom.player.settings.sponsorblock_settings import SponsorBlockSettings
16+
17+
# from ..custom.player.settings.sponsorblock_settings import SponsorBlockSettings
1718
from ..custom.ui import ViewButton, ViewMenu
1819
from ..nameless import Nameless
1920
from .check import bot_in_voice
@@ -292,9 +293,9 @@ async def connect(
292293
await channel.connect(self_deaf=True, cls=CustomPlayer)
293294
voice_client = cast(CustomPlayer, ctx.guild.voice_client) # pyright: ignore[reportOptionalMemberAccess]
294295
voice_client.trigger_channel_id = ctx.channel.id # type: ignore
295-
voice_client.sponsorblock_settings = ( # type: ignore
296-
await SponsorBlockSettings.get_from_database(ctx.guild)
297-
)
296+
# voice_client.sponsorblock_settings = ( # type: ignore
297+
# await SponsorBlockSettings.get_from_database(ctx.guild)
298+
# )
298299
await ctx.send(f"Connected to {channel.name}.")
299300
else:
300301
await ctx.send("Failed to connect to the voice channel.")
@@ -679,31 +680,39 @@ async def filter(self, ctx: commands.Context[Nameless]):
679680

680681
async def setup(bot: Nameless):
681682
autostart_lavalink = False
683+
autoupdate_lavalink = False
682684

683685
for node in nameless_config["wavelinks"]:
684-
if node.get("is_default", False) is True:
686+
if node.get("auto_start", False) is True:
685687
autostart_lavalink = True
688+
autoupdate_lavalink = node.get("auto_update", False)
686689
break
687690

688691
if not autostart_lavalink and not nameless_config.get("wavelinks"):
689692
nameless_config["wavelinks"] = [
690-
{"uri": "http://localhost:2333", "password": "youshallnotpass"}
693+
{
694+
"uri": "http://localhost:2333",
695+
"password": "youshallnotpass",
696+
"auto_start": True,
697+
"auto_update": True,
698+
}
691699
]
692700
logging.warning("No Lavalink nodes found. Added a default node.")
693701
autostart_lavalink = True
702+
autoupdate_lavalink = True
694703

695704
if autostart_lavalink:
696705
from ..custom.player.lavalink import main as lavalink_main
697706

698-
await lavalink_main(bot.loop)
707+
await lavalink_main(bot.loop, autoupdate_lavalink)
699708

700709
await bot.add_cog(MusicCommands(bot))
701710
logging.info("%s cog added!", __name__)
702711

703712

704713
async def teardown(bot: Nameless):
705714
for node in nameless_config.get("wavelinks", {}):
706-
if node.get("is_default", False) is True:
715+
if node.get("auto_start", False) is True:
707716
from ..custom.player.lavalink import stop as lavalink_stop
708717

709718
logging.warning("Stop default Lavalink node...")

nameless/config.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ class WavelinkNode(TypedDict):
2424
uri: str
2525
password: str
2626
region: NotRequired[str]
27-
is_default: NotRequired[bool]
28-
autostart: NotRequired[bool]
27+
auto_start: NotRequired[bool]
28+
auto_update: NotRequired[bool]
2929

3030

3131
class NamelessConfig(TypedDict):

nameless/custom/player/lavalink/bin/application.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ lavalink:
2424
# defaultPluginRepository: "https://maven.lavalink.dev/releases" # optional, defaults to the Lavalink release repository
2525
# defaultPluginSnapshotRepository: "https://maven.lavalink.dev/snapshots" # optional, defaults to the Lavalink snapshot repository
2626
# Replace VERSION with the current version as shown by the Releases tab or a long commit hash for snapshots.
27-
- dependency: "dev.lavalink.youtube:youtube-plugin:1.10.2"
27+
- dependency: "dev.lavalink.youtube:youtube-plugin:1.11.4"
2828
snapshot: false
2929
- dependency: "com.github.topi314.sponsorblock:sponsorblock-plugin:3.0.1"
3030
repository: "https://maven.lavalink.dev/releases"

nameless/custom/player/lavalink/start.py

+112-2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,105 @@
1919
stop_event = asyncio.Event()
2020

2121

22+
async def check_plugin_version(auto_update: bool = False) -> bool:
23+
"""
24+
Check for latest version of Lavalink plugin.
25+
The youtube-source plugin to be specific
26+
"""
27+
target = "dev.lavalink.youtube:youtube-plugin:"
28+
29+
with open(LAVALINK_CONFIG) as f:
30+
config = f.read()
31+
start_index = config.find(target) + 36
32+
end_index = config.find('"', start_index)
33+
version = config[start_index:end_index]
34+
35+
if not version:
36+
logging.error("Failed to check Lavalink plugin version. Version not found.")
37+
return False
38+
39+
async with aiohttp.ClientSession() as session:
40+
git_req = await session.get(
41+
"https://api.github.com/repos/lavalink-devs/youtube-source/releases/latest"
42+
)
43+
latest_version: str = (await git_req.json()).get("tag_name", "0.0.0") # pyright: ignore[reportAny]
44+
45+
if version == latest_version:
46+
return True
47+
48+
logging.warning(
49+
"youtube-source plugin version is outdated. Current: %s, Latest: %s",
50+
version,
51+
latest_version,
52+
)
53+
54+
if auto_update:
55+
with open(LAVALINK_CONFIG, "w") as f:
56+
f.write(config.replace(target + version, target + latest_version))
57+
logging.info("Updated youtube-source plugin to version %s", latest_version)
58+
return True
59+
60+
return False
61+
62+
63+
async def check_lavalink_version() -> bool:
64+
"""Check for latest version of Lavalink.
65+
66+
Returns:
67+
bool: True if the version is the latest, False otherwise
68+
"""
69+
try:
70+
proc = await asyncio.create_subprocess_exec(
71+
"java", "-jar", "Lavalink.jar", "-v", cwd=CWD / "bin", stdout=-1, stderr=-1
72+
)
73+
status_code = await proc.wait()
74+
if status_code != 0:
75+
logging.error("Failed to check Lavalink version.")
76+
return False
77+
78+
stdout = proc.stdout
79+
if not stdout:
80+
logging.error("Failed to check Lavalink version. stdout somehow empty.")
81+
return False
82+
83+
version = ""
84+
r = await stdout.read()
85+
r_decode = r.decode("utf-8")
86+
for line in r_decode.splitlines():
87+
if "Version: " in line:
88+
version = line.split("Version: ")[1].strip()
89+
90+
if not version:
91+
logging.error("Failed to check Lavalink version. Version not found.")
92+
return False
93+
async with aiohttp.ClientSession() as session:
94+
git_req = await session.get(
95+
"https://api.github.com/repos/lavalink-devs/Lavalink/releases/latest"
96+
)
97+
latest_version: str = (await git_req.json()).get("tag_name", "0.0.0") # pyright: ignore[reportAny]
98+
99+
if version == latest_version:
100+
return True
101+
102+
logging.warning(
103+
"Lavalink version is outdated. Current: %s, Latest: %s",
104+
version,
105+
latest_version,
106+
)
107+
return False
108+
109+
except FileNotFoundError:
110+
return False
111+
112+
except Exception as e:
113+
logging.error(
114+
"An error occurred while checking Lavalink version [%s]: %s",
115+
e.__class__.__name__,
116+
e,
117+
)
118+
return False
119+
120+
22121
async def start():
23122
"""Start the Lavalink server from /bin folder."""
24123
global proc, stop_event
@@ -69,11 +168,22 @@ async def download_lavalink():
69168
f.write(await resp.read())
70169

71170

72-
async def main(loop: asyncio.AbstractEventLoop):
171+
async def main(loop: asyncio.AbstractEventLoop, auto_update: bool = False):
73172
"""Main function to start the Lavalink server."""
74173
global task
75174

76175
if not check_file():
77-
logging.warning("Lavalink.jar not found. Downloading...")
176+
logging.warning("Lavalink.jar not found Downloading...")
78177
await download_lavalink()
178+
else:
179+
if not await check_lavalink_version():
180+
if auto_update:
181+
logging.info("Updating Lavalink...")
182+
await download_lavalink()
183+
logging.info("Lavalink updated.")
184+
else:
185+
logging.warning("Please update Lavalink to the latest version.")
186+
187+
await check_plugin_version(auto_update)
188+
79189
task = loop.create_task(start())

0 commit comments

Comments
 (0)