def respond_to_edit_request(self, message): """ Responds to an edit request. The bot will parse the body of the message, looking for verse quotations. These will replace the quotations that were placed in the original response to the user. Once the comment has been successfully edited, the bot then sends a message to the user letting them know that their verse quotations have been updated. """ try: comment_url = message.body[1:message.body.find("}")] comment = self.r.get_submission(comment_url) except: try: message.reply("An error occurred while processing your edit request. " "Please make sure that you do not modify the subject line of your message to %s." % REDDIT_USERNAME) except requests.exceptions.ConnectionError: pass return if message.author == comment.author and comment: verses = find_verses(message.body) if verses is not None: for reply in comment.comments[0].replies: if str(reply.author) == REDDIT_USERNAME: try: self.log.info("%s has requested a comment edit..." % comment.author) link = reply.permalink[24:comment.permalink.find("/", 24)] response = Response(message, self.parser, comment_url) for verse in verses: book_name = books.get_book(verse[0]) if book_name is not None: v = Verse(book_name, # Book verse[1], # Chapter verse[3], # Translation message.author, # User link, # Subreddit verse[2]) # Verse if not response.is_duplicate_verse(v): response.add_verse(v) if len(response.verse_list) != 0: message_response = ("*^This ^comment ^has ^been ^edited ^by ^%s.*\n\n" % message.author) message_response += response.construct_message() if message_response is not None: self.log.info("Editing %s's comment with updated verse quotations..." % message.author) database.remove_invalid_statistics(reply.body, link) reply.edit(message_response) database.update_db_stats(response.verse_list) try: message.reply("[Your triggered %s response](%s) has been successfully edited to reflect" " your updated quotations." % (REDDIT_USERNAME, comment_url)) except requests.exceptions.ConnectionError: pass break except: raise self.log.warning("Comment edit failed. Will try again later...") break
def respond_to_username_mention(self, msg): """ Responds to a username mention. This could either contain one or more valid Bible verse quotation requests, or it could simply be a username mention without any valid Bible verses. If there are valid Bible verses, VerseBot generates a response that contains the text from these quotations. Otherwise, the message is forwarded to the VerseBot admin for review. :param msg: The message that contains the username mention """ verses = find_verses(msg.body) if verses is not None: response = Response(msg, self.parser) for verse in verses: book_name = books.get_book(verse[0]) if book_name is not None: v = Verse( book_name, # Book verse[1], # Chapter verse[3], # Translation msg.author, # User msg.subreddit.display_name, # Subreddit verse[2]) # Verse if not response.is_duplicate_verse(v): response.add_verse(v) if len(response.verse_list) != 0: message_response = response.construct_message() if message_response is not None: self.log.info("Replying to %s with verse quotations..." % msg.author) try: msg.reply(message_response) database.update_db_stats(response.verse_list) database.increment_comment_count() except praw.errors.Forbidden: # This message is unreachable. pass except praw.errors.APIException as err: if err.error_type in ("TOO_OLD", "DELETED_LINK", "DELETED_COMMENT"): self.log.warning("An error occurred while replying" " with error_type %s." % err.error_type) else: self.log.info("No verses found in this message. " "Forwarding to /u/%s..." % VERSEBOT_ADMIN) try: self.r.send_message( VERSEBOT_ADMIN, "Forwarded VerseBot Message", "%s\n\n[[Link to Original Message](%s)]" % (msg.body, msg.permalink)) except requests.ConnectionError: pass
def respond_to_username_mention(self, msg): """ Responds to a username mention. This could either contain one or more valid Bible verse quotation requests, or it could simply be a username mention without any valid Bible verses. If there are valid Bible verses, VerseBot generates a response that contains the text from these quotations. Otherwise, the message is forwarded to the VerseBot admin for review. :param msg: The message that contains the username mention """ verses = find_verses(msg.body) if verses is not None: response = Response(msg, self.parser) for verse in verses: book_name = books.get_book(verse[0]) if book_name is not None: v = Verse(book_name, # Book verse[1], # Chapter verse[3], # Translation msg.author, # User msg.subreddit.display_name, # Subreddit verse[2]) # Verse if not response.is_duplicate_verse(v): response.add_verse(v) if len(response.verse_list) != 0: message_response = response.construct_message() if message_response is not None: self.log.info("Replying to %s with verse quotations..." % msg.author) try: msg.reply(message_response) database.update_db_stats(response.verse_list) database.increment_comment_count() except praw.errors.Forbidden: # This message is unreachable. pass except praw.errors.APIException as err: if err.error_type in ("TOO_OLD", "DELETED_LINK", "DELETED_COMMENT"): self.log.warning("An error occurred while replying" " with error_type %s." % err.error_type) else: self.log.info("No verses found in this message. " "Forwarding to /u/%s..." % VERSEBOT_ADMIN) try: self.r.send_message(VERSEBOT_ADMIN, "Forwarded VerseBot Message", "%s\n\n[[Link to Original Message](%s)]" % (msg.body, msg.permalink)) except requests.ConnectionError: pass
async def send_verses(self, body, verses, user, channel, websocket): if verses is not None: response = Response(body, self.parser) for verse in verses: book_name = books.get_book(verse[0]) if book_name is not None: v = Verse(book_name, verse[1], verse[3], user, channel, verse[2]) if not response.is_duplicate_verse(v): response.add_verse(v) if len(response.verse_list) != 0: message_response = response.construct_message() if message_response is not None: await self.send_message(message_response, channel, websocket) else: pass
def respond_to_edit_request(self, msg): """ Responds to an edit request. The bot will parse the body of the message, looking for verse quotations. These will replace the quotations that were placed in the original response to the user. Once the comment has been successfully edited, the bot then sends a message to the user letting them know that their verse quotations have been updated. :param msg: The message that contains the edit request """ try: comment_url = re.search("\{(.*?)\}", msg.body).group(1) comment = self.r.get_submission(comment_url) except: try: msg.reply("An error occurred while processing your edit " "request. Please make sure that you do not modify " "the subject line of your message to %s." % REDDIT_USERNAME) except requests.ConnectionError: pass return if msg.author == comment.author and comment: verses = find_verses(msg.body) if verses is not None: for reply in comment.comments[0].replies: if str(reply.author) == REDDIT_USERNAME: try: self.log.info( "%s has requested a comment edit..." % comment.author) sub = re.search("/r/(.*?)/", comment_url).group(1) response = Response(msg, self.parser, comment_url) for verse in verses: book_name = books.get_book(verse[0]) if book_name is not None: v = Verse( book_name, # Book verse[1], # Chapter verse[3], # Translation msg.author, # User sub, # Subreddit verse[2]) # Verse if not response.is_duplicate_verse(v): response.add_verse(v) if len(response.verse_list) != 0: msg_response = ("*^This ^comment ^has ^been " "^edited ^by ^%s.*\n\n" % msg.author) msg_response += response.construct_message() if msg_response is not None: self.log.info( "Editing %s's comment with " "updated verse quotations..." % msg.author) database.remove_invalid_stats( reply.body, sub) reply.edit(msg_response) database.update_db_stats( response.verse_list) try: msg.reply( "[Your triggered %s " "response](%s) has been " "successfully edited to " "reflect your updated " "quotations." % (REDDIT_USERNAME, comment_url)) except requests.ConnectionError: pass break except: self.log.warning("Comment edit failed. " "Will try again later...")
def respond_to_edit_request(self, message): """ Responds to an edit request. The bot will parse the body of the message, looking for verse quotations. These will replace the quotations that were placed in the original response to the user. Once the comment has been successfully edited, the bot then sends a message to the user letting them know that their verse quotations have been updated. """ try: comment_url = message.body[1:message.body.find("}")] comment = self.r.get_submission(comment_url) except: try: message.reply( "An error occurred while processing your edit request. " "Please make sure that you do not modify the subject line of your message to %s." % REDDIT_USERNAME) except requests.exceptions.ConnectionError: pass return if message.author == comment.author and comment: verses = find_verses(message.body) if verses is not None: for reply in comment.comments[0].replies: if str(reply.author) == REDDIT_USERNAME: try: self.log.info( "%s has requested a comment edit..." % comment.author) link = reply.permalink[24:comment.permalink. find("/", 24)] response = Response(message, self.parser, comment_url) for verse in verses: book_name = books.get_book(verse[0]) if book_name is not None: v = Verse( book_name, # Book verse[1], # Chapter verse[3], # Translation message.author, # User link, # Subreddit verse[2]) # Verse if not response.is_duplicate_verse(v): response.add_verse(v) if len(response.verse_list) != 0: message_response = ( "*^This ^comment ^has ^been ^edited ^by ^%s.*\n\n" % message.author) message_response += response.construct_message( ) if message_response is not None: self.log.info( "Editing %s's comment with updated verse quotations..." % message.author) database.remove_invalid_statistics( reply.body, link) reply.edit(message_response) database.update_db_stats( response.verse_list) try: message.reply( "[Your triggered %s response](%s) has been successfully edited to reflect" " your updated quotations." % (REDDIT_USERNAME, comment_url)) except requests.exceptions.ConnectionError: pass break except: raise self.log.warning( "Comment edit failed. Will try again later...") break
def respond_to_edit_request(self, msg): """ Responds to an edit request. The bot will parse the body of the message, looking for verse quotations. These will replace the quotations that were placed in the original response to the user. Once the comment has been successfully edited, the bot then sends a message to the user letting them know that their verse quotations have been updated. :param msg: The message that contains the edit request """ try: comment_url = re.search("\{(.*?)\}", msg.body).group(1) comment = self.r.get_submission(comment_url) except: try: msg.reply("An error occurred while processing your edit " "request. Please make sure that you do not modify " "the subject line of your message to %s." % REDDIT_USERNAME) except requests.ConnectionError: pass return if msg.author == comment.author and comment: verses = find_verses(msg.body) if verses is not None: for reply in comment.comments[0].replies: if str(reply.author) == REDDIT_USERNAME: try: self.log.info("%s has requested a comment edit..." % comment.author) sub = re.search("/r/(.*?)/", comment_url).group(1) response = Response(msg, self.parser, comment_url) for verse in verses: book_name = books.get_book(verse[0]) if book_name is not None: v = Verse(book_name, # Book verse[1], # Chapter verse[3], # Translation msg.author, # User sub, # Subreddit verse[2]) # Verse if not response.is_duplicate_verse(v): response.add_verse(v) if len(response.verse_list) != 0: msg_response = ("*^This ^comment ^has ^been " "^edited ^by ^%s.*\n\n" % msg.author) msg_response += response.construct_message() if msg_response is not None: self.log.info("Editing %s's comment with " "updated verse quotations..." % msg.author) database.remove_invalid_stats(reply.body, sub) reply.edit(msg_response) database.update_db_stats( response.verse_list) try: msg.reply("[Your triggered %s " "response](%s) has been " "successfully edited to " "reflect your updated " "quotations." % (REDDIT_USERNAME, comment_url)) except requests.ConnectionError: pass break except: self.log.warning("Comment edit failed. " "Will try again later...")