aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorParker <contact@pkrm.dev>2024-12-03 06:05:04 +0000
committerGitHub <noreply@github.com>2024-12-03 06:05:04 +0000
commitce18cd27488d90fbd0aae7319a36a89e9fa85aa7 (patch)
treea5455e0a8391747c7226a751354b7236c8c5d40b
parentfbe8dbe4552b55959980cc93d94adb491336900c (diff)
parentfcbfe460701316ded25e29356ed1fda42386e5c0 (diff)
Merge branch 'main' into dev
-rw-r--r--code/cogs/music.py6
-rw-r--r--code/cogs/owner/stats.py66
2 files changed, 64 insertions, 8 deletions
diff --git a/code/cogs/music.py b/code/cogs/music.py
index 4b3fdbb..4814076 100644
--- a/code/cogs/music.py
+++ b/code/cogs/music.py
@@ -113,10 +113,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()
@@ -135,6 +137,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 6ab9043..00a2c2f 100644
--- a/code/cogs/owner/stats.py
+++ b/code/cogs/owner/stats.py
@@ -3,7 +3,7 @@ import sqlite3
import discord
import os
-from utils.config import BOT_COLOR
+from utils.config import BOT_COLOR, LOG
class Stats(commands.Cog):
@@ -14,6 +14,10 @@ class Stats(commands.Cog):
if not os.path.exists("data"):
os.makedirs("data")
+ if not os.access("data/count.db", os.W_OK):
+ LOG.error("Cannot write to data/count.db - check permissions")
+ return
+
connection = sqlite3.connect("data/count.db")
cursor = connection.cursor()
cursor.execute(
@@ -25,6 +29,16 @@ class Stats(commands.Cog):
self.dump_count.start()
+ def millis_to_readable(self, ms):
+ hours = ms // 3600000
+ ms %= 3600000
+ minutes = ms // 60000
+ ms %= 60000
+ seconds = ms // 1000
+ milliseconds = ms % 1000
+
+ return f"{hours:02}:{minutes:02}:{seconds:02}.{milliseconds:03}"
+
@tasks.loop(seconds=30)
async def dump_count(self):
connection = sqlite3.connect("data/count.db")
@@ -54,10 +68,19 @@ class Stats(commands.Cog):
except KeyError:
self.bot.temp_command_count[interaction.command.name] = 1
- @commands.command()
+ @commands.group(invoke_without_command=True)
@commands.dm_only()
@commands.is_owner()
async def stats(self, ctx: commands.Context):
+ await ctx.author.send(
+ f"This is a group command. Use `{self.bot.command_prefix}stats"
+ " bot/lavalink` to get specific statistics."
+ )
+
+ @stats.command()
+ @commands.dm_only()
+ @commands.is_owner()
+ async def bot(self, ctx: commands.Context):
connection = sqlite3.connect("data/count.db")
cursor = connection.cursor()
@@ -74,10 +97,8 @@ class Stats(commands.Cog):
embed = discord.Embed(
title="Statistics",
description=(
- f"Total Guilds: `{len(self.bot.guilds):,}`\nTotal Commands:"
- f" `{total_commands:,}`\n\nTotal Players:"
- f" `{self.bot.lavalink.nodes[0].stats.playing_players}`\nLoad:"
- f" `{round(self.bot.lavalink.nodes[0].stats.lavalink_load * 100, 2)}%`"
+ f"Total Guilds: `{len(self.bot.guilds):,}`\n"
+ f"Total Commands: `{total_commands:,}`\n\n"
),
color=BOT_COLOR,
)
@@ -90,8 +111,37 @@ class Stats(commands.Cog):
connection.close()
await ctx.send(embed=embed)
- @stats.error
- async def stats_error(self, ctx, error):
+ @bot.error
+ async def bot_error(self, ctx, error):
+ return
+
+ @stats.command()
+ @commands.dm_only()
+ @commands.is_owner()
+ async def lavalink(self, ctx: commands.Context):
+ if not self.bot.lavalink:
+ return await ctx.send("No connection with Lavalink.")
+
+ embed = discord.Embed(
+ title="Lavalink Statistics",
+ color=BOT_COLOR,
+ )
+
+ for node in self.bot.lavalink.nodes:
+ embed.add_field(
+ name=node.name,
+ value=(
+ f"\tPlayers: `{node.stats.players}`\n\tUptime:"
+ f" `{self.millis_to_readable(node.stats.uptime)}`\n\tMemory"
+ f" Used: `{node.stats.memory_used / 1024 / 1024:.2f}MB`\n"
+ ),
+ inline=True,
+ )
+
+ await ctx.send(embed=embed)
+
+ @lavalink.error
+ async def lavalink_error(self, ctx, error):
return