1 # Mattermost Bot module.
2 # Copyright (c) 2021 by @top_left <gisilfrid@protonmail.com>
3 # published under MIT-License
5 from random import randint
9 # pylint: disable=wrong-import-position
10 from AbstractCommand import AbstractCommand
11 class CommandDSARollDice(AbstractCommand):
13 CONFIG = {"display_name": "somebot-command", "auto_complete": True,
14 "auto_complete_hint": "[n(d|w)](6|20)[(+|-)b]",
16 USEINFO = CONFIG["auto_complete_desc"] = CONFIG["description"] = "Roll a 6 or 20 sided die, optionally apply modifications according to the DSA rules."
19 def on_POST(self, request, data):
20 if re.search(r"^(6|20)\s?(\+\d{1,3}|-\d{1,3})?$", data["text"]):
21 groups = re.findall(r"^(6|20)\s?(\+\d{1,3}|-\d{1,3})?$", data["text"])[0]
23 msg = "Rolling a d{}".format(die)
28 modification = int(groups[1])
29 msg += " modified by {}:\n".format(modification)
31 modification = -modification
32 msg += str(randint(1, die) + modification)
33 elif re.search(r"^(\d{1,2})[d|w](6|20)\s?(\+\d{1,3}|-\d{1,3})?$", data["text"]):
34 groups = re.findall(r"^(\d{1,2})[d|w](6|20)\s?(\+\d{1,3}|-\d{1,3})?$", data["text"])[0]
35 times = int(groups[0])
37 msg = "Rolling a d{} {} times".format(die, times)
42 modification = int(groups[2])
43 msg += " modified by {}:\n".format(modification)
45 for _ in range(times):
46 msg += str(randint(1, die) - modification) + ", "
49 # die should be 6 sided
50 dice_sum = modification
51 for _ in range(times):
52 dice_sum += randint(1, die)
55 request.cmd_respond_text_temp(
56 "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"
60 request.respond_cmd_chan(msg)