2 # Copyright (c) 2016-2020 by Someone <someone@somenet.org> (aka. Jan Vales <jan@jvales.net>)
3 # published under MIT-License
8 from AbstractCommand import *
9 class CommandMate(AbstractCommand):
11 CONFIG = {"display_name": "somebot-command", "auto_complete": True,
12 "auto_complete_hint": "['help'|<[count]shortcut>]",
14 USEINFO = CONFIG["auto_complete_desc"] = CONFIG["description"] = "Buy a drink."
17 def __init__(self, team_id, mate_terminal_user, mate_terminal_pass):
18 super().__init__(team_id)
20 self.mate_terminal_user = mate_terminal_user
21 self.mate_terminal_pass = mate_terminal_pass
24 def on_POST(self, request, data):
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.")
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|---|---|---|"
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)
54 mat = re.match(r"(\d*)\s*(\w+)", cmd)
56 request.cmd_respond_text_temp(msg+"\n:stop_sign: Invalid input: Nothing useful matched.")
59 (count, sc) = mat.groups()
66 request.cmd_respond_text_temp(msg+"\n:stop_sign: Invalid input: count < 1")
69 for s in account_data["shortcuts"]:
75 request.cmd_respond_text_temp(msg+"\n:stop_sign: Invalid input: no such shortcut :(\nUse ``/mate-shortcuts`` for a list of shortcuts.")
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"])+'``')
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. :("
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"])+"``**"
91 request.cmd_respond_text_temp(msg)