diff --git a/README.md b/README.md index c43c6d7..4e53a3e 100644 --- a/README.md +++ b/README.md @@ -49,9 +49,9 @@ i. e. the text between the end of the "!echo" and the end of the whole message. ## TODOs -- [ ] implement !uptime for proper botrulez conformity - [ ] implement !kill - [ ] implement !restart and add an easier way to run bots +- [ ] untruncate LiveMessage-s - [ ] config file support for bots, used by default - [ ] package in a distutils-compatible way (users should be able to install yaboli using `pip install git+https://github.com/Garmelon/yaboli`) @@ -62,3 +62,4 @@ i. e. the text between the end of the "!echo" and the end of the whole message. - [ ] document new classes (docstrings, maybe comments) - [ ] write project readme - [ ] write examples +- [x] implement !uptime for proper botrulez conformity diff --git a/yaboli/bot.py b/yaboli/bot.py index 942470d..cc6a74a 100644 --- a/yaboli/bot.py +++ b/yaboli/bot.py @@ -1,3 +1,4 @@ +import datetime import logging from typing import List, Optional @@ -5,6 +6,7 @@ from .client import Client from .command import * from .message import LiveMessage, Message from .room import Room +from .util import * logger = logging.getLogger(__name__) @@ -20,6 +22,8 @@ class Bot(Client): self._commands: List[Command] = [] + self.start_time = datetime.datetime.now() + # Registering commands def register(self, command: Command) -> None: @@ -74,7 +78,8 @@ class Bot(Client): def register_botrulez(self, ping: bool = True, - help_: bool = True + help_: bool = True, + uptime: bool = True ) -> None: if ping: self.register_general("ping", self.cmd_ping, args=False) @@ -87,6 +92,9 @@ class Bot(Client): self.register_general("help", self.cmd_help_general, args=False) self.register_specific("help", self.cmd_help_specific, args=False) + if uptime: + self.register_specific("uptime", self.cmd_uptime, args=False) + async def cmd_ping(self, room: Room, message: LiveMessage, @@ -109,3 +117,13 @@ class Bot(Client): ) -> None: if self.HELP_SPECIFIC is not None: await message.reply(self.format_help(room, self.HELP_SPECIFIC)) + + async def cmd_uptime(self, + room: Room, + message: LiveMessage, + args: SpecificArgumentData + ) -> None: + time = format_time(self.start_time) + delta = format_delta(datetime.datetime.now() - self.start_time) + text = f"/me has been up since {time} UTC ({delta})" + await message.reply(text) diff --git a/yaboli/util.py b/yaboli/util.py index 5353ec1..6439799 100644 --- a/yaboli/util.py +++ b/yaboli/util.py @@ -1,6 +1,8 @@ +import datetime import re -__all__ = ["mention", "atmention", "normalize", "similar", "plural"] +__all__ = ["mention", "atmention", "normalize", "similar", "plural", + "format_time", "format_delta"] # Name/nick related functions @@ -28,3 +30,36 @@ def plural( return if_singular else: return if_plural + +def format_time(time: datetime.datetime) -> str: + return time.strftime("%F %T") + +def format_delta(delta: datetime.timedelta) -> str: + seconds = int(delta.total_seconds()) + negative = seconds < 0 + seconds = abs(seconds) + + days = seconds // (60 * 60 * 24) + seconds -= days * (60 * 60 * 24) + + hours = seconds // (60 * 60) + seconds -= hours * (60 * 60) + + minutes = seconds // 60 + seconds -= minutes * 60 + + text: str + + if days > 0: + text = f"{days}d {hours}h {minutes}m {seconds}s" + elif hours > 0: + text = f"{hours}h {minutes}m {seconds}s" + elif minutes > 0: + text = f"{minutes}m {seconds}s" + else: + text = f"{seconds}s" + + if negative: + text = "- " + text + + return text