Advertisement
Guest User

Untitled

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