From 257628aa5e7bc05229f806c7ba596d2dd25e4a75 Mon Sep 17 00:00:00 2001 From: PinLin Date: Thu, 24 Aug 2017 18:05:44 +0800 Subject: [PATCH] fix group fail-connecting bug --- bot.py | 32 ++++++++++++++++---------------- interface.py | 37 +++++++++++++++++++++---------------- 2 files changed, 37 insertions(+), 32 deletions(-) diff --git a/bot.py b/bot.py index 6d0e1fb..8feaa3b 100644 --- a/bot.py +++ b/bot.py @@ -58,13 +58,13 @@ def on_chat(msg): bot.sendMessage(chat_id, "請先私訊我登入 kCOJ", reply_to_message_id=msg['message_id']) elif command[0] == '/start' or command[0] == '首頁🏠': - if user.check_online() == True: + if user.check_online(chat_id, msg['message_id']) == True: user.display_main(chat_id) elif command[0] == '/question' or command[0] == '題庫📝' or command[0] == '更新🔃': - if user.check_online() == True: + if user.check_online(chat_id, msg['message_id']) == True: if len(command) > 1: - user.display_question(chat_id, command[1]) + user.display_question(command[1], chat_id) else: user.display_questions(chat_id) @@ -82,11 +82,11 @@ def on_chat(msg): elif chat_type == 'private': if command[0] == '/help' or command[0] == '幫助📚': - if user.check_online() == True: + if user.check_online(chat_id, msg['message_id']) == True: user.help_you() elif command[0] == '/password' or command[0] == '改密碼💱': - if user.check_online() == True: + if user.check_online(chat_id, msg['message_id']) == True: user.press_oldpassword() elif command[0] == '/logout' or command[0] == '登出🚪': @@ -95,44 +95,44 @@ def on_chat(msg): user.logout_system() elif (command[0] == '/delete' or command[0] == '刪除作業⚔️') and user.question != '題外': - if user.check_online() == True: + if user.check_online(chat_id, msg['message_id']) == True: user.delete_answer() elif (command[0] == '/upload' or command[0] == '交作業📮') and user.question != '題外': - if user.check_online() == True: + if user.check_online(chat_id, msg['message_id']) == True: user.upload_answer() elif (command[0] == '/result' or command[0] == '看結果☑️') and user.question != '題外': - if user.check_online() == True: + if user.check_online(chat_id, msg['message_id']) == True: user.list_results() elif (command[0] == '/passer' or command[0] == '通過者🌐') and user.question != '題外': - if user.check_online() == True: + if user.check_online(chat_id, msg['message_id']) == True: user.list_passers() elif command[0] == '回題目📜' and user.question != '題外': - if user.check_online() == True: - user.display_question(chat_id ,user.question) + if user.check_online(chat_id, msg['message_id']) == True: + user.display_question(user.question, chat_id) elif user.status == '舊的密碼': - if user.check_online() == True: + if user.check_online(chat_id, msg['message_id']) == True: user.press_newpassword(msg['text']) elif user.status == '修改密碼': - if user.check_online() == True: + if user.check_online(chat_id, msg['message_id']) == True: user.change_password(msg['text']) elif user.status == '上傳答案': - if user.check_online() == True: + if user.check_online(chat_id, msg['message_id']) == True: user.send_answer(msg['text'], '') else: - if user.check_online() == True: + if user.check_online(chat_id, msg['message_id']) == True: bot.sendMessage(chat_id, "(ˊ・ω・ˋ)") elif content_type == 'document': if user.status == '上傳答案' or user.status == '查看題目': - if user.check_online() == True: + if user.check_online(chat_id, msg['message_id']) == True: if msg['document']['file_size'] > 167770000: user.fail_send() else: diff --git a/interface.py b/interface.py index 436048b..dc36e2a 100644 --- a/interface.py +++ b/interface.py @@ -75,37 +75,42 @@ class kuser: self.question = '題外' self.password = text bot.sendMessage(self.userid, "登入中...", reply_markup=ReplyKeyboardRemove()) - if self.check_online() == True: + if self.check_online(self.userid) == True: self.display_main(self.userid) - def fail_login(self): + def fail_login(self, chat_id, message_id): self.status = '正常使用' self.question = '題外' - bot.sendMessage(self.userid, "哇...登入失敗,讓我們重新開始一次", reply_markup=ReplyKeyboardRemove()) + if chat_id != self.userid: + bot.sendMessage(chat_id, "登入失敗,請先私訊我重新登入 kCOJ", reply_to_message_id=message_id) + bot.sendMessage(self.userid, "哇...登入失敗,讓我們重新開始", reply_markup=ReplyKeyboardRemove()) self.press_username() + + def fail_connecting(self, chat_id, message_id): + self.status = '正常使用' + self.question = '題外' + if chat_id != self.userid: + bot.sendMessage(chat_id, "kCOJ 離線中!", reply_to_message_id=message_id) + else: + bot.sendMessage(self.userid, "kCOJ 離線中!", + reply_markup=ReplyKeyboardMarkup(keyboard=[ + ["首頁🏠"] + ], resize_keyboard=True)) - def check_online(self): + def check_online(self, chat_id, message_id=''): result = self.api.check_online() if result == None: - self.fail_connecting() + self.fail_connecting(chat_id, message_id) return False else: if result == False: self.api.login_kcoj(self.username, self.password) result = self.api.check_online() if result == False: - self.fail_login() + self.fail_login(chat_id, message_id) elif result == None: - self.fail_connecting() + self.fail_connecting(chat_id, message_id) return result == True - - def fail_connecting(self): - self.status = '正常使用' - self.question = '題外' - bot.sendMessage(self.userid, "郭老 Online Judge 離線中!", - reply_markup=ReplyKeyboardMarkup(keyboard=[ - ["首頁🏠"] - ], resize_keyboard=True)) def logout_system(self): self.status = '正常使用' @@ -154,7 +159,7 @@ class kuser: ], resize_keyboard=True) if chat_id == self.userid else ReplyKeyboardRemove()) bot.sendMessage(chat_id, "點我到題庫頂", reply_to_message_id=reply['message_id']) - def display_question(self, chat_id, number): + def display_question(self, number, chat_id): self.status = '查看題目' self.question = number content = self.api.show_question(number)