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