]> git.somenet.org - pub/jan/mattermost-bot.git/blob - modules/CommandDSARollDice.py
README.md
[pub/jan/mattermost-bot.git] / modules / CommandDSARollDice.py
1 # Mattermost Bot module.
2 #  Copyright (c) 2021 by @top_left <gisilfrid@protonmail.com>
3 #  published under MIT-License
4
5 from random import randint
6 import re
7
8
9 from AbstractCommand import AbstractCommand
10 class CommandDSARollDice(AbstractCommand):
11     TRIGGER = "dsaroll"
12     CONFIG = {"display_name": "somebot-command", "auto_complete": True,
13               "auto_complete_hint": "[n(d|w)](6|20)[(+|-)b]",
14              }
15     USEINFO = CONFIG["auto_complete_desc"] = CONFIG["description"] = "Roll a 6 or 20 sided die, optionally apply modifications according to the DSA rules."
16
17
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]
21             die = int(groups[0])
22             msg = "Rolling a d{}".format(die)
23             modification = 0
24             if groups[1] == "":
25                 msg += ":\n"
26             else:
27                 modification = int(groups[1])
28                 msg += " modified by {}:\n".format(modification)
29             if groups[0] == "20":
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])
35             die = int(groups[1])
36             msg = "Rolling a d{} {} times".format(die, times)
37             modification = 0
38             if groups[2] == "":
39                 msg += ":\n"
40             else:
41                 modification = int(groups[2])
42                 msg += " modified by {}:\n".format(modification)
43             if die == 20:
44                 for _ in range(times):
45                     msg += str(randint(1, die) - modification) + ", "
46                 msg = msg[:-2]
47             else:
48                 # die should be 6 sided
49                 dice_sum = modification
50                 for _ in range(times):
51                     dice_sum += randint(1, die)
52                 msg += str(dice_sum)
53         else:
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"
56             )
57             return
58
59         request.cmd_respond_text_chan(msg)