diff options
author | Jan200101 <sentrycraft123@gmail.com> | 2022-09-09 13:51:52 +0200 |
---|---|---|
committer | Jan200101 <sentrycraft123@gmail.com> | 2022-09-09 13:51:52 +0200 |
commit | 86dd3902f78f470d4e571213fb126119c079b1c6 (patch) | |
tree | e951e200341b59b3869a18542900a50322ed5e57 /labbot/commands.py | |
parent | 78f3ee84bcb1164b0260325d42fe7ef96c034ed0 (diff) | |
download | lab-bot-86dd3902f78f470d4e571213fb126119c079b1c6.tar.gz lab-bot-86dd3902f78f470d4e571213fb126119c079b1c6.zip |
add command framework, correct access token and secret passing
Diffstat (limited to 'labbot/commands.py')
-rw-r--r-- | labbot/commands.py | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/labbot/commands.py b/labbot/commands.py new file mode 100644 index 0000000..10b6325 --- /dev/null +++ b/labbot/commands.py @@ -0,0 +1,58 @@ +""" +Command Framework for Lab-Bot +Allows the creation of functions that run when specific comments are made +e.g. +!close -> closes issue +""" + +import logging + +log = logging.getLogger(__name__) + +class Commands: + + def __init__(self, **kwargs): + self.prefix = kwargs.get("prefix", "!") + self.hooks = {} + + def command(self, *args, name=None, aliases=None): + def decorator(func): + nonlocal name + nonlocal aliases + + if name is None: + name = func.__name__ + + if aliases is None: + aliases = [] + + aliases.append(name) + + for alias in aliases: + self.hooks[alias] = func + + return decorator + + async def process_note(self, event, gl, *args, **kwargs): + note = event.object_attributes.get("note", "").strip() + if not note.startswith(self.prefix): + return + + note = note[len(self.prefix):] + + note = note.split() + command = note[0] + arguments = note[1:] + + hook = None + try: + hook = self.hooks[command] + except KeyError: + log.warn(f"Attempted to invoke nonexistant command `{command}`") + + if hook: + await hook(event, gl, *arguments) + def setup_hook(self, bot): + bot.register_note_hook(self.process_note) + +commands = Commands() |