From 4dfefe904189b2e54f12fccf6353d254c695578d Mon Sep 17 00:00:00 2001 From: Someone Date: Wed, 8 Dec 2021 19:38:38 +0100 Subject: [PATCH] [somebot] /drink* --- modules/CommandDrink.py | 101 +++++++++++++++++++++++++++++ modules/CommandDrinkBindAccount.py | 43 ++++++++++++ 2 files changed, 144 insertions(+) create mode 100644 modules/CommandDrink.py create mode 100644 modules/CommandDrinkBindAccount.py diff --git a/modules/CommandDrink.py b/modules/CommandDrink.py new file mode 100644 index 0000000..ee4cf04 --- /dev/null +++ b/modules/CommandDrink.py @@ -0,0 +1,101 @@ +# Mattermost Bot module. +# Copyright (c) 2016-2021 by Someone (aka. Jan Vales ) +# published under MIT-License + +from inspect import cleandoc +import re +import datetime + +import MCAPI + + +from AbstractCommand import AbstractCommand +class CommandDrink(AbstractCommand): + TRIGGER = "drink" + CONFIG = {"display_name": "somebot-command", "auto_complete": True, + "auto_complete_hint": "['help'|<[count]shortcut>]", + } + USEINFO = CONFIG["auto_complete_desc"] = CONFIG["description"] = "Buy a drink." + + + def __init__(self, team_id, drink_terminal_user, drink_terminal_pass): + super().__init__(team_id) + + self.drink_terminal_user = drink_terminal_user + self.drink_terminal_pass = drink_terminal_pass + + + def on_POST(self, request, data, rerun=True): + try: + apirequest = dict() + apirequest["act"] = "create_token_secret" + handle = MCAPI.API("KEYBOARD", self.drink_terminal_user, self.drink_terminal_pass) + token_secret = handle.api_request(apirequest) + handle = MCAPI.API("MM", data["user_id"]) + MCAPI.API.token_secret = token_secret["token_secret"] + account_data = handle.get_account_info() + msg = ":arrow_right: Available credits:``"+str(account_data["account_info"]["balance"])+"``" + except MCAPI.APIException as e: + if e.message == "CERR_AUTH_FAIL_INVALID_token": + request.cmd_respond_text_temp(cleandoc(""" + :stop_sign: Mattermost account seems to not be bound to any drink-terminal account. :( + Use ``/drink-bind-account `` to bind this Mattermost account to your drink-terminal account. + """)) + return + + try: + cmd = data["text"] = data["text"].strip() + if data["text"] == "" or data["text"].lower() == "help": + msg += "\nUse ``/drink [count]`` and one of the following shortcuts to pay for ``$count * $shortcut``.\n\n|shortcut|price|Description|\n|---|---|---|" + + for s in account_data["shortcuts"]: + if int(s["vislevel"]) >= 2: + msg += "\n|"+s["short"]+"|"+s["amount"]+"|"+s["description"]+"|" + request.cmd_respond_text_temp(msg) + return + + count = None + shortcut = None + + mat = re.match(r"(\d*)\s*(\w+)", cmd) + if mat is None: + request.cmd_respond_text_temp(msg+"\n:stop_sign: Invalid input: Nothing useful matched.") + return + + (count, sc) = mat.groups() + + if count == "": + count = 1 + + count = int(count) + if count < 1: + request.cmd_respond_text_temp(msg+"\n:stop_sign: Invalid input: count < 1") + return + + for s in account_data["shortcuts"]: + if s["short"] == sc: + shortcut = s + break + + if shortcut is None: + request.cmd_respond_text_temp(msg+"\n:stop_sign: Invalid input: no such shortcut :(\nUse ``/drink-shortcuts`` for a list of shortcuts.") + return + else: + handle.do_transaction(shortcut["target"], str(int(shortcut["amount"])*count), shortcut["reason"]) + account_data = handle.get_account_info(True) + + # Infcoin-IPC + 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())) + + msg = (":arrow_right:Available credits:``"+str(account_data["account_info"]["balance"])+"``"+ + "\n:white_check_mark: Sucessfully paid **``"+str(int(shortcut["amount"])*count)+"``** TO **``"+str(shortcut["target"])+"``** FOR **``"+str(shortcut["reason"])+"``** :beers:") + self.bot.debug_chan("``/drink success: @"+data["user_name"]+' --'+str(int(shortcut["amount"])*count)+'--> '+str(shortcut["target"])+'``') + + except MCAPI.APIException as e: + if e.message == "CERR_AUTH_FAIL_ACCTOK": + msg += "\n:stop_sign: Mattermost account seems to not be bound to any drink-terminal account. :(" + + elif e.message == "CERR_FAILED_TO_SEND": + 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"])+"``**" + + request.cmd_respond_text_temp(msg) diff --git a/modules/CommandDrinkBindAccount.py b/modules/CommandDrinkBindAccount.py new file mode 100644 index 0000000..21edf7a --- /dev/null +++ b/modules/CommandDrinkBindAccount.py @@ -0,0 +1,43 @@ +# Mattermost Bot module. +# Copyright (c) 2016-2021 by Someone (aka. Jan Vales ) +# published under MIT-License + +from inspect import cleandoc + +import MCAPI + + +from AbstractCommand import AbstractCommand +class CommandDrinkBindAccount(AbstractCommand): + TRIGGER = "drink-bind-account" + CONFIG = {"display_name": "somebot-command", "auto_complete": True, + "auto_complete_hint": " ", + } + USEINFO = CONFIG["auto_complete_desc"] = CONFIG["description"] = "Bind your drink-terminal-account to your mattermost username." + + + def on_POST(self, request, data): + data['text'] = data['text'].strip() + mc_credentials = data['text'].split(" ", 2) + + if data["text"] == "" or data["text"].lower() == "help" or len(mc_credentials) != 2: + request.cmd_respond_text_temp(cleandoc("""This command takes exactly 2 parameters: `` `` to pay for your drink. + """)) + + try: + handle = MCAPI.API("KEYBOARD", mc_credentials[0], mc_credentials[1]) + handle.manage_token('add', 'MM_'+data["user_id"]) + except MCAPI.APIException as e: + if e.message == 'CERR_AUTH_FAIL_ACCPWDRATE': + request.cmd_respond_text_temp(":stop_sign: Failed to bind Mattermost account to drink-terminal account: Invalid drink-terminal credentials :(") + elif e.message == 'CERR_FAILED_TO_ADD_TOKEN_EXISTING': + request.cmd_respond_text_temp(":warning: Failed to bind Mattermost account to drink-terminal account: Mattermost username is already bound to an account.") + else: + request.cmd_respond_text_temp(":warning: Failed to bind Mattermost account to drink-terminal account: Unknown error: "+ e.message) + return + + request.cmd_respond_text_temp(":white_check_mark: Sucessfully bound Mattermost account and drink-terminal account :)") + self.bot.debug_chan("``/drink-bind-account success: @"+data["user_name"]+'``') -- 2.43.0