From d742c268819a78bb6df98d010b89afe8ac357410 Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Tue, 28 Mar 2017 22:48:53 +0200 Subject: [PATCH 1/4] Probably fixed #2 --- telegram.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/telegram.py b/telegram.py index d51b5085..b7569ed9 100644 --- a/telegram.py +++ b/telegram.py @@ -63,7 +63,7 @@ class Bot: self.updates.append(Update(update)) except NotImplementedError: pass - if len(data) > 0: + if len(self.updates) > 0: self.offset = self.updates[-1].update_id + 1 async def parse_update(self, update): From 4197ba34878ae561ca27e08f266a3c645f23c7e1 Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Wed, 29 Mar 2017 09:15:38 +0200 Subject: [PATCH 2/4] Removed unused argument --- grandbot.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/grandbot.py b/grandbot.py index 1fe8ddf6..4d2745d2 100644 --- a/grandbot.py +++ b/grandbot.py @@ -31,7 +31,7 @@ def currently_logged_in(thing): return user -async def start_telegram(bot, update, arguments): +async def start_telegram(bot, update, _): # Set status to typing await update.message.chat.set_chat_action(bot, "typing") user = currently_logged_in(update) From 60f10fa72e4f359e247648366207cc716e71cc19 Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Wed, 29 Mar 2017 09:32:38 +0200 Subject: [PATCH 3/4] New configuration system, this time for real --- .gitignore | 3 --- royalbotconfig.py | 19 +++++++++++++++++++ 2 files changed, 19 insertions(+), 3 deletions(-) create mode 100644 royalbotconfig.py diff --git a/.gitignore b/.gitignore index b108dbc7..a2870570 100644 --- a/.gitignore +++ b/.gitignore @@ -92,9 +92,6 @@ ENV/ .idea/ -# config file -royalbotconfig.py - # grandbot.py files diario.txt diff --git a/royalbotconfig.py b/royalbotconfig.py new file mode 100644 index 00000000..6f150f45 --- /dev/null +++ b/royalbotconfig.py @@ -0,0 +1,19 @@ +import os + +class MissingTokenError(Exception): + pass + +if "telegram_token" in os.environ: + telegram_token = os.environ["telegram_token"] +else: + raise MissingTokenError("telegram_token") + +if "discord_token" in os.environ: + discord_token = os.environ["discord_token"] +else: + raise MissingTokenError("discord_token") + +if "discord_webhook" in os.environ: + discord_webhook = os.environ["discord_webhook"] +else: + raise MissingTokenError("discord_webhook") From 03dc07ff9b4f4fff67bf550d28372959826f7ffe Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Wed, 29 Mar 2017 09:44:32 +0200 Subject: [PATCH 4/4] Added find_user function --- database.py | 23 ++++++++++++++--------- grandbot.py | 6 ++---- 2 files changed, 16 insertions(+), 13 deletions(-) diff --git a/database.py b/database.py index 56a82ec2..ca8f9622 100644 --- a/database.py +++ b/database.py @@ -71,17 +71,12 @@ def login(username, password, enable_exceptions=False): # Create a new session session = Session() # Find the matching user - users = session.query(User).filter_by(username=username).all() - # No user with a matching username found - if len(users) == 0: - if enable_exceptions: - raise NoUsersMatchingError("No users with the specified username found.") - else: - return session, None - db_user = users[0] + db_user = session.query(User).filter_by(username=username).first() + # Verify that the user exists + if db_user is not None: + return session, None # Test the password and return the session and the user if successful if bcrypt.hashpw(password.encode("utf8"), db_user.password) == db_user.password: - # TODO: Maybe there's a better way to do this? return session, db_user else: if enable_exceptions: @@ -93,6 +88,16 @@ def login(username, password, enable_exceptions=False): def init_royal_db(): create_user("test", "test", True) + +def find_user(username): + """Find the user with the specified username and return the session and the user object.""" + # Create a new session + session = Session() + # Find the matching user + db_user = session.query(User).filter_by(username=username).first() + # Return the session and the user + return session, db_user + session = Session() # Generate the database if it's empty if session.query(User).first() is None: diff --git a/grandbot.py b/grandbot.py index c1658613..ea147a92 100644 --- a/grandbot.py +++ b/grandbot.py @@ -540,10 +540,8 @@ Sintassi: `/toggleroyal `""" if len(arguments) != 1: await update.message.reply(bot, "⚠ Sintassi del comando non valida.\n`/toggleroyal `", parse_mode="Markdown") return - # Create a new database session - session = database.Session() - # Find the user - user = session.query(database.User).filter_by(username=arguments[0]).first() + # Find the specified user + session, user = database.find_user(arguments[0]) # Check if the user exists if user is None: await update.message.reply(bot, "⚠ L'utente specificato non esiste.")