[somebot.] /dsaroll
author@top_left <gisilfrid@protonmail.com>
Wed, 8 Dec 2021 18:38:38 +0000 (19:38 +0100)
committer@top_left <gisilfrid@protonmail.com>
Wed, 8 Dec 2021 18:38:38 +0000 (19:38 +0100)
modules/CommandDSARollDice.py [new file with mode: 0644]

diff --git a/modules/CommandDSARollDice.py b/modules/CommandDSARollDice.py
new file mode 100644 (file)
index 0000000..160f31a
--- /dev/null
@@ -0,0 +1,59 @@
+# Mattermost Bot module.
+#  Copyright (c) 2021 by @top_left <gisilfrid@protonmail.com>
+#  published under MIT-License
+
+from random import randint
+import re
+
+
+from AbstractCommand import AbstractCommand
+class CommandDSARollDice(AbstractCommand):
+    TRIGGER = "dsaroll"
+    CONFIG = {"display_name": "somebot-command", "auto_complete": True,
+              "auto_complete_hint": "[n(d|w)](6|20)[(+|-)b]",
+             }
+    USEINFO = CONFIG["auto_complete_desc"] = CONFIG["description"] = "Roll a 6 or 20 sided die, optionally apply modifications according to the DSA rules."
+
+
+    def on_POST(self, request, data):
+        if re.search(r"^(6|20)\s?(\+\d{1,3}|-\d{1,3})?$", data["text"]):
+            groups = re.findall(r"^(6|20)\s?(\+\d{1,3}|-\d{1,3})?$", data["text"])[0]
+            die = int(groups[0])
+            msg = "Rolling a d{}".format(die)
+            modification = 0
+            if groups[1] == "":
+                msg += ":\n"
+            else:
+                modification = int(groups[1])
+                msg += " modified by {}:\n".format(modification)
+            if groups[0] == "20":
+                modification = -modification
+            msg += str(randint(1, die) + modification)
+        elif re.search(r"^(\d{1,2})[d|w](6|20)\s?(\+\d{1,3}|-\d{1,3})?$", data["text"]):
+            groups = re.findall(r"^(\d{1,2})[d|w](6|20)\s?(\+\d{1,3}|-\d{1,3})?$", data["text"])[0]
+            times = int(groups[0])
+            die = int(groups[1])
+            msg = "Rolling a d{} {} times".format(die, times)
+            modification = 0
+            if groups[2] == "":
+                msg += ":\n"
+            else:
+                modification = int(groups[2])
+                msg += " modified by {}:\n".format(modification)
+            if die == 20:
+                for _ in range(times):
+                    msg += str(randint(1, die) - modification) + ", "
+                msg = msg[:-2]
+            else:
+                # die should be 6 sided
+                dice_sum = modification
+                for _ in range(times):
+                    dice_sum += randint(1, die)
+                msg += str(dice_sum)
+        else:
+            request.cmd_respond_text_temp(
+                "Possible invocations are either m[+b] or ndm[+b] where n is between 1 and 99, b can be any integer betwenn -999 and 999, and m is either 6 or 20"
+            )
+            return
+
+        request.cmd_respond_text_chan(msg)