]> git.somenet.org - pub/jan/mattermost-bot.git/blob - modules/CommandRollDice.py
[somebot] DialogManagedReport + /report
[pub/jan/mattermost-bot.git] / modules / CommandRollDice.py
1 # Mattermost APIv4 Bot.
2 # written 2019 by Ju <daju@fsinf.at>
3 # essential feedback by frunobulax
4
5 from random import randint
6 import re
7
8
9 from AbstractCommand import AbstractCommand
10 class CommandRollDice(AbstractCommand):
11     TRIGGER = "roll"
12     CONFIG = {"display_name": "somebot-command", "auto_complete": True,
13               "auto_complete_hint": "[n or ndm]",
14              }
15     USEINFO = CONFIG["auto_complete_desc"] = CONFIG["description"] = "Roll dice n [n] or n dices m times [ndm]."
16
17
18     def on_POST(self, request, data):
19         if re.search(r"^([1-9]|[1-9][0-9]|100)$", data["text"]):
20             msg = "Rolling a d" + data["text"] + ": " + str(randint(1, int(data["text"])))
21         elif re.search(r"^([1-9])[d]([1-9]|[1-9][0-9]|100)$", data["text"]):
22             msg = "Rolling a d" + re.findall(r"\d+", data["text"])[1] + " " + re.findall(r"\d+", data["text"])[0] + " times: "
23             for x in range(int(re.findall(r"\d+", data["text"])[0])):
24                 msg = msg + str(randint(1, int(re.findall(r"\d+", data["text"])[1]))) + ", "
25             msg = msg[:-2]
26         else:
27             request.cmd_respond_text_temp("Zulässige Parameter sind: 'n' oder 'ndm' wobei n [1-9] und m [1-100] Zahlen sind.")
28             return
29
30         request.cmd_respond_text_chan(msg)