1 # Mattermost Bot module.
2 # Copyright (c) 2016-2022 by Someone <someone@somenet.org> (aka. Jan Vales <jan@jvales.net>)
3 # published under MIT-License
5 from inspect import cleandoc
12 # pylint: disable=wrong-import-position
13 from AbstractCommand import AbstractCommand
14 class CommandDrink(AbstractCommand):
16 CONFIG = {"display_name": "somebot-command", "auto_complete": True,
17 "auto_complete_hint": "['help'|<[count]shortcut>]",
19 USEINFO = CONFIG["auto_complete_desc"] = CONFIG["description"] = "Buy a drink."
22 def __init__(self, team_id, drink_terminal_user, drink_terminal_pass):
23 super().__init__(team_id)
25 self.drink_terminal_user = drink_terminal_user
26 self.drink_terminal_pass = drink_terminal_pass
29 def on_POST(self, request, data, rerun=True):
32 apirequest["act"] = "create_token_secret"
33 handle = MCAPI.API("KEYBOARD", self.drink_terminal_user, self.drink_terminal_pass)
34 token_secret = handle.api_request(apirequest)
35 handle = MCAPI.API("MM", data["user_id"])
36 MCAPI.API.token_secret = token_secret["token_secret"]
37 account_data = handle.get_account_info()
38 msg = ":arrow_right: Available credits:``"+str(account_data["account_info"]["balance"])+"``"
39 except MCAPI.APIException as e:
40 if e.message == "CERR_AUTH_FAIL_INVALID_token":
41 request.cmd_respond_text_temp(cleandoc("""
42 :stop_sign: Mattermost account seems to not be bound to any drink-terminal account. :(
43 Use ``/drink-bind-account <drink-terminal-username> <drink-terminal-password>`` to bind this Mattermost account to your drink-terminal account.
48 cmd = data["text"] = data["text"].strip()
49 if data["text"] == "" or data["text"].lower() == "help":
50 msg += "\nUse ``/drink [count]<shortcut>`` and one of the following shortcuts to pay for ``$count * $shortcut``.\n\n|shortcut|price|Description|\n|---|---|---|"
52 for s in account_data["shortcuts"]:
53 if int(s["vislevel"]) >= 2:
54 msg += "\n|"+s["short"]+"|"+s["amount"]+"|"+s["description"]+"|"
55 request.cmd_respond_text_temp(msg)
61 mat = re.match(r"(\d*)\s*(\w+)", cmd)
63 request.cmd_respond_text_temp(msg+"\n:stop_sign: Invalid input: Nothing useful matched.")
66 (count, sc) = mat.groups()
73 request.cmd_respond_text_temp(msg+"\n:stop_sign: Invalid input: count < 1")
76 for s in account_data["shortcuts"]:
82 request.cmd_respond_text_temp(msg+"\n:stop_sign: Invalid input: no such shortcut :(\nUse ``/drink-shortcuts`` for a list of shortcuts.")
85 handle.do_transaction(shortcut["target"], str(int(shortcut["amount"])*count), shortcut["reason"])
86 account_data = handle.get_account_info(True)
89 self.bot.modules[self.TEAM_ID]["inf-coin"].award_infcoins(data["user_id"], 100, "drink-use, ts="+str(datetime.datetime.now().replace(microsecond=0).isoformat()))
91 msg = (":arrow_right:Available credits:``"+str(account_data["account_info"]["balance"])+"``"+
92 "\n:white_check_mark: Sucessfully paid **``"+str(int(shortcut["amount"])*count)+"``** TO **``"+str(shortcut["target"])+"``** FOR **``"+str(shortcut["reason"])+"``** :beers:")
93 self.bot.debug_chan("``/drink success: @"+data["user_name"]+' --'+str(int(shortcut["amount"])*count)+'--> '+str(shortcut["target"])+'``')
95 except MCAPI.APIException as e:
96 if e.message == "CERR_AUTH_FAIL_ACCTOK":
97 msg += "\n:stop_sign: Mattermost account seems to not be bound to any drink-terminal account. :("
99 elif e.message == "CERR_FAILED_TO_SEND":
100 msg += "\n:stop_sign: Failed to send drink-credits. Balance exceeded? Sending too much? :(\nTried to send **``"+str(int(shortcut["amount"])*count)+"``** TO **``"+str(shortcut["target"])+"``** FOR **``"+str(shortcut["reason"])+"``**"
102 request.cmd_respond_text_temp(msg)