Implement !uptime
This commit is contained in:
parent
14b4e74c7e
commit
a0f7c8e84a
3 changed files with 57 additions and 3 deletions
|
|
@ -49,9 +49,9 @@ i. e. the text between the end of the "!echo" and the end of the whole message.
|
||||||
|
|
||||||
## TODOs
|
## TODOs
|
||||||
|
|
||||||
- [ ] implement !uptime for proper botrulez conformity
|
|
||||||
- [ ] implement !kill
|
- [ ] implement !kill
|
||||||
- [ ] implement !restart and add an easier way to run bots
|
- [ ] implement !restart and add an easier way to run bots
|
||||||
|
- [ ] untruncate LiveMessage-s
|
||||||
- [ ] config file support for bots, used by default
|
- [ ] config file support for bots, used by default
|
||||||
- [ ] package in a distutils-compatible way (users should be able to install
|
- [ ] package in a distutils-compatible way (users should be able to install
|
||||||
yaboli using `pip install git+https://github.com/Garmelon/yaboli`)
|
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)
|
- [ ] document new classes (docstrings, maybe comments)
|
||||||
- [ ] write project readme
|
- [ ] write project readme
|
||||||
- [ ] write examples
|
- [ ] write examples
|
||||||
|
- [x] implement !uptime for proper botrulez conformity
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,4 @@
|
||||||
|
import datetime
|
||||||
import logging
|
import logging
|
||||||
from typing import List, Optional
|
from typing import List, Optional
|
||||||
|
|
||||||
|
|
@ -5,6 +6,7 @@ from .client import Client
|
||||||
from .command import *
|
from .command import *
|
||||||
from .message import LiveMessage, Message
|
from .message import LiveMessage, Message
|
||||||
from .room import Room
|
from .room import Room
|
||||||
|
from .util import *
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
@ -20,6 +22,8 @@ class Bot(Client):
|
||||||
|
|
||||||
self._commands: List[Command] = []
|
self._commands: List[Command] = []
|
||||||
|
|
||||||
|
self.start_time = datetime.datetime.now()
|
||||||
|
|
||||||
# Registering commands
|
# Registering commands
|
||||||
|
|
||||||
def register(self, command: Command) -> None:
|
def register(self, command: Command) -> None:
|
||||||
|
|
@ -74,7 +78,8 @@ class Bot(Client):
|
||||||
|
|
||||||
def register_botrulez(self,
|
def register_botrulez(self,
|
||||||
ping: bool = True,
|
ping: bool = True,
|
||||||
help_: bool = True
|
help_: bool = True,
|
||||||
|
uptime: bool = True
|
||||||
) -> None:
|
) -> None:
|
||||||
if ping:
|
if ping:
|
||||||
self.register_general("ping", self.cmd_ping, args=False)
|
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_general("help", self.cmd_help_general, args=False)
|
||||||
self.register_specific("help", self.cmd_help_specific, 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,
|
async def cmd_ping(self,
|
||||||
room: Room,
|
room: Room,
|
||||||
message: LiveMessage,
|
message: LiveMessage,
|
||||||
|
|
@ -109,3 +117,13 @@ class Bot(Client):
|
||||||
) -> None:
|
) -> None:
|
||||||
if self.HELP_SPECIFIC is not None:
|
if self.HELP_SPECIFIC is not None:
|
||||||
await message.reply(self.format_help(room, self.HELP_SPECIFIC))
|
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)
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,8 @@
|
||||||
|
import datetime
|
||||||
import re
|
import re
|
||||||
|
|
||||||
__all__ = ["mention", "atmention", "normalize", "similar", "plural"]
|
__all__ = ["mention", "atmention", "normalize", "similar", "plural",
|
||||||
|
"format_time", "format_delta"]
|
||||||
|
|
||||||
# Name/nick related functions
|
# Name/nick related functions
|
||||||
|
|
||||||
|
|
@ -28,3 +30,36 @@ def plural(
|
||||||
return if_singular
|
return if_singular
|
||||||
else:
|
else:
|
||||||
return if_plural
|
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
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue