diff --git a/code/cogs/owner/stats.py b/code/cogs/owner/stats.py index 04da7ca..00a2c2f 100644 --- a/code/cogs/owner/stats.py +++ b/code/cogs/owner/stats.py @@ -2,7 +2,6 @@ from discord.ext import commands, tasks import sqlite3 import discord import os -import lavalink from utils.config import BOT_COLOR, LOG @@ -30,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") @@ -59,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() @@ -85,20 +103,6 @@ class Stats(commands.Cog): 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 @@ -107,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