Skip to content

Commit

Permalink
Implement lavalink stats command
Browse files Browse the repository at this point in the history
  • Loading branch information
jackra1n committed Mar 22, 2024
1 parent 403a0c3 commit 2bc45ac
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 6 deletions.
36 changes: 35 additions & 1 deletion extensions/music.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from discord import ButtonStyle, Interaction, ui
from discord.ext import commands

import utils
from core import config
from core.bot import Substiify

Expand Down Expand Up @@ -72,7 +73,7 @@ async def ensure_voice(self, ctx: commands.Context):
if wavelink.Pool.nodes is None:
raise NoNodeAccessible()

if ctx.command.name in ["players", "cleanup"]:
if ctx.command.name in ["players", "cleanup", "lavalink"]:
return True

player: wavelink.Player = ctx.voice_client
Expand Down Expand Up @@ -222,6 +223,39 @@ async def players(self, ctx: commands.Context):
embed.description = players_string
await ctx.send(embed=embed, delete_after=60)

@commands.is_owner()
@commands.command(name="lavalink", aliases=["lv"], hidden=True)
async def lavalink_stats(self, ctx: commands.Context):
"""
Shows the Lavalink stats.
"""
stats: wavelink.StatsResponsePayload = await wavelink.Pool.get_node().fetch_stats()
info: wavelink.InfoResponsePayload = await wavelink.Pool.get_node().fetch_info()

players_str = f"[` {stats.players} | {stats.playing} `]"
version_str = f"[` {info.version.semver} `]"
uptime_str = f"[` {utils.seconds_to_human_readable(stats.uptime/1000)} `]"
memory_used = utils.bytes_to_human_readable(stats.memory.used)
memory_free = utils.bytes_to_human_readable(stats.memory.reservable)
memory_str = f"[` {memory_used} | {memory_free} `]"
system_load = round((stats.cpu.system_load * 100), 1)
cpu_str = f"[` {stats.cpu.cores} vCPU | {system_load} `]"
jvm_str = f"[` {info.jvm} `]"
sources_str = f"```ml\n{', '.join(info.source_managers).title()}\n```"
plugins_list_str = [f"({plugin.name} - {plugin.version})" for plugin in info.plugins]
plugins_str = f"```ml\n{', '.join(plugins_list_str).title()}\n```"

embed = discord.Embed(title="Lavalink Info", color=EMBED_COLOR)
embed.add_field(name="Players", value=players_str)
embed.add_field(name="Uptime", value=uptime_str)
embed.add_field(name="Version", value=version_str)
embed.add_field(name="Memory", value=memory_str)
embed.add_field(name="CPU Usage", value=cpu_str)
embed.add_field(name="JVM", value=jvm_str)
embed.add_field(name="Sources", value=sources_str, inline=False)
embed.add_field(name="Plugins", value=plugins_str, inline=False)
await ctx.send(embed=embed)

@commands.hybrid_command()
@commands.check_any(commands.has_permissions(manage_channels=True), commands.is_owner())
async def cleanup(self, ctx: commands.Context, enable: bool = None):
Expand Down
10 changes: 5 additions & 5 deletions utils/general.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ def seconds_to_human_readable(seconds: int) -> str:
if hours >= 24:
days = hours // 24
hours = hours % 24
return f"{int(days)}d, {int(hours)}h, {int(minutes)}m"
return f"{int(hours)}h, {int(minutes)}m"
return f"{int(days)}d {int(hours)}h {int(minutes)}m"
return f"{int(hours)}h {int(minutes)}m"


def bytes_to_human_readable(size_in_bytes: int) -> str:
units = ("B", "KiB", "MiB", "GiB", "TiB")
power = int(math.log(max(abs(size_in_bytes), 1), 1024))
return f"{size_in_bytes / (1024 ** power):.2f} {units[power]}"
units = ("B", "KB", "MB", "GB", "TB")
power = int(math.log(size_in_bytes, 1024))
return f"{size_in_bytes / (1024 ** power):.1f} {units[power]}"

0 comments on commit 2bc45ac

Please sign in to comment.