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 from AbstractCommand import AbstractCommand
10 class CommandDSARollDice(AbstractCommand):
12 CONFIG = {"display_name": "somebot-command", "auto_complete": True,
13 "auto_complete_hint": "[n(d|w)](6|20)[(+|-)b]",
15 USEINFO = CONFIG["auto_complete_desc"] = CONFIG["description"] = "Roll a 6 or 20 sided die, optionally apply modifications according to the DSA rules."
18 def on_POST(self, request, data):
19 if re.search(r"^(6|20)\s?(\+\d{1,3}|-\d{1,3})?$", data["text"]):
20 groups = re.findall(r"^(6|20)\s?(\+\d{1,3}|-\d{1,3})?$", data["text"])[0]
22 msg = "Rolling a d{}".format(die)
27 modification = int(groups[1])
28 msg += " modified by {}:\n".format(modification)
30 modification = -modification
31 msg += str(randint(1, die) + modification)
32 elif re.search(r"^(\d{1,2})[d|w](6|20)\s?(\+\d{1,3}|-\d{1,3})?$", data["text"]):
33 groups = re.findall(r"^(\d{1,2})[d|w](6|20)\s?(\+\d{1,3}|-\d{1,3})?$", data["text"])[0]
34 times = int(groups[0])
36 msg = "Rolling a d{} {} times".format(die, times)
41 modification = int(groups[2])
42 msg += " modified by {}:\n".format(modification)
44 for _ in range(times):
45 msg += str(randint(1, die) - modification) + ", "
48 # die should be 6 sided
49 dice_sum = modification
50 for _ in range(times):
51 dice_sum += randint(1, die)
54 request.cmd_respond_text_temp(
55 "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"
59 request.cmd_respond_text_chan(msg)