Advertisement
Guest User

Untitled

a guest
Sep 7th, 2017
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.26 KB | None | 0 0
  1. import praw
  2. import config
  3. import random
  4. import os
  5. import time
  6.  
  7. def bot_login():
  8. print "Logging in..."
  9. r = praw.Reddit(username = config.username,
  10. password = config.password,
  11. client_id = config.client_id,
  12. client_secret = config.client_secret,
  13. user_agent = "kemonomimi bot v0.5")
  14. print "Logged in!"
  15.  
  16. return r
  17.  
  18. def run_bot(r, comments_replied_to, catgirls):
  19. #print comments_replied_to
  20. #print catgirls
  21. print "Searching last 25 comments..."
  22.  
  23. testsub = r.subreddit("kemonomimicheerupbot")
  24.  
  25. subreddits = r.subreddit('+kemonomimicheerupbot+anime_irl+animemes')
  26.  
  27. #print subreddits
  28.  
  29. trigger_phrases = ["im sad", "i'm sad", "cheer me up"]
  30.  
  31. #print trigger_phrases
  32.  
  33. ignored_users = [r.user.me(), "thiscatmightcheeryou"]
  34.  
  35. #print ignored_users
  36.  
  37. comment_reply = ("[Here](" + random.choice(catgirls) + ") is a "
  38. "picture of a catgirl! Hopefully this will cheer you up!"
  39. "\n\n"
  40. "---"
  41. "\n\n"
  42. "I am a bot. For more info on me and how to use me, see r/KemonomimiCheerUpBot "
  43. "Have I gone rogue? Reply \"!SHUTDOWN\" to stop me.")
  44.  
  45. xenonauts_reply = ("[Here](https://i.imgur.com/RcrLkpe.jpg) is a "
  46. "picture of a catgirl Sagiri! Hopefully this will cheer you up!"
  47. "\n\n"
  48. "---"
  49. "\n\n"
  50. "I am a bot. For more info on me and how to use me, see r/KemonomimiCheerUpBot "
  51. "Have I gone rogue? Reply \"!SHUTDOWN\" to stop me.")
  52.  
  53. #checks comments in above listed subreddit for triggers
  54.  
  55. for comment in subreddits.comments(limit=25):
  56. for trigger in (trigger_phrases):
  57. if (trigger in comment.body.lower() and \
  58. comment.id not in comments_replied_to and \
  59. comment.author != (ignored_users, "xenonauts")):
  60.  
  61. #print "Im sad found in comment" + comment.id
  62.  
  63. comment.reply(comment_reply)
  64.  
  65. print "replied to comment " + comment.id
  66.  
  67. comments_replied_to.append(comment.id)
  68.  
  69. with open("comments_replied_to.txt", "a") as f:
  70. f.write(comment.id + "\n")
  71.  
  72. #Patiently awaiting the return of u/xenonauts...
  73.  
  74. for trigger in (trigger_phrases):
  75. if (trigger in comment.body.lower() and \
  76. comment.id not in comments_replied_to and \
  77. comment.author == (xenonauts)):
  78.  
  79. print "XENONAUTS IS BACK!!! REPLIED TO COMMENT" + comment.id
  80.  
  81. comment.reply(xenonauts_reply)
  82.  
  83. print "replied to comment " + comment.id
  84.  
  85. comments_replied_to.append(comment.id)
  86.  
  87. with open("comments_replied_to.txt", "a") as f:
  88. f.write(comment.id + "\n")
  89.  
  90. #checks inbox mentions for triggers
  91.  
  92. print "searching mentions..."
  93.  
  94. for mention in r.inbox.mentions(limit=5):
  95. if trigger in (trigger_phrases):
  96. if (trigger in mention.body.lower() and \
  97. mention.id not in comments_replied_to and \
  98. mention.author != (ignored_users, "xenonauts")):
  99.  
  100. print "Mentioned in comment " + mention.id
  101.  
  102. mention.reply(comment_reply)
  103.  
  104. print "replied to mention " + mention.id
  105.  
  106. comments_replied_to.append(comment.id)
  107.  
  108. with open("comments_replied_to.txt", "a") as f:
  109. f.write(comment.id + "\n")
  110.  
  111. if trigger in (trigger_phrases):
  112. if (trigger in mention.body.lower() and \
  113. mention.id not in comments_replied_to and \
  114. mention.author == ("xenonauts")):
  115.  
  116. print "Mentioned in xenonauts' comment " + mention.id
  117.  
  118. mention.reply(xenonauts_reply)
  119.  
  120. print "replied to xenonauts' mention " + mention.id
  121.  
  122. comments_replied_to.append(comment.id)
  123.  
  124. with open("comments_replied_to.txt", "a") as f:
  125. f.write(comment.id + "\n")
  126.  
  127. #checks inbox replies for trigger phrases
  128.  
  129. print "searching inbox..."
  130.  
  131. for reply in r.inbox.comment_replies(limit=3):
  132. if ("good bot" in reply.body.lower() and reply.id not in comments_replied_to):
  133.  
  134. #print "Good bot found in reply " + reply.id
  135.  
  136. reply.reply("[Thank You! :)](https://i.imgur.com/P3GRavv.gifv)")
  137.  
  138. print "Thanked reply " + reply.id
  139.  
  140. comments_replied_to.append(reply.id)
  141.  
  142. with open("comments_replied_to.txt", "a") as f:
  143. f.write(reply.id+ "\n")
  144.  
  145. if ("bad bot" in reply.body.lower() and reply.id not in comments_replied_to):
  146.  
  147. print "TRASH WAIFU FOUND!!"
  148.  
  149. reply.reply("[Your waifu](merriam-webster.com/dictionary/trash)")
  150.  
  151. comments_replied_to.append(reply.id)
  152.  
  153. with open("comments_replied_to.txt", "a") as f:
  154. f.write(reply.id+ "\n")
  155.  
  156. for waifu_trigger in ["who is your waifu", "whos your waifu", "who's your waifu"]:
  157. if (waifu_trigger in reply.body.lower() and reply.id not in comments_replied_to):
  158.  
  159. reply.reply("[Fixed Artillery-San](https://i.imgur.com/qrsKQCH.jpg)")
  160.  
  161. print "bragged about waifu"
  162.  
  163. comments_replied_to.append(reply.id)
  164.  
  165. with open("comments_replied_to.txt", "a") as f:
  166. f.write(reply.id+ "\n")
  167.  
  168. #This is to allow a shutdown when necessary.
  169. #Times this has prevented to robot uprising: 1
  170.  
  171. #print "Checking for SHUTDOWN command"
  172. if ("!SHUTDOWN" in reply.body and reply.id not in comments_replied_to):
  173. print "!SHUTDOWN issued by " + reply.author
  174. reply.repl("")
  175.  
  176. print "Time for a cat-nap!..."
  177.  
  178. time.sleep(60)
  179.  
  180. #Load catgirls from a file (defaults to catgirls.txt)
  181. def get_catgirls(fname='catgirls.txt'):
  182.  
  183. print "collecting catgirls..."
  184.  
  185. if not os.path.isfile(fname):
  186. raise FileNotFoundError("Can't get catgirls from " + fname)
  187.  
  188. catgirls = []
  189.  
  190. with open(fname) as catgirls_file:
  191. catgirls_contents = catgirls_file.read()
  192.  
  193. catgirls = catgirls_contents.split("\n")
  194. catgirls = filter(None, catgirls)
  195.  
  196. catgirls = [catgirl.strip() for catgirl in catgirls]
  197.  
  198. return catgirls
  199.  
  200. #reads file of comments bot has replied to
  201. def get_saved_comments():
  202.  
  203. print "reading comments_replied_to..."
  204.  
  205. if not os.path.isfile("comments_replied_to.txt"):
  206. comments_replied_to = []
  207. else:
  208. with open("comments_replied_to.txt", "r") as f:
  209. comments_replied_to = f.read()
  210. comments_replied_to = comments_replied_to.split("\n")
  211. comments_replied_to = filter(None, comments_replied_to)
  212.  
  213. return comments_replied_to
  214.  
  215.  
  216. if __name__ == '__main__':
  217.  
  218. r = bot_login()
  219. catgirls = get_catgirls()
  220. comments_replied_to = get_saved_comments()
  221.  
  222. while True:
  223. run_bot(r, comments_replied_to, catgirls)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement