Make bot console less verbose by default
This commit is contained in:
parent
172b8d5dac
commit
9749796a95
|
|
@ -26,6 +26,6 @@ repos:
|
||||||
- id: add-trailing-comma
|
- id: add-trailing-comma
|
||||||
args: [--py36-plus]
|
args: [--py36-plus]
|
||||||
- repo: https://github.com/pre-commit/mirrors-mypy
|
- repo: https://github.com/pre-commit/mirrors-mypy
|
||||||
rev: v0.740
|
rev: v0.750
|
||||||
hooks:
|
hooks:
|
||||||
- id: mypy
|
- id: mypy
|
||||||
|
|
|
||||||
39
bot.py
39
bot.py
|
|
@ -22,6 +22,10 @@ import aiosqlite
|
||||||
HOST = 'irc.chat.twitch.tv'
|
HOST = 'irc.chat.twitch.tv'
|
||||||
PORT = 6697
|
PORT = 6697
|
||||||
|
|
||||||
|
MSG_RE = re.compile('^:([^!]+).* PRIVMSG #[^ ]+ :([^\r]+)')
|
||||||
|
PRIVMSG = 'PRIVMSG #{channel} :{msg}\r\n'
|
||||||
|
SEND_MSG_RE = re.compile('^PRIVMSG #[^ ]+ :(?P<msg>[^\r]+)')
|
||||||
|
|
||||||
|
|
||||||
class Config(NamedTuple):
|
class Config(NamedTuple):
|
||||||
username: str
|
username: str
|
||||||
|
|
@ -82,9 +86,6 @@ class CmdResponse(Response):
|
||||||
return self.cmd
|
return self.cmd
|
||||||
|
|
||||||
|
|
||||||
PRIVMSG = 'PRIVMSG #{channel} :{msg}\r\n'
|
|
||||||
|
|
||||||
|
|
||||||
class MessageResponse(Response):
|
class MessageResponse(Response):
|
||||||
def __init__(self, match: Match[str], msg_fmt: str) -> None:
|
def __init__(self, match: Match[str], msg_fmt: str) -> None:
|
||||||
self.match = match
|
self.match = match
|
||||||
|
|
@ -336,23 +337,26 @@ def msg_gnu_please(match: Match[str]) -> Response:
|
||||||
|
|
||||||
# TODO: !tags, only allowed by stream admin / mods????
|
# TODO: !tags, only allowed by stream admin / mods????
|
||||||
|
|
||||||
|
def dt_str() -> str:
|
||||||
@handler('.*')
|
dt_now = datetime.datetime.now()
|
||||||
def unhandled(match: Match[str]) -> Response:
|
return f'[{dt_now.hour}:{dt_now.minute}]'
|
||||||
print(f'UNHANDLED: {match.group()}', end='')
|
|
||||||
return Response()
|
|
||||||
|
|
||||||
|
|
||||||
async def amain(config: Config) -> NoReturn:
|
async def amain(config: Config, *, quiet: bool) -> NoReturn:
|
||||||
reader, writer = await asyncio.open_connection(HOST, PORT, ssl=True)
|
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'PASS {config.oauth_token}\r\n', quiet=True)
|
||||||
await send(writer, f'NICK {config.username}\r\n')
|
await send(writer, f'NICK {config.username}\r\n', quiet=quiet)
|
||||||
await send(writer, f'JOIN #{config.channel}\r\n')
|
await send(writer, f'JOIN #{config.channel}\r\n', quiet=quiet)
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
data = await recv(reader)
|
data = await recv(reader, quiet=quiet)
|
||||||
msg = data.decode('UTF-8', errors='backslashreplace')
|
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:
|
for pattern, handler in HANDLERS:
|
||||||
match = pattern.match(msg)
|
match = pattern.match(msg)
|
||||||
if match:
|
if match:
|
||||||
|
|
@ -365,19 +369,26 @@ async def amain(config: Config) -> NoReturn:
|
||||||
msg=f'*** unhandled {type(e).__name__} -- see logs',
|
msg=f'*** unhandled {type(e).__name__} -- see logs',
|
||||||
)
|
)
|
||||||
if res is not None:
|
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
|
break
|
||||||
|
else:
|
||||||
|
if not quiet:
|
||||||
|
print(f'UNHANDLED: {msg}', end='')
|
||||||
|
|
||||||
|
|
||||||
def main() -> int:
|
def main() -> int:
|
||||||
parser = argparse.ArgumentParser()
|
parser = argparse.ArgumentParser()
|
||||||
parser.add_argument('--config', default='config.json')
|
parser.add_argument('--config', default='config.json')
|
||||||
|
parser.add_argument('--verbose', action='store_true')
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
|
|
||||||
with open(args.config) as f:
|
with open(args.config) as f:
|
||||||
config = Config(**json.load(f))
|
config = Config(**json.load(f))
|
||||||
|
|
||||||
asyncio.run(amain(config))
|
asyncio.run(amain(config, quiet=not args.verbose))
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue