Refactor system check to LayoutView #15

Open
slipstream wants to merge 1 commits from pr/system-check-components into master

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
@ -20,21 +20,8 @@ class SystemCheckCog(commands.Cog):
def __init__(self, bot):
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."""
async def _build_system_check_view(self, context_or_interaction):
"""Return a view with detailed bot and system information."""
# Bot information
bot_user = self.bot.user
guild_count = len(self.bot.guilds)
@ -153,62 +140,76 @@ 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)
class SystemStatusView(ui.LayoutView):
def __init__(self) -> None:
super().__init__(timeout=None)
# 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,
)
else:
embed.add_field(
name="🤖 Bot Information",
value="Bot user information not available.",
inline=False,
)
container = ui.Container(accent_colour=discord.Color.blue())
self.add_item(container)
# 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,
)
if bot_user:
header = ui.Section(
accessory=ui.Thumbnail(media=bot_user.display_avatar.url)
)
header.add_item(ui.TextDisplay("**📊 System Status**"))
container.add_item(header)
else:
container.add_item(ui.TextDisplay("**📊 System Status**"))
# 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,
)
container.add_item(ui.Separator(spacing=discord.SeparatorSpacing.small))
if user:
embed.set_footer(
text=f"Requested by: {user.display_name}", icon_url=avatar_url
)
container.add_item(ui.TextDisplay("**🤖 Bot Information**"))
if bot_user:
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()
return embed
container.add_item(ui.Separator(spacing=discord.SeparatorSpacing.small))
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 ---
@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)
view = await self._build_system_check_view(ctx)
await ctx.reply(view=view)
# --- Slash Command ---
@app_commands.command(
@ -219,9 +220,8 @@ class SystemCheckCog(commands.Cog):
# Defer the response to prevent interaction timeout
await 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._build_system_check_view(interaction)
await interaction.followup.send(view=view)
except Exception as e:
# Handle any errors that might occur during processing
print(f"Error in system_check_slash: {e}")