diff --git a/basicbot.py b/basicbot.py index 887c1625..34be3d46 100644 --- a/basicbot.py +++ b/basicbot.py @@ -13,6 +13,13 @@ import database b = telegram.Bot(royalbotconfig.telegram_token) +def currently_logged_in(update): + """Find the database user which sent the update.""" + session = database.Session() + user = session.query(database.User).filter_by(telegram_id=update.message.sent_from.user_id).first() + return session, user + + async def diario(bot, update, arguments): """Aggiungi una frase al diario Royal Games. @@ -125,7 +132,7 @@ Sintassi: `/sync `""" # Handle duplicate logged_user.telegram_id = update.message.sent_from.user_id session.commit() - await update.message.chat.send_message(bot, f"Sincronizzazione riuscita: {logged_user}") + await update.message.chat.send_message(bot, f"Sincronizzazione riuscita!\nSei loggato come `{logged_user}`.") else: await update.message.chat.send_message(bot, "⚠ L'account è già stato sincronizzato.") else: @@ -135,17 +142,16 @@ Sintassi: `/sync `""" async def changepassword(bot, update, arguments): """Cambia la tua password del Database Royal Games. -Sintassi: `/changepassword `""" - if len(arguments) != 3: - await update.message.chat.send_message(bot, "⚠ Sintassi del comando non valida.\n`/sync `") +Sintassi: `/changepassword `""" + if len(arguments) != 2: + await update.message.chat.send_message(bot, "⚠ Sintassi del comando non valida.\n`/changepassword `") return - # TODO: this can be improved - # Try to login - _, logged_user = database.login(arguments[0], arguments[1]) + # TODO: this can be improved, maybe? + session, logged_user = currently_logged_in(update) # Check if the login is successful if logged_user is not None: # Change the password - database.change_password(logged_user.username, arguments[2]) + database.change_password(logged_user.username, arguments[1]) await update.message.chat.send_message(bot, f"Il cambio password è riuscito!\n\n_Info per smanettoni: la tua password è hashata nel database come_ `{logged_user.password}`.") else: await update.message.chat.send_message(bot, "⚠ Username o password non validi.") diff --git a/database.py b/database.py index d5d60c9b..fb2b3aae 100644 --- a/database.py +++ b/database.py @@ -65,7 +65,7 @@ def login(username, password, enable_exceptions=False): # Create a new session session = Session() # Find the matching user - users = session.query(User).filter(User.username == username).all() + users = session.query(User).filter_by(username=username).all() # No user with a matching username found if len(users) == 0: if enable_exceptions: