]> git.somenet.org - pub/jan/mattermost-bot.git/blob - modules/CommandRollDice.py
modules/CommandDrink.py
[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 # pylint: disable=wrong-import-position
10 from AbstractCommand import AbstractCommand
11 class CommandRollDice(AbstractCommand):
12     TRIGGER = "roll"
13     CONFIG = {"display_name": "somebot-command", "auto_complete": True,
14               "auto_complete_hint": "[n or ndm]",
15              }
16     USEINFO = CONFIG["auto_complete_desc"] = CONFIG["description"] = AbstractCommand.ICON_PUBLIC+"Roll dice n [n] or n dices m times [ndm]."
17
18
19     def on_POST(self, request, data):
20         if re.search(r"^([1-9]|[1-9][0-9]|100)$", data["text"]):
21             msg = "Rolling a d" + data["text"] + ": " + str(randint(1, int(data["text"])))
22         elif re.search(r"^([1-9])[d]([1-9]|[1-9][0-9]|100)$", data["text"]):
23             msg = "Rolling a d" + re.findall(r"\d+", data["text"])[1] + " " + re.findall(r"\d+", data["text"])[0] + " times: "
24             for x in range(int(re.findall(r"\d+", data["text"])[0])):
25                 msg = msg + str(randint(1, int(re.findall(r"\d+", data["text"])[1]))) + ", "
26             msg = msg[:-2]
27         else:
28             request.respond_cmd_temp("Zulässige Parameter sind: 'n' oder 'ndm' wobei n [1-9] und m [1-100] Zahlen sind.")
29             return
30
31         request.respond_cmd_chan(msg)