From 082ca714a146ac956a82611755264e3268e05233 Mon Sep 17 00:00:00 2001 From: Parker Date: Tue, 19 Nov 2024 22:36:06 -0600 Subject: [PATCH] `bot.lavalink = None` if no connection is made --- code/cogs/music.py | 8 +++++++- code/cogs/owner/stats.py | 18 +++++++++++++++--- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/code/cogs/music.py b/code/cogs/music.py index f3e98b4..0bed94b 100644 --- a/code/cogs/music.py +++ b/code/cogs/music.py @@ -1,8 +1,8 @@ import discord from discord.ext import commands -from discord import app_commands import lavalink from lavalink import errors +from discord.ext import tasks from utils.config import ( LAVALINK_HOST, @@ -111,10 +111,12 @@ class Music(commands.Cog): try: await node.get_version() except lavalink.errors.ClientError: + self.bot.lavalink = None LOG.error( "Authentication to lavalink node failed. Check your login" " credentials." ) + return else: await node.connect() LOG.info(f"Connected to lavalink node {node.name}") @@ -128,6 +130,10 @@ class Music(commands.Cog): async def create_player(interaction: discord.Interaction): """Create a player for the guild associated with the interaction, or raise an error""" + if not interaction.client.lavalink: + LOG.error("Lavalink is not connected.") + return + try: player = interaction.client.lavalink.player_manager.create( interaction.guild.id diff --git a/code/cogs/owner/stats.py b/code/cogs/owner/stats.py index 5409bfb..04da7ca 100644 --- a/code/cogs/owner/stats.py +++ b/code/cogs/owner/stats.py @@ -2,6 +2,7 @@ from discord.ext import commands, tasks import sqlite3 import discord import os +import lavalink from utils.config import BOT_COLOR, LOG @@ -80,13 +81,24 @@ class Stats(commands.Cog): description=( f"Total Guilds: `{len(self.bot.guilds):,}`\n" f"Total Commands: `{total_commands:,}`\n\n" - f"Total Players: `{len(self.bot.lavalink.get_players())}`\n" - "Load:" - f" `{round(self.bot.lavalink.nodes[0].stats.lavalink_load * 100, 2)}%`" ), color=BOT_COLOR, ) + # Determine the content of the Lavalink description + if self.bot.lavalink: + embed.description += ( + "Total Players:" + f" `{len(self.bot.lavalink.get_players())}`\n" + "Load:" + f" `{round(self.bot.lavalink.nodes[0].stats.lavalink_load * 100, 2)}%`" + ) + else: + embed.description += ( + "Total Players: `NO LAVALINK CONNECTION`\n" + "Load: `NO LAVALINK CONNECTION`" + ) + for entry in data: embed.add_field( name=entry[0], value=f"` {entry[1]:,} `", inline=True