Browse Source

First pass at subcommand injection, with an example in watchlist

Kirk Trombley 5 years ago
parent
commit
e9dd1609f0
3 changed files with 24 additions and 7 deletions
  1. 15 0
      lib/rollbot/messaging.py
  2. 3 1
      lib/rollbot/plugins.py
  3. 6 6
      src/plugins/watchlist.py

+ 15 - 0
lib/rollbot/messaging.py

@@ -33,6 +33,21 @@ class RollbotMessage:
                 self.command = cmd.lower()
                 self.raw_args = raw
 
+    @staticmethod
+    def from_subcommand(msg):
+        subc = RollbotMessage(
+            msg.src,
+            msg.name,
+            msg.sender_id,
+            msg.group_id,
+            msg.message_id,
+            msg.raw_args, # this strips the command of the old message
+            msg.from_admin
+        )
+        if subc.is_command:
+            return subc
+        # silently return None if a subcommand could not be made
+
     @staticmethod
     def from_groupme(msg, global_admins=(), group_admins={}):
         sender_id = msg["sender_id"]

+ 3 - 1
lib/rollbot/plugins.py

@@ -2,7 +2,7 @@ import logging
 import inspect
 from functools import reduce
 
-from .messaging import RollbotResponse
+from .messaging import RollbotResponse, RollbotMessage
 
 
 class RollbotPlugin:
@@ -50,6 +50,8 @@ def as_plugin(command):
                 converters.append(lambda cmd, db, msg: cmd.logger)
             elif p in ("bot", "rollbot"):
                 converters.append(lambda cmd, db, msg: cmd.bot)
+            elif p in ("subc", "subcommand"):
+                converters.append(lambda cmd, db, msg: RollbotMessage.from_subcommand(msg))
             elif p.startswith("data") or p.endswith("data") or p in ("group_singleton", "singleton"):
                 annot = fn.__annotations__.get(p, p)
                 converters.append(lambda cmd, db, msg, sing_cls=annot: sing_cls.get_or_create(db, msg.group_id))

+ 6 - 6
src/plugins/watchlist.py

@@ -79,12 +79,12 @@ subcommands = {
 
 
 @as_plugin
-def watchlist(msg, data: Watchlist):
-    if msg.raw_args is not None and msg.raw_args.startswith("!"):
-        subc, args = pop_arg(msg.raw_args)
-        subc = subc[1:].lower()
-        subc_fn = subcommands.get(subc, lambda *_: f"Sorry, subcommand must be one of {', '.join(subcommands)}, you used: {subc}")
-        return subc_fn(args, data)
+def watchlist(subc, data: Watchlist):
+    if subc:
+        subc_fn = subcommands.get(subc.command, None)
+        if subc_fn:
+            return subc_fn(subc.raw_args, data)
+        return f"Sorry, subcommand must be one of {', '.join(subcommands)}, you used: {subc.command}"
     return subcommands["help"]() # default to help message
 
 # some quick aliases for convenience, !wl is aliased in the config file instead