Refactor system check to components

This commit is contained in:
Slipstream 2025-06-14 16:43:37 +00:00
parent b9fb671bed
commit 27c69065ae
Signed by: slipstream
GPG Key ID: 13E498CE010AC6FD

View File

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