bot.lavalink = None if no connection is made

This commit is contained in:
Parker M. 2024-11-19 22:36:06 -06:00
parent f25d05ec42
commit 082ca714a1
Signed by: parker
GPG Key ID: 505ED36FC12B5D5E
2 changed files with 22 additions and 4 deletions

View File

@ -1,8 +1,8 @@
import discord import discord
from discord.ext import commands from discord.ext import commands
from discord import app_commands
import lavalink import lavalink
from lavalink import errors from lavalink import errors
from discord.ext import tasks
from utils.config import ( from utils.config import (
LAVALINK_HOST, LAVALINK_HOST,
@ -111,10 +111,12 @@ class Music(commands.Cog):
try: try:
await node.get_version() await node.get_version()
except lavalink.errors.ClientError: except lavalink.errors.ClientError:
self.bot.lavalink = None
LOG.error( LOG.error(
"Authentication to lavalink node failed. Check your login" "Authentication to lavalink node failed. Check your login"
" credentials." " credentials."
) )
return
else: else:
await node.connect() await node.connect()
LOG.info(f"Connected to lavalink node {node.name}") LOG.info(f"Connected to lavalink node {node.name}")
@ -128,6 +130,10 @@ class Music(commands.Cog):
async def create_player(interaction: discord.Interaction): async def create_player(interaction: discord.Interaction):
"""Create a player for the guild associated with the interaction, or raise an error""" """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: try:
player = interaction.client.lavalink.player_manager.create( player = interaction.client.lavalink.player_manager.create(
interaction.guild.id interaction.guild.id

View File

@ -2,6 +2,7 @@ from discord.ext import commands, tasks
import sqlite3 import sqlite3
import discord import discord
import os import os
import lavalink
from utils.config import BOT_COLOR, LOG from utils.config import BOT_COLOR, LOG
@ -80,13 +81,24 @@ class Stats(commands.Cog):
description=( description=(
f"Total Guilds: `{len(self.bot.guilds):,}`\n" f"Total Guilds: `{len(self.bot.guilds):,}`\n"
f"Total Commands: `{total_commands:,}`\n\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, 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: for entry in data:
embed.add_field( embed.add_field(
name=entry[0], value=f"` {entry[1]:,} `", inline=True name=entry[0], value=f"` {entry[1]:,} `", inline=True