From d2843fb7ed02a6280e20f4a885bca602906bc255 Mon Sep 17 00:00:00 2001 From: Jacob Henry Date: Thu, 29 Nov 2018 15:30:48 -0500 Subject: [PATCH] Added some aliases. Maybe fixed some shit idk. --- channel_util.py | 2 +- identifier.py | 8 ++++---- job_commands.py | 22 +++++++++++++++++----- main.py | 3 ++- management_commands.py | 5 ++--- scroll_util.py | 2 +- slack_util.py | 22 +++++++++++++++------- slavestothemachine.py | 4 ++-- 8 files changed, 44 insertions(+), 24 deletions(-) diff --git a/channel_util.py b/channel_util.py index fb90b8b..620a2da 100644 --- a/channel_util.py +++ b/channel_util.py @@ -28,4 +28,4 @@ async def channel_check_callback(slack: SlackClient, msg: dict, match: Match) -> channel_check_hook = slack_util.Hook(channel_check_callback, - pattern=r"channel id\s*(.*)") + patterns=r"channel id\s*(.*)") diff --git a/identifier.py b/identifier.py index a20dda0..93d8300 100644 --- a/identifier.py +++ b/identifier.py @@ -138,7 +138,7 @@ def lookup_brother_userids(brother: scroll_util.Brother) -> List[str]: return result -identify_hook = slack_util.Hook(identify_callback, pattern=r"my scroll is (.*)") -identify_other_hook = slack_util.Hook(identify_other_callback, pattern=r"<@(.*)>\s+has scroll\s+(.*)") -check_hook = slack_util.Hook(check_callback, pattern=r"what is my scroll") -name_hook = slack_util.Hook(name_callback, pattern=r"what is my name") +identify_hook = slack_util.Hook(identify_callback, patterns=r"my scroll is (.*)") +identify_other_hook = slack_util.Hook(identify_other_callback, patterns=r"<@(.*)>\s+has scroll\s+(.*)") +check_hook = slack_util.Hook(check_callback, patterns=r"what is my scroll") +name_hook = slack_util.Hook(name_callback, patterns=r"what is my name") diff --git a/job_commands.py b/job_commands.py index 9f9e50e..f828ee0 100644 --- a/job_commands.py +++ b/job_commands.py @@ -314,21 +314,33 @@ async def nag_callback(slack, msg, match): signoff_hook = slack_util.Hook(signoff_callback, - pattern=r"signoff\s+(.*)", + patterns=[ + r"signoff\s+(.*)", + r"sign off\s+(.*)", + ], channel_whitelist=[channel_util.HOUSEJOBS]) late_hook = slack_util.Hook(late_callback, - pattern=r"marklate\s+(.*)", + patterns=[ + r"marklate\s+(.*)", + r"mark late\s+(.*)", + ], channel_whitelist=[channel_util.HOUSEJOBS]) reset_hook = slack_util.Hook(reset_callback, - pattern=r"reset signoffs", + patterns=[ + r"reset signoffs", + r"reset sign offs", + ], channel_whitelist=[channel_util.COMMAND_CENTER_ID]) nag_hook = slack_util.Hook(nag_callback, - pattern=r"nagjobs\s*(.*)", + patterns=[ + r"nagjobs\s+(.*)", + r"nag jobs\s+(.*)" + ], channel_whitelist=[channel_util.COMMAND_CENTER_ID]) reassign_hook = slack_util.Hook(reassign_callback, - pattern=r"reassign\s+(.*?)->\s+(.+)", + patterns=r"reassign\s+(.*?)->\s+(.+)", channel_whitelist=[channel_util.HOUSEJOBS]) diff --git a/main.py b/main.py index 00c8775..f9ea297 100644 --- a/main.py +++ b/main.py @@ -45,7 +45,7 @@ def main() -> None: wrap.add_hook(job_commands.reassign_hook) # Add help - wrap.add_hook(slack_util.Hook(help_callback, pattern=management_commands.bot_help_pattern)) + wrap.add_hook(slack_util.Hook(help_callback, patterns=[r"help", r"bot\s+help"])) # Add boozebot # wrap.add_passive(periodicals.ItsTenPM()) @@ -61,6 +61,7 @@ def main() -> None: event_loop.run_until_complete(both) +# noinspection PyUnusedLocal async def help_callback(slack: SlackClient, msg: dict, match: Match) -> None: slack_util.reply(slack, msg, textwrap.dedent(""" Commands are as follows. Note that some only work in certain channels. diff --git a/management_commands.py b/management_commands.py index d4564e2..d1ca512 100644 --- a/management_commands.py +++ b/management_commands.py @@ -9,7 +9,7 @@ import slack_util def list_hooks_callback_gen(hooks: List[slack_util.Hook]) -> slack_util.Callback: # noinspection PyUnusedLocal async def callback(slack, msg, match): - slack_util.reply(slack, msg, "\n".join(hook.pattern for hook in hooks)) + slack_util.reply(slack, msg, "\n".join(hook.patterns for hook in hooks)) return callback @@ -23,7 +23,6 @@ async def reboot_callback(slack: SlackClient, msg: dict, match: Match) -> None: # Make hooks -bot_help_pattern = r"help" # Can't init this directly, as it relies on us knowing all other hooks. handle in main reboot_hook = slack_util.Hook(reboot_callback, - pattern=r"reboot", + patterns=r"reboot", channel_whitelist=[channel_util.COMMAND_CENTER_ID]) diff --git a/scroll_util.py b/scroll_util.py index c46c9bf..7de85d6 100644 --- a/scroll_util.py +++ b/scroll_util.py @@ -105,4 +105,4 @@ async def find_by_name(name: str, threshold: Optional[float] = None) -> Brother: raise BrotherNotFound(msg) -scroll_hook = slack_util.Hook(scroll_callback, pattern=r"scroll\s+(.*)") +scroll_hook = slack_util.Hook(scroll_callback, patterns=r"scroll\s+(.*)") diff --git a/slack_util.py b/slack_util.py index d3e2f2e..e27b710 100644 --- a/slack_util.py +++ b/slack_util.py @@ -1,7 +1,6 @@ import re -import typing from time import sleep, time -from typing import Any, Optional, Generator, Match, Callable, List, Coroutine +from typing import Any, Optional, Generator, Match, Callable, List, Coroutine, Union, TypeVar, Awaitable from slackclient import SlackClient from slackclient.client import SlackNotConnected @@ -68,7 +67,7 @@ def message_stream(slack: SlackClient) -> Generator[dict, None, None]: print("Connection failed - retrying") -T = typing.TypeVar("T") +T = TypeVar("T") class VerboseWrapper(Callable): @@ -80,7 +79,7 @@ class VerboseWrapper(Callable): self.slack = slack self.command_msg = command_msg - async def __call__(self, awt: typing.Awaitable[T]) -> T: + async def __call__(self, awt: Awaitable[T]) -> T: try: return await awt except Exception as e: @@ -108,14 +107,17 @@ class AbsHook(object): class Hook(AbsHook): def __init__(self, callback: Callback, - pattern: str, + patterns: Union[str, List[str]], channel_whitelist: Optional[List[str]] = None, channel_blacklist: Optional[List[str]] = None, consumer: bool = True): super(Hook, self).__init__(consumer) # Save all - self.pattern = pattern + if not isinstance(patterns, list): + patterns = [patterns] + + self.patterns = patterns self.channel_whitelist = channel_whitelist self.channel_blacklist = channel_blacklist self.callback = callback @@ -134,7 +136,12 @@ class Hook(AbsHook): Returns whether a message should be handled by this dict, returning a Match if so, or None """ # Fail if pattern invalid - match = re.match(self.pattern, msg['text'], flags=re.IGNORECASE) + match = None + for p in self.patterns: + match = re.match(p, msg['text'], flags=re.IGNORECASE) + if match is not None: + break + if match is None: return None @@ -170,6 +177,7 @@ class ReplyWaiter(AbsHook): # If so, give up the ghost if self.dead or should_expire: + print("Reply waiter has expired after {} seconds".format(time_alive)) raise DeadHook() # Otherwise proceed normally diff --git a/slavestothemachine.py b/slavestothemachine.py index 1dc4ca3..11af477 100644 --- a/slavestothemachine.py +++ b/slavestothemachine.py @@ -93,8 +93,8 @@ async def dump_work_callback(slack: SlackClient, msg: dict, match: Match) -> Non # Make dem HOOKs count_work_hook = slack_util.Hook(count_work_callback, - pattern=".*", + patterns=".*", channel_whitelist=[channel_util.SLAVES_TO_THE_MACHINE_ID]) dump_work_hook = slack_util.Hook(dump_work_callback, - pattern="dump towel data", + patterns="dump towel data", channel_whitelist=[channel_util.COMMAND_CENTER_ID])