mirror of
https://github.com/Matthww/TuneBot.git
synced 2026-09-21 22:07:48 +00:00
Merge pull request #38 from strNophix/decoupling-lavalink
decoupled lavalink creation from cogs.music
This commit is contained in:
@@ -87,6 +87,12 @@ class TuneBot(commands.Bot):
|
|||||||
print(f"Version: {discord.__version__}")
|
print(f"Version: {discord.__version__}")
|
||||||
print(f"Invite: {self.invite_link}")
|
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]:
|
def process_colours(self, colors: Dict[str, str]) -> Dict[str, Color]:
|
||||||
colour_dict: Dict[str, Color] = {}
|
colour_dict: Dict[str, Color] = {}
|
||||||
for name, color in colors.items():
|
for name, color in colors.items():
|
||||||
|
|||||||
+4
-18
@@ -76,25 +76,11 @@ class LavalinkVoiceClient(discord.VoiceClient):
|
|||||||
class Music(BaseCog):
|
class Music(BaseCog):
|
||||||
@commands.Cog.listener()
|
@commands.Cog.listener()
|
||||||
async def on_ready(self):
|
async def on_ready(self):
|
||||||
if not hasattr(
|
while not self.is_lavalink_ready():
|
||||||
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 AutoJoin.get_channels(self.bot._redis_client)
|
|
||||||
|
|
||||||
while len(self.bot.lavalink.node_manager.available_nodes) == 0:
|
|
||||||
await asyncio.sleep(1)
|
await asyncio.sleep(1)
|
||||||
|
|
||||||
|
self.bot.lavalink.add_event_hook(self.track_hook)
|
||||||
|
redis_result = await AutoJoin.get_channels(self.bot._redis_client)
|
||||||
for guild_id, (voicechannel_id, textchannel_id) in redis_result.items():
|
for guild_id, (voicechannel_id, textchannel_id) in redis_result.items():
|
||||||
player = self.bot.lavalink.player_manager.create(guild_id)
|
player = self.bot.lavalink.player_manager.create(guild_id)
|
||||||
player.store("channel", textchannel_id)
|
player.store("channel", textchannel_id)
|
||||||
@@ -153,7 +139,7 @@ class Music(BaseCog):
|
|||||||
# This is essentially the same as `@commands.guild_only()`
|
# This is essentially the same as `@commands.guild_only()`
|
||||||
# except it saves us repeating ourselves (and also a few lines).
|
# 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.")
|
await ctx.send("Still starting please wait a moment.")
|
||||||
|
|
||||||
if guild_check:
|
if guild_check:
|
||||||
|
|||||||
+5
-1
@@ -8,16 +8,20 @@ from lavalink.models import DefaultPlayer
|
|||||||
|
|
||||||
if TYPE_CHECKING:
|
if TYPE_CHECKING:
|
||||||
from bot import TuneBot
|
from bot import TuneBot
|
||||||
|
from utils.classes import BaseCog
|
||||||
|
|
||||||
|
|
||||||
class CustomContext(commands.Context):
|
class CustomContext(commands.Context):
|
||||||
|
bot: "TuneBot"
|
||||||
|
cog: "BaseCog"
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def redis(self) -> Redis:
|
def redis(self) -> Redis:
|
||||||
return self.bot._redis_client
|
return self.bot._redis_client
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def player(self) -> DefaultPlayer:
|
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)
|
return self.bot.lavalink.player_manager.get(self.guild.id)
|
||||||
|
|
||||||
raise CommandInvokeError("Lavalink is still starting up.")
|
raise CommandInvokeError("Lavalink is still starting up.")
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
import asyncio
|
||||||
from typing import Dict
|
from typing import Dict
|
||||||
|
|
||||||
from discord.ext.commands import Cog
|
from discord.ext.commands import Cog
|
||||||
@@ -13,3 +14,9 @@ class BaseCog(Cog):
|
|||||||
for command in self.walk_commands():
|
for command in self.walk_commands():
|
||||||
if brief := slash_descriptions.get(command.qualified_name):
|
if brief := slash_descriptions.get(command.qualified_name):
|
||||||
command.brief = brief
|
command.brief = brief
|
||||||
|
|
||||||
|
def is_lavalink_ready(self) -> bool:
|
||||||
|
return (
|
||||||
|
hasattr(self.bot, "lavalink")
|
||||||
|
and len(self.bot.lavalink.node_manager.available_nodes) > 0
|
||||||
|
)
|
||||||
|
|||||||
+2
-2
@@ -12,7 +12,7 @@ class AutoJoin:
|
|||||||
async def get_channels(redis: Redis) -> Dict[str, str]:
|
async def get_channels(redis: Redis) -> Dict[str, str]:
|
||||||
# return all channels
|
# return all channels
|
||||||
channels = await redis.hgetall(f"{redis_prefix}:autojoin")
|
channels = await redis.hgetall(f"{redis_prefix}:autojoin")
|
||||||
return {key: value.split("-") for key, value in channels.items()}
|
return {key: value.split(":") for key, value in channels.items()}
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
async def update_channel(
|
async def update_channel(
|
||||||
@@ -21,7 +21,7 @@ class AutoJoin:
|
|||||||
await redis.hset(
|
await redis.hset(
|
||||||
f"{redis_prefix}:autojoin",
|
f"{redis_prefix}:autojoin",
|
||||||
guild_id,
|
guild_id,
|
||||||
f"{voice_channel_id}-{text_channel_id}",
|
f"{voice_channel_id}:{text_channel_id}",
|
||||||
)
|
)
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
|
|||||||
Reference in New Issue
Block a user