]> git.somenet.org - pub/jan/mattermost.git/blob - somebot/modules/CommandMate.py
[somebot] WS:ConvertWebp
[pub/jan/mattermost.git] / somebot / modules / CommandMate.py
1 # Mattermost Bot.
2 #  Copyright (c) 2016-2020 by Someone <someone@somenet.org> (aka. Jan Vales <jan@jvales.net>)
3 #  published under MIT-License
4
5 import MCAPI
6
7
8 from AbstractCommand import *
9 class CommandMate(AbstractCommand):
10     TRIGGER = "mate"
11     CONFIG = {"display_name": "somebot-command", "auto_complete": True,
12         "auto_complete_hint": "['help'|<[count]shortcut>]",
13     }
14     USEINFO = CONFIG["auto_complete_desc"] = CONFIG["description"] = "Buy a drink."
15
16
17     def __init__(self, team_id, mate_terminal_user, mate_terminal_pass):
18         super().__init__(team_id)
19
20         self.mate_terminal_user = mate_terminal_user
21         self.mate_terminal_pass = mate_terminal_pass
22
23
24     def on_POST(self, request, data):
25         try:
26             apirequest = dict()
27             apirequest["act"] = "create_token_secret"
28             handle = MCAPI.API("KEYBOARD", self.mate_terminal_user, self.mate_terminal_pass)
29             token_secret = handle.api_request(apirequest)
30             handle = MCAPI.API("MM", data["user_id"])
31             MCAPI.API.token_secret = token_secret["token_secret"]
32             account_data = handle.get_account_info()
33             msg = ":arrow_right: Mate-terminal account:``"+str(account_data["account_info"]["accname"])+"``, available credits:``"+str(account_data["account_info"]["balance"])+"``"
34         except MCAPI.APIException as e:
35             if "CERR_AUTH_FAIL_ACCTOK" == e.message:
36                 request.cmd_respond_text_temp(":stop_sign: Mattermost account seems to not be bound to any mate-terminal account. :(\n"+
37                                           "Use ``/mate-bind-account <mate-terminal-username> <mate-terminal-password>`` to bind this Mattermost account to your mate-terminal account.")
38
39         try:
40             cmd = data["text"] = data["text"].strip()
41             if data["text"] == "" or data["text"].lower() == "help":
42                 msg += "\nUse ``/mate [count]<shortcut>`` and one of the following shortcuts to pay for ``$count * $shortcut``.\n\n|shortcut|price|Description|\n|---|---|---|"
43
44                 for s in account_data["shortcuts"]:
45                     if int(s["vislevel"]) >= 2:
46                         msg += "\n|"+s["short"]+"|"+s["amount"]+"|"+s["description"]+"|"
47                 request.cmd_respond_text_temp(msg)
48                 return
49
50             count = None
51             shortcut = None
52
53             import re
54             mat = re.match(r"(\d*)\s*(\w+)", cmd)
55             if mat is None:
56                 request.cmd_respond_text_temp(msg+"\n:stop_sign: Invalid input: Nothing useful matched.")
57                 return
58
59             (count, sc) = mat.groups()
60
61             if count == "":
62                 count = 1
63
64             count = int(count)
65             if count < 1:
66                 request.cmd_respond_text_temp(msg+"\n:stop_sign: Invalid input: count < 1")
67                 return
68
69             for s in account_data["shortcuts"]:
70                 if s["short"] == sc:
71                     shortcut = s
72                     break
73
74             if shortcut == None:
75                 request.cmd_respond_text_temp(msg+"\n:stop_sign: Invalid input: no such shortcut :(\nUse ``/mate-shortcuts`` for a list of shortcuts.")
76                 return
77             else:
78                 result = handle.do_transaction(shortcut["target"], str(int(shortcut["amount"])*count), shortcut["reason"])
79                 account_data = handle.get_account_info(True)
80                 msg = (":arrow_right: Mate-terminal account:``"+str(account_data["account_info"]["accname"])+"``, available credits:``"+str(account_data["account_info"]["balance"])+"``"+
81                       "\n:white_check_mark: Sucessfully paid **``"+str(int(shortcut["amount"])*count)+"``** TO **``"+str(shortcut["target"])+"``** FOR **``"+str(shortcut["reason"])+"``** :beers:")
82                 self.bot.debug_chan("``/mate success: @"+data["user_name"]+' --'+str(int(shortcut["amount"])*count)+'--> '+str(shortcut["target"])+'``')
83
84         except MCAPI.APIException as e:
85             if "CERR_AUTH_FAIL_ACCTOK" == e.message:
86                 msg += "\n:stop_sign: Mattermost account seems to not be bound to any mate-terminal account. :("
87
88             elif "CERR_FAILED_TO_SEND" == e.message:
89                 msg += "\n:stop_sign: Failed to send mate-credits. Balance exceeded? Sending too much? :(\nTried to send **``"+str(int(shortcut["amount"])*count)+"``** TO **``"+str(shortcut["target"])+"``** FOR **``"+str(shortcut["reason"])+"``**"
90
91         request.cmd_respond_text_temp(msg)
92