Advertisement
Guest User

Untitled

a guest
Sep 5th, 2017
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.87 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.3.2")
  14.     print "Logged in!"
  15.  
  16.     return r
  17.  
  18. def run_bot(r, comments_replied_to, catgirls):
  19.     #print catgirls
  20.     print "Searching last 25 comments..."
  21.  
  22.     for comment in r.subreddit('KemonomimiCheerUpBot+anime_irl+animemes').comments(limit=25):
  23.         if (("!im sad" in comment.body.lower())  and \
  24.             comment.id not in comments_replied_to and \
  25.             comment.author != r.user.me()):
  26.  
  27.             # print "!im sad found in comment" + comment.id
  28.  
  29.             comment.reply("[Here](" + random.choice(catgirls) + ") is a "
  30.                           "picture of a catgirl! Hopefully this will cheer you up!"
  31.                           "\n\n"
  32.                           "---"
  33.                           "\n\n"
  34.                           "I am a bot. For more info on me and how to use me, see r/KemonomimiCheerUpBot")
  35.  
  36.             print "replied to comment " + comment.id
  37.  
  38.             comments_replied_to.append(comment.id)
  39.  
  40.             with open("comments_replied_to.txt", "a") as f:
  41.                 f.write(comment.id + "\n")
  42.  
  43.     print "searching inbox..."
  44.  
  45.     for reply in r.inbox.comment_replies(limit=3):
  46.         if ("good bot" in reply.body.lower() and reply.id not in comments_replied_to):
  47.  
  48.             #print "Good bot found in reply " + reply.id
  49.  
  50.             reply.reply("[Thank You! :)](https://i.imgur.com/P3GRavv.gifv)")
  51.  
  52.             print "Thanked reply " + reply.id
  53.  
  54.             comments_replied_to.append(reply.id)
  55.  
  56.             with open("comments_replied_to.txt", "a") as f:
  57.                 f.write(reply.id+ "\n")
  58.  
  59.         if ("bad bot" in reply.body.lower() and reply.id not in comments_replied_to):
  60.  
  61.             print "TRASH WAIFU FOUND!!"
  62.  
  63.             reply.reply("[Your waifu](https://www.merriam-webster.com/dictionary/trash)")
  64.  
  65.             comments_replied_to.append(reply.id)
  66.  
  67.             with open("comments_replied_to.txt", "a") as f:
  68.                 f.write(reply.id+ "\n")
  69.  
  70.         if ("who is your waifu" in reply.body.lower() and reply.id not in comments_replied_to):
  71.  
  72.             reply.reply("[Fixed Artillery-San](https://i.imgur.com/Egekous.jpg)")
  73.  
  74.             print "bragged about waifu"
  75.  
  76.             comments_replied_to.append(reply.id)
  77.  
  78.             with open("comments_replied_to.txt", "a") as f:
  79.                 f.write(reply.id+ "\n")
  80.  
  81.  
  82.    
  83.     print "Time for a cat-nap!..."
  84.  
  85.     time.sleep(60)
  86.  
  87. # Load catgirls from a file (defaults to catgirls.txt)
  88. def get_catgirls(fname='catgirls.txt'):
  89.  
  90.     print "collecting catgirls..."
  91.  
  92.     if not os.path.isfile(fname):
  93.         raise FileNotFoundError("Can't get catgirls from " + fname)
  94.  
  95.     catgirls = []
  96.  
  97.     with open(fname) as catgirls_file:
  98.         catgirls_contents = catgirls_file.read()
  99.  
  100.     catgirls = catgirls_contents.split("\n")
  101.     catgirls = filter(None, catgirls)
  102.  
  103.     catgirls = [catgirl.strip() for catgirl in catgirls]
  104.  
  105.     return catgirls
  106.  
  107. def get_saved_comments():
  108.  
  109.     print "reading comments_replied_to..."
  110.  
  111.     if not os.path.isfile("comments_replied_to.txt"):
  112.         comments_replied_to = []
  113.     else:
  114.         with open("comments_replied_to.txt", "r") as f:
  115.             comments_replied_to = f.read()
  116.             comments_replied_to = comments_replied_to.split("\n")
  117.             comments_replied_to = filter(None, comments_replied_to)
  118.  
  119.     return comments_replied_to
  120.  
  121.  
  122. if __name__ == '__main__':
  123.  
  124.     r = bot_login()
  125.     catgirls = get_catgirls()
  126.     comments_replied_to = get_saved_comments()
  127.  
  128.     while True:
  129.         run_bot(r, comments_replied_to, catgirls)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement