feat(system-check): Display system status using UI views

Refactors the system check command to utilize `discord.ui.LayoutView` instead of traditional embeds for presenting system and bot information. This provides a more structured and visually distinct layout for the status output.
This commit is contained in:
Slipstreamm 2025-06-14 04:57:20 -06:00
parent 28d0c11e48
commit c40bb8ccab

View File

@ -1,6 +1,6 @@
import discord import discord
from discord.ext import commands from discord.ext import commands
from discord import app_commands from discord import app_commands, ui
import time import time
import psutil import psutil
import platform import platform
@ -20,21 +20,8 @@ class SystemCheckCog(commands.Cog):
def __init__(self, bot): def __init__(self, bot):
self.bot = bot self.bot = bot
async def _system_check_logic(self, context_or_interaction): async def _build_system_check_view(self, context_or_interaction):
"""Check the bot and system status.""" """Return a view with detailed bot and system information."""
# Defer the response to prevent interaction timeout
await context_or_interaction.response.defer(thinking=True)
try:
embed = await self._system_check_logic(context_or_interaction)
await context_or_interaction.followup.send(embed=embed)
except Exception as e:
print(f"Error in systemcheck command: {e}")
await context_or_interaction.followup.send(
f"An error occurred while checking system status: {e}"
)
async def _system_check_logic(self, context_or_interaction):
"""Return detailed bot and system information as a Discord embed."""
# Bot information # Bot information
bot_user = self.bot.user bot_user = self.bot.user
guild_count = len(self.bot.guilds) guild_count = len(self.bot.guilds)
@ -153,62 +140,76 @@ class SystemCheckCog(commands.Cog):
user = self.bot.user # Or some default user = self.bot.user # Or some default
avatar_url = self.bot.user.display_avatar.url if self.bot.user else None avatar_url = self.bot.user.display_avatar.url if self.bot.user else None
# Create embed class SystemStatusView(ui.LayoutView):
embed = discord.Embed(title="📊 System Status", color=discord.Color.blue()) def __init__(self) -> None:
if bot_user: super().__init__(timeout=None)
embed.set_thumbnail(url=bot_user.display_avatar.url)
# Bot Info Field container = ui.Container(accent_colour=discord.Color.blue())
if bot_user: self.add_item(container)
embed.add_field(
name="🤖 Bot Information",
value=f"**Name:** {bot_user.name}\n"
f"**ID:** {bot_user.id}\n"
f"**Servers:** {guild_count}\n"
f"**Unique Users:** {user_count}",
inline=False,
)
else:
embed.add_field(
name="🤖 Bot Information",
value="Bot user information not available.",
inline=False,
)
# System Info Field if bot_user:
embed.add_field( header = ui.Section(
name="🖥️ System Information", accessory=ui.Thumbnail(media=bot_user.display_avatar.url)
value=f"**OS:** {os_info}{distro_info_str}\n" # Use renamed variable )
f"**Hostname:** {hostname}\n" header.add_item(ui.TextDisplay("**📊 System Status**"))
f"**Uptime:** {uptime}", container.add_item(header)
inline=False, else:
) container.add_item(ui.TextDisplay("**📊 System Status**"))
# Hardware Info Field container.add_item(ui.Separator(spacing=discord.SeparatorSpacing.small))
embed.add_field(
name="⚙️ Hardware Information",
value=f"**Device Model:** {motherboard_info}\n"
f"**CPU:** {cpu_name}\n"
f"**CPU Usage:** {cpu_usage}%\n"
f"**RAM Usage:** {ram_usage}\n"
f"**GPU Info:**\n{gpu_info}",
inline=False,
)
if user: container.add_item(ui.TextDisplay("**🤖 Bot Information**"))
embed.set_footer( if bot_user:
text=f"Requested by: {user.display_name}", icon_url=avatar_url bot_info = (
) f"**Name:** {bot_user.name}\n"
f"**ID:** {bot_user.id}\n"
f"**Servers:** {guild_count}\n"
f"**Unique Users:** {user_count}"
)
else:
bot_info = "Bot user information not available."
container.add_item(ui.TextDisplay(bot_info))
embed.timestamp = discord.utils.utcnow() container.add_item(ui.Separator(spacing=discord.SeparatorSpacing.small))
return embed
container.add_item(ui.TextDisplay("**🖥️ System Information**"))
container.add_item(
ui.TextDisplay(
f"**OS:** {os_info}{distro_info_str}\n"
f"**Hostname:** {hostname}\n"
f"**Uptime:** {uptime}"
)
)
container.add_item(ui.Separator(spacing=discord.SeparatorSpacing.small))
container.add_item(ui.TextDisplay("**⚙️ Hardware Information**"))
container.add_item(
ui.TextDisplay(
f"**Device Model:** {motherboard_info}\n"
f"**CPU:** {cpu_name}\n"
f"**CPU Usage:** {cpu_usage}%\n"
f"**RAM Usage:** {ram_usage}\n"
f"**GPU Info:**\n{gpu_info}"
)
)
container.add_item(ui.Separator(spacing=discord.SeparatorSpacing.small))
timestamp = discord.utils.format_dt(discord.utils.utcnow(), style="f")
footer_text = timestamp
if user:
footer_text += f" | Requested by: {user.display_name}"
container.add_item(ui.TextDisplay(footer_text))
return SystemStatusView()
# --- Prefix Command --- # --- Prefix Command ---
@commands.command(name="systemcheck") @commands.command(name="systemcheck")
async def system_check(self, ctx: commands.Context): async def system_check(self, ctx: commands.Context):
"""Check the bot and system status.""" """Check the bot and system status."""
embed = await self._system_check_logic(ctx) # Pass context view = await self._build_system_check_view(ctx)
await ctx.reply(embed=embed) await ctx.reply(view=view)
# --- Slash Command --- # --- Slash Command ---
@app_commands.command( @app_commands.command(
@ -219,9 +220,8 @@ class SystemCheckCog(commands.Cog):
# Defer the response to prevent interaction timeout # Defer the response to prevent interaction timeout
await interaction.response.defer(thinking=True) await interaction.response.defer(thinking=True)
try: try:
embed = await self._system_check_logic(interaction) # Pass interaction view = await self._build_system_check_view(interaction)
# Use followup since we've already deferred await interaction.followup.send(view=view)
await interaction.followup.send(embed=embed)
except Exception as e: except Exception as e:
# Handle any errors that might occur during processing # Handle any errors that might occur during processing
print(f"Error in system_check_slash: {e}") print(f"Error in system_check_slash: {e}")