diff --git a/bot.py b/bot.py index 9b3389c..2bf0e1e 100644 --- a/bot.py +++ b/bot.py @@ -111,6 +111,12 @@ class TuneBot(commands.Bot): print(f"Version: {discord.__version__}") print(f"Invite: {self.invite_link}") + ll = self.config["lavalink"] + self.lavalink = lavalink.Client(self.user.id) + self.lavalink.add_node( + ll["host"], ll["port"], ll["password"], ll["region"], ll["name"] + ) + def process_colours(self, colors: Dict[str, str]) -> Dict[str, Color]: colour_dict: Dict[str, Color] = {} for name, color in colors.items(): diff --git a/cogs/music.py b/cogs/music.py index b93d50d..c480533 100644 --- a/cogs/music.py +++ b/cogs/music.py @@ -74,25 +74,11 @@ class LavalinkVoiceClient(discord.VoiceClient): class Music(BaseCog): @commands.Cog.listener() async def on_ready(self): - if not hasattr( - self.bot, "lavalink" - ): # This ensures the client isn't overwritten during cog reloads. - self.bot.lavalink = lavalink.Client(self.bot.user.id) - - ll = self.bot.config["lavalink"] - self.bot.lavalink.add_node( - ll["host"], ll["port"], ll["password"], ll["region"], ll["name"] - ) - - self.bot.lavalink.add_event_hook(self.track_hook) - await self.async_init() - - async def async_init(self): - redis_result = await self.bot.global_autojoin.fetch_channels() - - while len(self.bot.lavalink.node_manager.available_nodes) == 0: + while not self.is_lavalink_ready(): await asyncio.sleep(1) + self.bot.lavalink.add_event_hook(self.track_hook) + redis_result = await self.bot.global_autojoin.fetch_channels() for guild_id, (voicechannel_id, textchannel_id) in redis_result.items(): player = self.bot.lavalink.player_manager.create(guild_id) player.store("channel", textchannel_id) @@ -151,7 +137,7 @@ class Music(BaseCog): # This is essentially the same as `@commands.guild_only()` # except it saves us repeating ourselves (and also a few lines). - if not hasattr(self.bot, "lavalink"): + if not self.is_lavalink_ready(): await ctx.send("Still starting please wait a moment.") if guild_check: diff --git a/context.py b/context.py index ca67efe..0020b0d 100644 --- a/context.py +++ b/context.py @@ -13,10 +13,12 @@ if TYPE_CHECKING: from bot import TuneBot from tunebot import PlaylistSource from tunebot import AutoJoin + from utils.classes import BaseCog class CustomContext(commands.Context): bot: "TuneBot" + cog: "BaseCog" @property def redis(self) -> Redis: @@ -24,7 +26,7 @@ class CustomContext(commands.Context): @property def player(self) -> DefaultPlayer: - if hasattr(self.bot, "lavalink"): + if self.cog.is_lavalink_ready(): return self.bot.lavalink.player_manager.get(self.guild.id) raise CommandInvokeError("Lavalink is still starting up.") diff --git a/utils/classes.py b/utils/classes.py index 9cc95b2..7da2ad3 100644 --- a/utils/classes.py +++ b/utils/classes.py @@ -1,3 +1,4 @@ +import asyncio from typing import Dict from discord.ext.commands import Cog @@ -13,3 +14,9 @@ class BaseCog(Cog): for command in self.walk_commands(): if brief := slash_descriptions.get(command.qualified_name): command.brief = brief + + def is_lavalink_ready(self) -> bool: + return ( + hasattr(self.bot, "lavalink") + and len(self.bot.lavalink.node_manager.available_nodes) > 0 + )