From 9749796a95fa9789bc843ef48d362b34fc31912b Mon Sep 17 00:00:00 2001 From: Anthony Sottile Date: Sun, 8 Dec 2019 20:28:39 -0800 Subject: [PATCH] Make bot console less verbose by default --- .pre-commit-config.yaml | 2 +- bot.py | 39 +++++++++++++++++++++++++-------------- 2 files changed, 26 insertions(+), 15 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 26a61f3..28b633d 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -26,6 +26,6 @@ repos: - id: add-trailing-comma args: [--py36-plus] - repo: https://github.com/pre-commit/mirrors-mypy - rev: v0.740 + rev: v0.750 hooks: - id: mypy diff --git a/bot.py b/bot.py index 4ed32b2..f079c08 100644 --- a/bot.py +++ b/bot.py @@ -22,6 +22,10 @@ import aiosqlite HOST = 'irc.chat.twitch.tv' PORT = 6697 +MSG_RE = re.compile('^:([^!]+).* PRIVMSG #[^ ]+ :([^\r]+)') +PRIVMSG = 'PRIVMSG #{channel} :{msg}\r\n' +SEND_MSG_RE = re.compile('^PRIVMSG #[^ ]+ :(?P[^\r]+)') + class Config(NamedTuple): username: str @@ -82,9 +86,6 @@ class CmdResponse(Response): return self.cmd -PRIVMSG = 'PRIVMSG #{channel} :{msg}\r\n' - - class MessageResponse(Response): def __init__(self, match: Match[str], msg_fmt: str) -> None: self.match = match @@ -336,23 +337,26 @@ def msg_gnu_please(match: Match[str]) -> Response: # TODO: !tags, only allowed by stream admin / mods???? - -@handler('.*') -def unhandled(match: Match[str]) -> Response: - print(f'UNHANDLED: {match.group()}', end='') - return Response() +def dt_str() -> str: + dt_now = datetime.datetime.now() + return f'[{dt_now.hour}:{dt_now.minute}]' -async def amain(config: Config) -> NoReturn: +async def amain(config: Config, *, quiet: bool) -> NoReturn: reader, writer = await asyncio.open_connection(HOST, PORT, ssl=True) await send(writer, f'PASS {config.oauth_token}\r\n', quiet=True) - await send(writer, f'NICK {config.username}\r\n') - await send(writer, f'JOIN #{config.channel}\r\n') + await send(writer, f'NICK {config.username}\r\n', quiet=quiet) + await send(writer, f'JOIN #{config.channel}\r\n', quiet=quiet) while True: - data = await recv(reader) + data = await recv(reader, quiet=quiet) msg = data.decode('UTF-8', errors='backslashreplace') + + msg_match = MSG_RE.match(msg) + if msg_match: + print(f'{dt_str()}<{msg_match[1]}> {msg_match[2]}') + for pattern, handler in HANDLERS: match = pattern.match(msg) if match: @@ -365,19 +369,26 @@ async def amain(config: Config) -> NoReturn: msg=f'*** unhandled {type(e).__name__} -- see logs', ) if res is not None: - await send(writer, res) + send_match = SEND_MSG_RE.match(res) + if send_match: + print(f'{dt_str()}<{config.username}> {send_match[1]}') + await send(writer, res, quiet=quiet) break + else: + if not quiet: + print(f'UNHANDLED: {msg}', end='') def main() -> int: parser = argparse.ArgumentParser() parser.add_argument('--config', default='config.json') + parser.add_argument('--verbose', action='store_true') args = parser.parse_args() with open(args.config) as f: config = Config(**json.load(f)) - asyncio.run(amain(config)) + asyncio.run(amain(config, quiet=not args.verbose)) return 0