Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions tux/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ def __init__(self, *args: Any, **kwargs: Any) -> None:

self.emoji_manager = EmojiManager(self)
self.console = Console(stderr=True, force_terminal=True)
self.uptime = discord.utils.utcnow().timestamp()

logger.debug("Creating bot setup task")
self.setup_task = asyncio.create_task(self.setup(), name="bot_setup")
Expand Down
27 changes: 27 additions & 0 deletions tux/cogs/utility/ping.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
from datetime import UTC, datetime

import psutil
from discord.ext import commands

from tux.bot import Tux
from tux.ui.embeds import EmbedCreator
from tux.utils.env import get_current_env
from tux.utils.functions import generate_usage


Expand All @@ -13,6 +16,7 @@ def __init__(self, bot: Tux) -> None:

@commands.hybrid_command(
name="ping",
aliases=["status"],
)
async def ping(self, ctx: commands.Context[Tux]) -> None:
"""
Expand All @@ -27,6 +31,27 @@ async def ping(self, ctx: commands.Context[Tux]) -> None:
# Get the latency of the bot in milliseconds
discord_ping = round(self.bot.latency * 1000)

environment = get_current_env()

# Handles Time (turning POSIX time datetime)
bot_start_time = datetime.fromtimestamp(self.bot.uptime, UTC)
current_time = datetime.now(UTC) # Get current time
uptime_delta = current_time - bot_start_time

# Convert it into Human comprehensible times
days = uptime_delta.days
hours, remainder = divmod(uptime_delta.seconds, 3600)
minutes, seconds = divmod(remainder, 60)

# Format it for the command
bot_uptime_parts = [
f"{days}d" if days else "",
f"{hours}h" if hours else "",
f"{minutes}m" if minutes else "",
f"{seconds}s",
]
bot_uptime_readable = " ".join(part for part in bot_uptime_parts if part).strip()

# Get the CPU usage and RAM usage of the bot
cpu_usage = psutil.Process().cpu_percent()
# Get the amount of RAM used by the bot
Expand All @@ -49,8 +74,10 @@ async def ping(self, ctx: commands.Context[Tux]) -> None:
)

embed.add_field(name="API Latency", value=f"{discord_ping}ms", inline=True)
embed.add_field(name="Uptime", value=f"{bot_uptime_readable}", inline=True)
embed.add_field(name="CPU Usage", value=f"{cpu_usage}%", inline=True)
embed.add_field(name="RAM Usage", value=f"{ram_amount_formatted}", inline=True)
embed.add_field(name="Prod/Dev", value=f"`{environment}`", inline=True)

await ctx.send(embed=embed)

Expand Down