Browse Source

Fix bug in arg min annotations and make choice have delimiter tiers

Kirk Trombley 4 years ago
parent
commit
2d63f3b662
2 changed files with 15 additions and 7 deletions
  1. 4 4
      lib/rollbot/plugins/decorators/requires.py
  2. 11 3
      src/plugins/simple.py

+ 4 - 4
lib/rollbot/plugins/decorators/requires.py

@@ -8,8 +8,8 @@ def require_min_args(n, alert_response=None):
         old_on_command = cls.on_command
         @wraps(old_on_command)
         def wrapper(self, db, message):
-            if len(message.arg_list()) < n:
-                failure = RollbotFailure.INVALID_ARGUMENTS.with_reason(alert_response or f"{cls.command.title()} requires at least {n} argument(s)")
+            if message.raw_args is None or len(message.arg_list()) < n:
+                failure = RollbotFailure.INVALID_ARGUMENTS.with_reason(alert_response or f"{self.command.title()} requires at least {n} argument(s)")
                 return RollbotResponse(message, failure=failure, debugging=failure.get_debugging())
             return old_on_command(self, db, message)
         setattr(cls, "on_command", wrapper)
@@ -23,8 +23,8 @@ def require_args(n, alert_response=None):
         old_on_command = cls.on_command
         @wraps(old_on_command)
         def wrapper(self, db, message):
-            if len(message.arg_list()) < n:
-                failure = RollbotFailure.INVALID_ARGUMENTS.with_reason(alert_response or f"{cls.command.title()} requires exactly {n} argument(s)")
+            if message.raw_args is None or len(message.arg_list()) != n:
+                failure = RollbotFailure.INVALID_ARGUMENTS.with_reason(alert_response or f"{self.command.title()} requires exactly {n} argument(s)")
                 return RollbotResponse(message, failure=failure, debugging=failure.get_debugging())
             return old_on_command(self, db, message)
         setattr(cls, "on_command", wrapper)

+ 11 - 3
src/plugins/simple.py

@@ -1,6 +1,8 @@
 import random
 
 from rollbot import as_plugin
+from rollbot.plugins.decorators import require_min_args
+from rollbot.plugins.injection import ArgString, ArgList
 
 
 @as_plugin
@@ -36,10 +38,16 @@ def console(message):
     return random.choice(opts)
 
 
+@require_min_args(1)
 @as_plugin
-def choice(message):
-    opts = list(message.args())
-    chosen = random.choice(opts)
+def choice(args: ArgString, arg_list: ArgList):
+    for delim in ("|", ";", ","):
+        if delim in args:
+            opts = args.split(delim)
+            break
+    else:
+        opts = arg_list
+    chosen = random.choice(opts).strip()
     return f"Randomly chose {chosen} from {len(opts)} options"