Guest User

cheer up bot v2

a guest
Jul 30th, 2014
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.27 KB | None | 0 0
  1. from praw import *
  2.  
  3. #------------------------------------------------------------------------------
  4. #==============================================================================
  5.  
  6. # Here are all the various variables that you can change to alter the behavior of the bot
  7. # (what subreddits it scans, what message it responds with, etc.)
  8.  
  9. # A list of subreddits to scan for the list of trigger phrases:
  10. subreddits = ["mylittlepony", "mlplounge"]
  11. # Add any other subreddits you want the bot to look in
  12. # make sure to enclose them in quotation marks and separate them with commas
  13.  
  14. # Here's the message that the bot will reply to comments with. \n indicates a newline character (a hit of the enter key)
  15. message = (" [](/1c) [It's true some days are dark and lonely](https://www.youtube.com/watch?v=mNrXMOSkBas) \n \n" +
  16. "And maybe you feel sad \n \n" +
  17. "But Pinkie will be there to show you that it isn't that bad \n \n" +
  18. "There's one thing that makes me happy and makes my whole life worthwhile \n \n" +
  19. "And that's when I talk to my friends and get them to smile! \n \n --- \n \n" +
  20. "^(I am a bot. If you feel that you need to talk to somebody stop by /r/mylittlesupportgroup or PM /u/RavenclawDash.)\n \n
  21. "^feeling ^suicidal? ^[find help here](http://www.reddit.com/r/SWResources/comments/17gu7g/hotline_numbers/)")
  22.  
  23. # A list of the different phrases to look for
  24. trigger_phrases = ["I'm feeling down",
  25. "I'm feeling pretty down",
  26. "I feel depressed",
  27. "i feel down",
  28. "i hate my life",
  29. "I feel down",
  30. "I hate my life",
  31. "I'(m|ve) ha(d|ving) a (bad|crappy|shi..y|terrible) (day|month|week)",
  32. "I feel sad",
  33. "i feel sad",
  34. "I think I need a hug",
  35. "i think i need a hug",
  36. "i need some cheering up",
  37. "I need some cheering up",
  38. "This is my last post",
  39. "this is my last post",
  40. "I'm ending it all",
  41. "I'm ending it all tonight",
  42. "I'm ending it all today",
  43. "im ending it all",
  44. "im ending it all today",
  45. "im ending it all tonight",
  46. "i hate my life",
  47. "I hate my life",
  48. "my life sucks",
  49. "My life sucks",
  50. "kill myself",
  51. "end it all",
  52. "ending it all",
  53. "suicide",
  54. "goodbye everyone",
  55. "Goodbye everyone",
  56. "I hate myself",
  57. "i hate myself",
  58. "I'm worthless",
  59. "Im worthless",
  60. "I am worthless",
  61. "im worthless",
  62. "i am worthless",
  63. "i'm worthless",
  64. "I'm sad",
  65. "I am sad",
  66. "i am sad",
  67. "i'm sad"]
  68.  
  69. # The user to whom the bot will send a message when it is replied to or sent a pm
  70. controlling_user = "RavenclawDash"
  71.  
  72. # The username of whatever account you want the bot to use
  73. bot_name = "cheer-up-bot"
  74.  
  75. # The password for the account that you want the bot to use
  76. bot_password = "*****"
  77.  
  78. # A useragent name provided so that reddit knows who is responsible for the bot
  79. bot_user_agent_name = "/u/RavenclawDash's Cheer Up Bot"
  80.  
  81. #==============================================================================
  82.  
  83. # Everything below this is what makes the bot function.
  84. # Unless you are familiar with both python and PRAW I do not recommend you try and change any of it.
  85.  
  86. #------------------------------------------------------------------------------
  87.  
  88. # This is just a function to create the bot itself, called at the beginning of the program
  89.  
  90. def create_bot(user_name, password, user_agent):
  91. bot = Reddit(user_agent = user_agent)
  92. if user_name is not None:
  93. bot.login(user_name, password)
  94. return bot
  95.  
  96. #------------------------------------------------------------------------------
  97.  
  98. # Scans the passed in submission, scanning the submission itself if it is a self post
  99. # then calling scan_comment on each top level comment in the submission
  100.  
  101. def scan_submission(submission):
  102. if submission.is_self and submission not in seen and bot_name is not str(submission.author):
  103. for phrase in trigger_phrases:
  104. if phrase in submission.selftext:
  105. reply_to_submission(submission)
  106. break
  107. for comment in submission.comments:
  108. scan_comment(submission, comment)
  109.  
  110. #------------------------------------------------------------------------------
  111.  
  112. # Scans the passed in comment
  113. # 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
  114. # if the comment has replies, recursively scans those comments with scan_comment() as well
  115.  
  116. seen = []
  117. def scan_comment(submission, comment):
  118. if hasattr(comment, "body"):
  119. for phrase in trigger_phrases:
  120. if phrase in comment.body and comment not in seen and bot_name is not str(comment.author):
  121. reply_to_comment(submission, comment)
  122. break
  123. if hasattr(comment, "replies") and comment.replies and comment not in seen and bot_name is not str(comment.author):
  124. try:
  125. replies = comment.replies
  126. if replies:
  127. for c in comment.replies:
  128. if bot_name is not str(c.author):
  129. scan_comment(submission, c)
  130. except:
  131. pass
  132. if hasattr(comment, "comments") and comment.comments and comment not in seen and bot_name is not str(comment.author):
  133. for c in comment.comments():
  134. if bot_name is not str(c.author):
  135. scan_comment(submission, c)
  136.  
  137. #------------------------------------------------------------------------------
  138.  
  139. def reply_to_comment(submission, comment):
  140.  
  141. # Replies to the comment with the preferred message
  142. comment.reply(message)
  143.  
  144. # Adds the comment to the seen list so that the bot won't reply to it multiple times
  145. seen.append(comment)
  146.  
  147. # Prints out in the window when a comment is posted
  148. print("Comment reply posted at: ")
  149. print(submission)
  150. print(comment.body)
  151. print(comment.author.name)
  152. print("")
  153.  
  154. #------------------------------------------------------------------------------
  155.  
  156. def reply_to_submission(submission):
  157.  
  158. # Adds a comment to the submission with the preferred message
  159. submission.add_comment(message)
  160.  
  161. # Adds the submission to the seen list so that the bot won't keep adding comments to it over and over
  162. seen.append(submission)
  163.  
  164. # Prints out in the window when a comment is posted
  165. print("Submission reply posted at: ")
  166. print(submission)
  167. print(submission.author)
  168. print("")
  169.  
  170. #------------------------------------------------------------------------------
  171.  
  172. # When you run the program, this code will execute
  173. # The while True part means that it will continue to run until it either encounters an error or you stop the program
  174. r = create_bot(bot_name, bot_password, bot_user_agent_name)
  175. bot_obect = r.get_redditor(bot_name)
  176.  
  177. while True:
  178. for sub_name in subreddits:
  179. subreddit = r.get_subreddit(sub_name)
  180. 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
  181. for submission in submissions:
  182. scan_submission(submission)
  183. for msg in r.get_unread(limit=5):
  184. # Here's the message that the bot will send when it detects a reply or PM
  185. reply_message = ("/u/" + str(msg.author) + " has replied or sent a message to /u/" + bot_name + " : \n \n >" + msg.body)
  186. r.get_redditor(controlling_user).send_message("Bot Reply or PM", reply_message)
  187. print(reply_message)
  188. print("")
  189. print("")
  190. msg.mark_as_read()
Add Comment
Please, Sign In to add comment