From 2fc33d598e68da880b9da5b5e086a4531f2de9b4 Mon Sep 17 00:00:00 2001 From: lza_menace Date: Wed, 5 Aug 2020 22:52:36 -0700 Subject: [PATCH] move utils and decorators into module --- tipbot/helpers/__init__.py | 0 tipbot/helpers/decorators.py | 34 ++++++++++++++++++++++++++++++++++ tipbot/helpers/utils.py | 7 +++++++ 3 files changed, 41 insertions(+) create mode 100644 tipbot/helpers/__init__.py create mode 100644 tipbot/helpers/decorators.py create mode 100644 tipbot/helpers/utils.py diff --git a/tipbot/helpers/__init__.py b/tipbot/helpers/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tipbot/helpers/decorators.py b/tipbot/helpers/decorators.py new file mode 100644 index 0000000..13c9074 --- /dev/null +++ b/tipbot/helpers/decorators.py @@ -0,0 +1,34 @@ +import logging +from tipbot import wownero +from tipbot import db +from functools import wraps + + +def log_event(f): + @wraps(f) + def decorated_function(*args, **kwargs): + msg = args[0].message + logging.info(f'"{f.__name__}" invoked from {msg.from_user["id"]} ({msg.from_user["first_name"]}) - Full command: "{msg.text}"') + return f(*args, **kwargs) + return decorated_function + +def wallet_rpc_required(f): + @wraps(f) + def decorated_function(*args, **kwargs): + wallet = wownero.Wallet() + if not wallet.connected: + logging.error(f'Wallet RPC interface is not available: {args[0].message}') + args[0].message.reply_text('Wallet RPC interface is not available right now. Try again later.') + return False + return f(*args, **kwargs) + return decorated_function + +def registration_required(f): + @wraps(f) + def decorated_function(*args, **kwargs): + wallet = wownero.Wallet() + if not db.User.filter(telegram_id=args[0].message.from_user['id']): + args[0].message.reply_text('You are not yet registered. Issue the /register command.') + return False + return f(*args, **kwargs) + return decorated_function diff --git a/tipbot/helpers/utils.py b/tipbot/helpers/utils.py new file mode 100644 index 0000000..df974eb --- /dev/null +++ b/tipbot/helpers/utils.py @@ -0,0 +1,7 @@ +from tipbot import config + +def is_tg_admin(chat_id): + if chat_id == config.TG_ADMIN_ID: + return True + else: + return False