Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from praw import *
- #------------------------------------------------------------------------------
- #==============================================================================
- # Here are all the various variables that you can change to alter the behavior of the bot
- # (what subreddits it scans, what message it responds with, etc.)
- # A list of subreddits to scan for the list of trigger phrases:
- subreddits = ["mylittlepony", "mlplounge"]
- # Add any other subreddits you want the bot to look in
- # make sure to enclose them in quotation marks and separate them with commas
- # Here's the message that the bot will reply to comments with. \n indicates a newline character (a hit of the enter key)
- message = (" [](/1c) [It's true some days are dark and lonely](https://www.youtube.com/watch?v=mNrXMOSkBas) \n \n" +
- "And maybe you feel sad \n \n" +
- "But Pinkie will be there to show you that it isn't that bad \n \n" +
- "There's one thing that makes me happy and makes my whole life worthwhile \n \n" +
- "And that's when I talk to my friends and get them to smile! \n \n --- \n \n" +
- "^(I am a bot. If you feel that you need to talk to somebody stop by /r/mylittlesupportgroup or PM /u/RavenclawDash.)\n \n
- "^feeling ^suicidal? ^[find help here](http://www.reddit.com/r/SWResources/comments/17gu7g/hotline_numbers/)")
- # A list of the different phrases to look for
- trigger_phrases = ["I'm feeling down",
- "I'm feeling pretty down",
- "I feel depressed",
- "i feel down",
- "i hate my life",
- "I feel down",
- "I hate my life",
- "I'(m|ve) ha(d|ving) a (bad|crappy|shi..y|terrible) (day|month|week)",
- "I feel sad",
- "i feel sad",
- "I think I need a hug",
- "i think i need a hug",
- "i need some cheering up",
- "I need some cheering up",
- "This is my last post",
- "this is my last post",
- "I'm ending it all",
- "I'm ending it all tonight",
- "I'm ending it all today",
- "im ending it all",
- "im ending it all today",
- "im ending it all tonight",
- "i hate my life",
- "I hate my life",
- "my life sucks",
- "My life sucks",
- "kill myself",
- "end it all",
- "ending it all",
- "suicide",
- "goodbye everyone",
- "Goodbye everyone",
- "I hate myself",
- "i hate myself",
- "I'm worthless",
- "Im worthless",
- "I am worthless",
- "im worthless",
- "i am worthless",
- "i'm worthless",
- "I'm sad",
- "I am sad",
- "i am sad",
- "i'm sad"]
- # The user to whom the bot will send a message when it is replied to or sent a pm
- controlling_user = "RavenclawDash"
- # The username of whatever account you want the bot to use
- bot_name = "cheer-up-bot"
- # The password for the account that you want the bot to use
- bot_password = "*****"
- # A useragent name provided so that reddit knows who is responsible for the bot
- bot_user_agent_name = "/u/RavenclawDash's Cheer Up Bot"
- #==============================================================================
- # Everything below this is what makes the bot function.
- # Unless you are familiar with both python and PRAW I do not recommend you try and change any of it.
- #------------------------------------------------------------------------------
- # This is just a function to create the bot itself, called at the beginning of the program
- def create_bot(user_name, password, user_agent):
- bot = Reddit(user_agent = user_agent)
- if user_name is not None:
- bot.login(user_name, password)
- return bot
- #------------------------------------------------------------------------------
- # Scans the passed in submission, scanning the submission itself if it is a self post
- # then calling scan_comment on each top level comment in the submission
- def scan_submission(submission):
- if submission.is_self and submission not in seen and bot_name is not str(submission.author):
- for phrase in trigger_phrases:
- if phrase in submission.selftext:
- reply_to_submission(submission)
- break
- for comment in submission.comments:
- scan_comment(submission, comment)
- #------------------------------------------------------------------------------
- # Scans the passed in comment
- # If the comment contains any of the trigger phrases listed above and was not posted by the bot itself, calls reply_to_comment() on it
- # if the comment has replies, recursively scans those comments with scan_comment() as well
- seen = []
- def scan_comment(submission, comment):
- if hasattr(comment, "body"):
- for phrase in trigger_phrases:
- if phrase in comment.body and comment not in seen and bot_name is not str(comment.author):
- reply_to_comment(submission, comment)
- break
- if hasattr(comment, "replies") and comment.replies and comment not in seen and bot_name is not str(comment.author):
- try:
- replies = comment.replies
- if replies:
- for c in comment.replies:
- if bot_name is not str(c.author):
- scan_comment(submission, c)
- except:
- pass
- if hasattr(comment, "comments") and comment.comments and comment not in seen and bot_name is not str(comment.author):
- for c in comment.comments():
- if bot_name is not str(c.author):
- scan_comment(submission, c)
- #------------------------------------------------------------------------------
- def reply_to_comment(submission, comment):
- # Replies to the comment with the preferred message
- comment.reply(message)
- # Adds the comment to the seen list so that the bot won't reply to it multiple times
- seen.append(comment)
- # Prints out in the window when a comment is posted
- print("Comment reply posted at: ")
- print(submission)
- print(comment.body)
- print(comment.author.name)
- print("")
- #------------------------------------------------------------------------------
- def reply_to_submission(submission):
- # Adds a comment to the submission with the preferred message
- submission.add_comment(message)
- # Adds the submission to the seen list so that the bot won't keep adding comments to it over and over
- seen.append(submission)
- # Prints out in the window when a comment is posted
- print("Submission reply posted at: ")
- print(submission)
- print(submission.author)
- print("")
- #------------------------------------------------------------------------------
- # When you run the program, this code will execute
- # The while True part means that it will continue to run until it either encounters an error or you stop the program
- r = create_bot(bot_name, bot_password, bot_user_agent_name)
- bot_obect = r.get_redditor(bot_name)
- while True:
- for sub_name in subreddits:
- subreddit = r.get_subreddit(sub_name)
- submissions = subreddit.get_new(limit=20) # This gets the last n submissions to whichever subreddit it is processing at the moment, currently it is set to get the last 20 posts in the new queue
- for submission in submissions:
- scan_submission(submission)
- for msg in r.get_unread(limit=5):
- # Here's the message that the bot will send when it detects a reply or PM
- reply_message = ("/u/" + str(msg.author) + " has replied or sent a message to /u/" + bot_name + " : \n \n >" + msg.body)
- r.get_redditor(controlling_user).send_message("Bot Reply or PM", reply_message)
- print(reply_message)
- print("")
- print("")
- msg.mark_as_read()
Add Comment
Please, Sign In to add comment