From 928f7b00af1ee4a5ccffae45f11dbc5ca12fefa3 Mon Sep 17 00:00:00 2001 From: lza_menace Date: Thu, 6 Aug 2020 13:40:03 -0700 Subject: [PATCH] add debug mode option to disable the bot when developing locally --- tipbot/commands/balance.py | 3 ++- tipbot/commands/debug.py | 3 ++- tipbot/commands/deposit.py | 3 ++- tipbot/commands/register.py | 3 ++- tipbot/commands/tip.py | 3 ++- tipbot/commands/withdraw.py | 3 ++- tipbot/config.example.py | 1 + tipbot/helpers/decorators.py | 17 ++++++++++++++++- 8 files changed, 29 insertions(+), 7 deletions(-) diff --git a/tipbot/commands/balance.py b/tipbot/commands/balance.py index 68379c4..dfd65b3 100644 --- a/tipbot/commands/balance.py +++ b/tipbot/commands/balance.py @@ -1,12 +1,13 @@ import logging from tipbot import wownero from tipbot import db -from tipbot.helpers.decorators import wallet_rpc_required, log_event, registration_required +from tipbot.helpers.decorators import wallet_rpc_required, log_event, registration_required, check_debug @wallet_rpc_required @registration_required @log_event +@check_debug def balance(update, context): u = db.User.get(telegram_id=update.message.from_user['id']) balances = wownero.Wallet().balances(account=u.account_index) diff --git a/tipbot/commands/debug.py b/tipbot/commands/debug.py index 6bc9fe8..99e6a7f 100644 --- a/tipbot/commands/debug.py +++ b/tipbot/commands/debug.py @@ -1,9 +1,10 @@ -from tipbot.helpers.decorators import wallet_rpc_required, log_event +from tipbot.helpers.decorators import wallet_rpc_required, log_event, check_debug from tipbot.helpers.utils import is_tg_admin @wallet_rpc_required @log_event +@check_debug def debug(update, context): if is_tg_admin(update.message.from_user['id']): pass diff --git a/tipbot/commands/deposit.py b/tipbot/commands/deposit.py index 0e466a8..daf2395 100644 --- a/tipbot/commands/deposit.py +++ b/tipbot/commands/deposit.py @@ -1,12 +1,13 @@ import logging from tipbot import wownero from tipbot import db -from tipbot.helpers.decorators import wallet_rpc_required, log_event, registration_required +from tipbot.helpers.decorators import wallet_rpc_required, log_event, registration_required, check_debug @wallet_rpc_required @registration_required @log_event +@check_debug def deposit(update, context): u = db.User.get(telegram_id=update.message.from_user['id']) address = wownero.Wallet().addresses(account=u.account_index)[0] diff --git a/tipbot/commands/register.py b/tipbot/commands/register.py index eaa7a21..3d088e7 100644 --- a/tipbot/commands/register.py +++ b/tipbot/commands/register.py @@ -1,11 +1,12 @@ import logging from tipbot import wownero from tipbot import db -from tipbot.helpers.decorators import wallet_rpc_required, log_event +from tipbot.helpers.decorators import wallet_rpc_required, log_event, check_debug @wallet_rpc_required @log_event +@check_debug def register(update, context): uid = update.message.from_user['id'] un = update.message.from_user['first_name'] diff --git a/tipbot/commands/tip.py b/tipbot/commands/tip.py index 744af24..c4474a6 100644 --- a/tipbot/commands/tip.py +++ b/tipbot/commands/tip.py @@ -3,12 +3,13 @@ from decimal import Decimal from telegram import ParseMode from tipbot import wownero from tipbot import db -from tipbot.helpers.decorators import wallet_rpc_required, log_event, registration_required +from tipbot.helpers.decorators import wallet_rpc_required, log_event, registration_required, check_debug @wallet_rpc_required @registration_required @log_event +@check_debug def tip(update, context): if len(context.args) < 2: update.message.reply_text('Not enough arguments passed.') diff --git a/tipbot/commands/withdraw.py b/tipbot/commands/withdraw.py index 7a3bed1..5dc3366 100644 --- a/tipbot/commands/withdraw.py +++ b/tipbot/commands/withdraw.py @@ -3,12 +3,13 @@ from decimal import Decimal from telegram import ParseMode from tipbot import wownero from tipbot import db -from tipbot.helpers.decorators import wallet_rpc_required, log_event, registration_required +from tipbot.helpers.decorators import wallet_rpc_required, log_event, registration_required, check_debug @wallet_rpc_required @registration_required @log_event +@check_debug def withdraw(update, context): if len(context.args) < 2: update.message.reply_text('Not enough arguments passed.') diff --git a/tipbot/config.example.py b/tipbot/config.example.py index 8143bbd..26377b0 100644 --- a/tipbot/config.example.py +++ b/tipbot/config.example.py @@ -1,3 +1,4 @@ +DEBUG = True TG_TOKEN = 'tttttttttttt' TG_ADMIN_ID = 0000000000 WALLET_PROTO = 'http' diff --git a/tipbot/helpers/decorators.py b/tipbot/helpers/decorators.py index 13c9074..61660d2 100644 --- a/tipbot/helpers/decorators.py +++ b/tipbot/helpers/decorators.py @@ -1,9 +1,24 @@ import logging +from functools import wraps from tipbot import wownero from tipbot import db -from functools import wraps +from tipbot import config +from tipbot.helpers.utils import is_tg_admin +def check_debug(f): + @wraps(f) + def decorated_function(*args, **kwargs): + msg = args[0].message + is_admin = is_tg_admin(msg.from_user["id"]) + is_debug = getattr(config, 'DEBUG', True) == True + if is_debug: + if not is_admin: + msg.reply_text('I am in debug mode by my admin. Commands are disabled at the moment. Try again later.') + return False + return f(*args, **kwargs) + return decorated_function + def log_event(f): @wraps(f) def decorated_function(*args, **kwargs):