Skip to content

Commit 784e178

Browse files
authored
Merge pull request #989 from allthingslinux/feature/add-uptime-to-ping
feat: added uptime and current branch to status
2 parents 92154ea + 239ee7d commit 784e178

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

tux/bot.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ def __init__(self, *args: Any, **kwargs: Any) -> None:
6262

6363
self.emoji_manager = EmojiManager(self)
6464
self.console = Console(stderr=True, force_terminal=True)
65+
self.uptime = discord.utils.utcnow().timestamp()
6566

6667
logger.debug("Creating bot setup task")
6768
self.setup_task = asyncio.create_task(self.setup(), name="bot_setup")

tux/cogs/utility/ping.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1+
from datetime import UTC, datetime
2+
13
import psutil
24
from discord.ext import commands
35

46
from tux.bot import Tux
57
from tux.ui.embeds import EmbedCreator
8+
from tux.utils.env import get_current_env
69
from tux.utils.functions import generate_usage
710

811

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

1417
@commands.hybrid_command(
1518
name="ping",
19+
aliases=["status"],
1620
)
1721
async def ping(self, ctx: commands.Context[Tux]) -> None:
1822
"""
@@ -27,6 +31,27 @@ async def ping(self, ctx: commands.Context[Tux]) -> None:
2731
# Get the latency of the bot in milliseconds
2832
discord_ping = round(self.bot.latency * 1000)
2933

34+
environment = get_current_env()
35+
36+
# Handles Time (turning POSIX time datetime)
37+
bot_start_time = datetime.fromtimestamp(self.bot.uptime, UTC)
38+
current_time = datetime.now(UTC) # Get current time
39+
uptime_delta = current_time - bot_start_time
40+
41+
# Convert it into Human comprehensible times
42+
days = uptime_delta.days
43+
hours, remainder = divmod(uptime_delta.seconds, 3600)
44+
minutes, seconds = divmod(remainder, 60)
45+
46+
# Format it for the command
47+
bot_uptime_parts = [
48+
f"{days}d" if days else "",
49+
f"{hours}h" if hours else "",
50+
f"{minutes}m" if minutes else "",
51+
f"{seconds}s",
52+
]
53+
bot_uptime_readable = " ".join(part for part in bot_uptime_parts if part).strip()
54+
3055
# Get the CPU usage and RAM usage of the bot
3156
cpu_usage = psutil.Process().cpu_percent()
3257
# Get the amount of RAM used by the bot
@@ -49,8 +74,10 @@ async def ping(self, ctx: commands.Context[Tux]) -> None:
4974
)
5075

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

5582
await ctx.send(embed=embed)
5683

0 commit comments

Comments
 (0)