From 81ad3271006f60d34e9a2068611c47ce63081e7d Mon Sep 17 00:00:00 2001 From: "@top_left" Date: Wed, 8 Dec 2021 19:38:38 +0100 Subject: [PATCH] [somebot.] /dsaroll --- modules/CommandDSARollDice.py | 59 +++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 modules/CommandDSARollDice.py diff --git a/modules/CommandDSARollDice.py b/modules/CommandDSARollDice.py new file mode 100644 index 0000000..160f31a --- /dev/null +++ b/modules/CommandDSARollDice.py @@ -0,0 +1,59 @@ +# Mattermost Bot module. +# Copyright (c) 2021 by @top_left +# 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) -- 2.43.0