diff --git a/plugins/core.py b/plugins/core.py index 8bf868d..d24b00f 100644 --- a/plugins/core.py +++ b/plugins/core.py @@ -56,6 +56,26 @@ async def cmd_version(bot, message): await bot.reply(message, f"derp {__version__}") +@command("uptime", help="Show how long the bot has been running") +async def cmd_uptime(bot, message): + """Report bot uptime.""" + import time + + elapsed = int(time.monotonic() - bot._started) + days, rem = divmod(elapsed, 86400) + hours, rem = divmod(rem, 3600) + minutes, secs = divmod(rem, 60) + parts = [] + if days: + parts.append(f"{days}d") + if hours: + parts.append(f"{hours}h") + if minutes: + parts.append(f"{minutes}m") + parts.append(f"{secs}s") + await bot.reply(message, f"up {' '.join(parts)}") + + @command("load", help="Hot-load a plugin: !load ") async def cmd_load(bot, message): """Load a new plugin from the plugins directory.""" diff --git a/src/derp/bot.py b/src/derp/bot.py index d81cad5..2f29522 100644 --- a/src/derp/bot.py +++ b/src/derp/bot.py @@ -4,6 +4,7 @@ from __future__ import annotations import asyncio import logging +import time from pathlib import Path from derp.irc import IRCConnection, Message, format_msg, parse @@ -30,6 +31,7 @@ class Bot: self.nick: str = config["server"]["nick"] self.prefix: str = config["bot"]["prefix"] self._running = False + self._started: float = time.monotonic() async def start(self) -> None: """Connect, register, join channels, and enter the main loop."""