]> git.somenet.org - pub/jan/mattermost-bot.git/blob - modules/CommandDSARollDice.py
modules/CommandDrinkBindAccount.py
[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 # pylint: disable=wrong-import-position
10 from AbstractCommand import AbstractCommand
11 class CommandDSARollDice(AbstractCommand):
12     TRIGGER = "dsaroll"
13     CONFIG = {"display_name": "somebot-command", "auto_complete": True,
14               "auto_complete_hint": "[n(d|w)](6|20)[(+|-)b]",
15              }
16     USEINFO = CONFIG["auto_complete_desc"] = CONFIG["description"] = "Roll a 6 or 20 sided die, optionally apply modifications according to the DSA rules."
17
18
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]
22             die = int(groups[0])
23             msg = "Rolling a d{}".format(die)
24             modification = 0
25             if groups[1] == "":
26                 msg += ":\n"
27             else:
28                 modification = int(groups[1])
29                 msg += " modified by {}:\n".format(modification)
30             if groups[0] == "20":
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])
36             die = int(groups[1])
37             msg = "Rolling a d{} {} times".format(die, times)
38             modification = 0
39             if groups[2] == "":
40                 msg += ":\n"
41             else:
42                 modification = int(groups[2])
43                 msg += " modified by {}:\n".format(modification)
44             if die == 20:
45                 for _ in range(times):
46                     msg += str(randint(1, die) - modification) + ", "
47                 msg = msg[:-2]
48             else:
49                 # die should be 6 sided
50                 dice_sum = modification
51                 for _ in range(times):
52                     dice_sum += randint(1, die)
53                 msg += str(dice_sum)
54         else:
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"
57             )
58             return
59
60         request.cmd_respond_text_chan(msg)