def start(self): Logger.log('Starting bot as ' + self.bot['username'] + '...', Color.GREEN) while True: try: self.check_for_commands() if self.run: InboxHandler.read_inbox(self.database, self.reddit) subscriptions = self.database.get_subscriptions() Logger.log(str(len(subscriptions)) + ' Subs') matches = MatchFinder.find_matches(subscriptions, self.reddit, self.database) Logger.log(str(len(matches)) + ' Matches') MatchHandler.send_messages(self.reddit, self.database, matches, self.bot) self.database.purge_old_matches() Logger.log(times.get_time_passed(self.start_time), Color.YELLOW) SleepHandler.sleep(20) except KeyboardInterrupt: Logger.log('Keyboard Interrupt - Bot killed', Color.RED) exit() except: handle_crash(traceback.format_exc(), self.bot, message_dev=False, reddit=self.reddit, database=self.database)
def handle_crash(stacktrace, bot_credentials, message_dev=False, reddit=None, database=None): reset = False while not reset: SleepHandler.sleep(30) try: print('Trying to handle error \n\n' + stacktrace) if reddit: reddit.reset() if database: database.reset() reddit.send_message( accounts['bot_errors']['username'], bot_credentials['username'] + ' - Exception Handled', stacktrace) if message_dev: reddit.send_message( accounts['developer']['username'], bot_credentials['username'] + ' - Exception Handled', stacktrace) print('Messaging Dev - Crash Handler') reset = True except: Logger.log('Failed to restart bot. Trying again in 30 seconds.', Color.RED)
def start(self): while True: try: unread_messages = self.reddit.get_unread() self.handle_messages(unread_messages) SleepHandler.sleep(60) except KeyboardInterrupt: Logger.log('Keyboard Interrupt - Bot Killed', Color.RED) exit() except: handle_crash(traceback.format_exc(), self.bot, reddit=self.reddit) Logger.log(traceback.format_exc(), Color.RED)
def handle_crash(stacktrace, reddit, database, message_dev): reset = False while not reset: SleepHandler.sleep(30) try: print('Trying to handle error \n\n' + stacktrace) reddit.reset() database.reset() if message_dev: print('Message Dev') reddit.send_message(accountinfo.bot_errors, accountinfo.username + " - Exception Handled", stacktrace) reset = True except: Logger.log('Failed to restart bot. Trying again in 30 seconds.', Color.RED)
def start(self): Logger.log('Starting bot...', Color.GREEN) while True: try: self.check_for_commands() if self.run: InboxHandler.read_inbox(self.database, self.reddit) subscriptions = self.database.get_subscriptions() Logger.log(str(len(subscriptions)) + ' Subs') matches = MatchFinder.find_matches(subscriptions, self.reddit, self.database) Logger.log(str(len(matches)) + ' Matches') MatchHandler.send_messages(self.reddit, self.database, matches) self.database.purge_old_matches() Logger.log(times.get_time_passed(self.start_time), Color.YELLOW) SleepHandler.sleep(20) except KeyboardInterrupt: Logger.log('Keyboard Interrupt - Bot killed', Color.RED) exit() except: handle_crash(traceback.format_exc(), self.reddit, self.database, True)
def read_inbox(database, reddit): Logger.log('Reading inbox...', Color.GREEN) unread = [] try: unread = reddit.get_unread() except: unread = [] num_messages = 0 for message in unread: num_messages += 1 username = str(message.author).lower() subject = inbox.format_subject(str(message.subject).lower()) body = str(message.body).lower() try: if username == 'reddit': InboxHandler.handle_message_from_reddit(reddit, message) elif subject == 'username mention': InboxHandler.handle_username_mention_message(reddit, message) elif subject == 'post reply': InboxHandler.handle_post_reply_message(reddit, message) elif subject in MessageLexer.feedback_keywords: InboxHandler.handle_feedback_message(reddit, message) elif subject in MessageLexer.help_keywords: InboxHandler.handle_help_message(database, message) else: m = MessageParser(message) valid = m.data[MessageParser.KEY_VALID] action = m.data[MessageParser.KEY_ACTION] payload = m.get_payload() Logger.log(json.dumps(m.data, 2), Color.MAGENTA) if valid and action == MessageParser.ACTION_STATISTICS: InboxHandler.handle_statistics_message(database, message) elif valid and action == MessageParser.ACTION_GET_SUBSCRIPTIONS: InboxHandler.handle_get_subscriptions_message(database, message) elif valid and action == MessageParser.ACTION_UNSUBSCRIBE_ALL: InboxHandler.handle_unsubscribe_all_message(database, message) elif valid and action == MessageParser.ACTION_UNSUBSCRIBE: InboxHandler.handle_unsubscribe_message(reddit, database, message) elif valid and action == MessageParser.ACTION_UNSUBSCRIBE_FROM_NUM: InboxHandler.handle_unsubscribe_from_num_message(database, message, payload) elif valid and action == MessageParser.ACTION_SUBSCRIBE: InboxHandler.handle_subscription_message(database, reddit, message, payload) elif valid and action == MessageParser.ACTION_EDIT: InboxHandler.handle_edit_message(database, message, payload) elif valid and action == MessageParser.ACTION_HELP: InboxHandler.handle_help_message(database, message) elif valid and action == MessageParser.ACTION_FEEDBACK: InboxHandler.handle_feedback_message(reddit, message) else: Logger.log('VALID: ' + str(valid)) Logger.log('REJECT:\nSubject:\t' + str(message.subject) + '\nBODY:\t\t' + str(message.body)) InboxHandler.handle_reject_message(reddit, message) except DatabaseHandlerException as ex: Logger.log(traceback.format_exc(), Color.RED) if ex.errorArgs == DatabaseHandlerException.INTEGRITY_ERROR: message.mark_as_read() reddit.send_message(accountinfo.developerusername, 'Integrity Error', 'SUBJECT: ' + str(inbox.format_subject(message.subject)) + '\n\n' + 'BODY:\n' + str(message.body)) except: Logger.log(traceback.format_exc(), Color.RED) reddit.send_message(accountinfo.developerusername, 'ERROR HANDLING MESSAGE - POTENTIALLY STUCK IN INBOX', 'SUBJECT: ' + str(message.subject) + '\n\n' + 'BODY:\n' + str(message.body)) SleepHandler.sleep(2) Logger.log(str(num_messages) + ' unread messages handled', Color.CYAN)
def read_inbox(database, reddit): Logger.log('Reading inbox...', Color.GREEN) unread = [] try: unread = reddit.get_unread() except: unread = [] num_messages = 0 for message in unread: num_messages += 1 username = str(message.author).lower() subject = inbox.format_subject(str(message.subject).lower()) body = str(message.body).lower() try: if username == 'reddit': InboxHandler.handle_message_from_reddit(reddit, message) elif subject == 'username mention': InboxHandler.handle_username_mention_message( reddit, message) elif subject == 'post reply': InboxHandler.handle_post_reply_message(reddit, message) elif subject in MessageLexer.feedback_keywords: InboxHandler.handle_feedback_message(reddit, message) elif subject in MessageLexer.help_keywords: InboxHandler.handle_help_message(database, message) else: m = MessageParser(message) action = m.data[MessageParser.KEY_ACTION] error = m.data[MessageParser.KEY_ERROR] payload = m.get_payload() Logger.log(json.dumps(m.data, 2), Color.MAGENTA) if error: Logger.log('REJECT:\n' + 'Error:\t' + str(error) + '\n' + 'Subject:\t' + str(message.subject) + '\n' + 'Body:\t\t' + str(message.body)) InboxHandler.handle_reject_message( reddit, message, error) elif action == MessageParser.ACTION_STATISTICS: InboxHandler.handle_statistics_message( database, message) elif action == MessageParser.ACTION_GET_SUBSCRIPTIONS: InboxHandler.handle_get_subscriptions_message( database, message) elif action == MessageParser.ACTION_UNSUBSCRIBE_ALL: InboxHandler.handle_unsubscribe_all_message( database, message) elif action == MessageParser.ACTION_UNSUBSCRIBE: InboxHandler.handle_unsubscribe_message( reddit, database, message) elif action == MessageParser.ACTION_UNSUBSCRIBE_FROM_NUM: InboxHandler.handle_unsubscribe_from_num_message( database, message, payload) elif action == MessageParser.ACTION_SUBSCRIBE: InboxHandler.handle_subscription_message( database, reddit, message, payload) elif action == MessageParser.ACTION_EDIT: InboxHandler.handle_edit_message( database, message, payload) elif action == MessageParser.ACTION_HELP: InboxHandler.handle_help_message(database, message) elif action == MessageParser.ACTION_FEEDBACK: InboxHandler.handle_feedback_message(reddit, message) except DatabaseHandlerException as ex: Logger.log(traceback.format_exc(), Color.RED) if ex.errorArgs == DatabaseHandlerException.INTEGRITY_ERROR: message.mark_read() reddit.send_message( accounts['developer']['username'], 'Integrity Error', 'SUBJECT: ' + str(inbox.format_subject(message.subject)) + '\n\n' + 'BODY:\n' + str(message.body)) except: Logger.log(traceback.format_exc(), Color.RED) reddit.send_message( accounts['developer']['username'], 'ERROR HANDLING MESSAGE - POTENTIALLY STUCK IN INBOX', 'Should NOT be seeing this message anymore hopefully...\t \n' + 'AUTHOR: /u/' + str(message.author) + '\t \n' + 'SUBJECT: ' + str(message.subject) + '\t \n' + 'BODY:\n' + str(message.body)) SleepHandler.sleep(2) Logger.log(str(num_messages) + ' unread messages handled', Color.CYAN)