force tg username in register

This commit is contained in:
lza_menace 2020-08-08 23:39:19 -07:00
parent de002b27de
commit e9a1e105e7

View file

@ -1,6 +1,6 @@
import logging msgimport logging
from tipbot import wownero from tipbot import wownero
from tipbot import db from tipbot.db import User
from tipbot.helpers.decorators import wallet_rpc_required, log_event, check_debug from tipbot.helpers.decorators import wallet_rpc_required, log_event, check_debug
@ -8,32 +8,36 @@ from tipbot.helpers.decorators import wallet_rpc_required, log_event, check_debu
@log_event @log_event
@check_debug @check_debug
def register(update, context): def register(update, context):
uid = update.message.from_user['id'] wallet = wownero.Wallet()
un = update.message.from_user['first_name'] msg = update.message
uid = msg.from_user['id']
un = getattr(msg.from_user, 'username', None)
if un is None:
msg.reply_text('You need a username configured in Telegram to use this bot.')
return False
if db.User.filter(telegram_id=uid): if User.filter(telegram_id=uid):
if db.User.filter(telegram_id=uid, telegram_user=un): if User.filter(telegram_id=uid, telegram_user=un):
update.message.reply_text('You are already registered. Use /help to see available bot commands.') msg.reply_text('You are already registered. Use /help to see available bot commands.')
else: else:
try: try:
u = db.User.get(telegram_id=uid) u = User.get(telegram_id=uid)
u.telegram_user = un u.telegram_user = un
u.save() u.save()
update.message.reply_text(f'You have been registered again as Telegram ID {uid} but with username {un}.') msg.reply_text(f'You have been registered again as Telegram ID {uid} but with username {un}.')
except Exception as e: except Exception as e:
logging.error(f'Unable to update user in DB: {e}. Debug: {update.message}') logging.error(f'Unable to update user in DB: {e}. Debug: {msg}')
update.message.reply_text('Unable to update your existing account. Ask for help.') msg.reply_text('Unable to update your existing account. Ask for help.')
return False return False
else: else:
try: try:
wallet = wownero.Wallet()
account_index = wallet.new_account(label=un) account_index = wallet.new_account(label=un)
except Exception as e: except Exception as e:
logging.error(f'Unable to create a new account in wallet RPC: {e}. Debug: {update.message}') logging.error(f'Unable to create a new account in wallet RPC: {e}. Debug: {msg}')
update.message.reply_text('Unable to create a new account for you. Ask for help.') msg.reply_text('Unable to create a new account for you. Ask for help.')
return False return False
try: try:
u = db.User( u = User(
telegram_id=uid, telegram_id=uid,
telegram_user=un, telegram_user=un,
account_index=account_index, account_index=account_index,
@ -43,8 +47,8 @@ def register(update, context):
f'You have been registered as Telegram ID {uid} and username {un} and can now send and receive tips.', f'You have been registered as Telegram ID {uid} and username {un} and can now send and receive tips.',
'Ask for /help to see all available bot commands. Maybe start with /deposit to get your deposit address.' 'Ask for /help to see all available bot commands. Maybe start with /deposit to get your deposit address.'
] ]
update.message.reply_text(' '.join(reply_text)) msg.reply_text(' '.join(reply_text))
except Exception as e: except Exception as e:
logging.error(f'Unable to register user in DB: {e}. Debug: {update.message}') logging.error(f'Unable to register user in DB: {e}. Debug: {msg}')
update.message.reply_text('Unable to create a new account for you. Ask for help.') msg.reply_text('Unable to create a new account for you. Ask for help.')
return False return False