From 92a84262617ac56d881b9e400e7c11652b88510c Mon Sep 17 00:00:00 2001 From: Stan James Date: Fri, 19 Oct 2018 18:30:28 +0200 Subject: [PATCH] Fix bug when user has no first or last name Fixing: https://github.com/OriginProtocol/telegram-moderator/issues/15 Using `.format()` to handle `None` instead of the conditional check. --- bot.py | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/bot.py b/bot.py index b266bb0..637b906 100644 --- a/bot.py +++ b/bot.py @@ -86,8 +86,9 @@ class TelegramMonitorBot: def security_check_username(self, bot, update): """ Test username for security violations """ - full_name = (update.message.from_user.first_name + " " - + update.message.from_user.last_name) + full_name = "{} {}".format( + update.message.from_user.first_name, + update.message.from_user.last_name) if self.name_ban_re and self.name_ban_re.search(full_name): # Logging log_message = "❌ 🙅‍♂️ BAN MATCH FULL NAME: {}".format(full_name.encode('utf-8')) @@ -255,18 +256,22 @@ class TelegramMonitorBot: else: print("Something went wrong adding the user {}".format(user.id), file=sys.stderr) + user_name = ( + user.username or + "{} {}".format(user.first_name, user.last_name) or + "").encode('utf-8') if update.message.text: print("{} {} ({}) : {}".format( strftime("%Y-%m-%dT%H:%M:%S"), user.id, - (user.username or (user.first_name + " " + user.last_name) or "").encode('utf-8'), + user_name, update.message.text.encode('utf-8')) ) else: print("{} {} ({}) : non-message".format( strftime("%Y-%m-%dT%H:%M:%S"), user.id, - (user.username or (user.first_name + " " + user.last_name) or "").encode('utf-8')) + user_name) ) # Don't check admin activity