diff --git a/README.md b/README.md index ee4dddc..70ad6a7 100644 --- a/README.md +++ b/README.md @@ -21,7 +21,7 @@ class EchoBot(yaboli.Bot): def __init__(self, config_file): super().__init__(config_file) - self.register_botrulez() + self.register_botrulez(kill=True) self.register_general("echo", self.cmd_echo) async def cmd_echo(self, room, message, args): @@ -33,7 +33,9 @@ The bot's nick and default rooms are specified in a config file. The help command from the botrulez uses the `HELP_GENERAL` and `HELP_SPECIFIC` fields. -In the `__init__` function, the bot's commands are registered. +In the `__init__` function, the bot's commands are registered. The required +botrulez commands (!ping, !help, !uptime) are enabled by default. Other +commands like !kill need to be enabled explicitly. In the `cmd_echo` function, the echo command is implemented. In this case, the bot replies to the message containing the command with the raw argument string, @@ -41,11 +43,10 @@ i. e. the text between the end of the "!echo" and the end of the whole message. ## TODOs -- [ ] implement !restart and add an easier way to run bots +- [ ] implement !restart - [ ] package in a distutils-compatible way (users should be able to install yaboli using `pip install git+https://github.com/Garmelon/yaboli`) - [ ] document yaboli (markdown files in a "docs" folder?) -- [ ] make it easier to enable log messages - [ ] cookie support - [ ] fancy argument parsing - [ ] document new classes (docstrings, maybe comments) @@ -55,3 +56,5 @@ i. e. the text between the end of the "!echo" and the end of the whole message. - [x] implement !kill - [x] untruncate LiveMessage-s - [x] config file support for bots, used by default +- [x] make it easier to enable log messages +- [x] make it easier to run bots diff --git a/examples/echo/echobot.py b/examples/echo/echobot.py index 7f5b103..4804992 100644 --- a/examples/echo/echobot.py +++ b/examples/echo/echobot.py @@ -1,5 +1,3 @@ -import asyncio - import yaboli @@ -12,15 +10,13 @@ class EchoBot(yaboli.Bot): def __init__(self, config_file): super().__init__(config_file) - self.register_botrulez() + self.register_botrulez(kill=True) self.register_general("echo", self.cmd_echo) async def cmd_echo(self, room, message, args): await message.reply(args.raw) -async def main(): - bot = EchoBot("bot.conf") - await bot.run() if __name__ == "__main__": - asyncio.run(main()) + yaboli.enable_logging() + yaboli.run(EchoBot) diff --git a/yaboli/__init__.py b/yaboli/__init__.py index e749ce5..553b8d7 100644 --- a/yaboli/__init__.py +++ b/yaboli/__init__.py @@ -1,4 +1,6 @@ -from typing import List +import asyncio +import logging +from typing import Callable from .bot import * from .client import * @@ -12,7 +14,9 @@ from .room import * from .session import * from .util import * -__all__: List[str] = [] +__all__ = ["STYLE", "FORMAT", "DATE_FORMAT", "FORMATTER", "enable_logging", + "run"] + __all__ += bot.__all__ __all__ += client.__all__ __all__ += command.__all__ @@ -24,3 +28,31 @@ __all__ += module.__all__ __all__ += room.__all__ __all__ += session.__all__ __all__ += util.__all__ + +STYLE = "{" +FORMAT = "{asctime} [{levelname:<7}] <{name}>: {message}" +DATE_FORMAT = "%F %T" + +FORMATTER = logging.Formatter( + fmt=FORMAT, + datefmt=DATE_FORMAT, + style=STYLE +) + +def enable_logging(name: str = "yaboli", level: int = logging.INFO) -> None: + handler = logging.StreamHandler() + handler.setFormatter(FORMATTER) + + logger = logging.getLogger(name) + logger.setLevel(level) + logger.addHandler(handler) + +def run( + client: Callable[[str], Client], + config_file: str = "bot.conf" + ) -> None: + async def _run(): + client_ = client(config_file) + await client_.run() + + asyncio.run(_run()) diff --git a/yaboli/bot.py b/yaboli/bot.py index 3ca7171..1777498 100644 --- a/yaboli/bot.py +++ b/yaboli/bot.py @@ -152,5 +152,6 @@ class Bot(Client): message: LiveMessage, args: SpecificArgumentData ) -> None: + logger.info(f"Killed in &{room.name} by {message.sender.atmention}") await message.reply(self.KILL_REPLY) await self.part(room)