Advertisement
Guest User

Untitled

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