Guest User

Untitled

a guest
Sep 8th, 2017
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 8.42 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.6')
  14.     print 'Logged in!'
  15.  
  16.     return r
  17.  
  18. def run_bot(r, comments_replied_to, catgirls, foxgirls):
  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.     catgirl_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.                           'Did you want a foxgirl? Just reply saying so. '
  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.     foxgirl_reply = ('[Here](' + random.choice(foxgirls) + ') is a '
  42.                           'picture of a foxgirl! Hopefully this will cheer you up!'
  43.                           '\n\n'
  44.                           '---'
  45.                           '\n\n'
  46.                           'Did you want a catgirl? Just reply saying so. '
  47.                           'I am a bot. For more info on me and how to use me, see r/KemonomimiCheerUpBot '
  48.                           'Have I gone rogue? Reply \'!SHUTDOWN\' to stop me.')
  49.  
  50.     #Patiently awaiting the return of u/xenonauts...
  51.     xenonauts_reply = ('[Here](https://i.imgur.com/RcrLkpe.jpg) is a '
  52.                           'picture of a catgirl Sagiri! Hopefully this will cheer you up!'
  53.                           '\n\n'
  54.                           '---'
  55.                           '\n\n'
  56.                           'I am a bot. For more info on me and how to use me, see r/KemonomimiCheerUpBot '
  57.                           'Have I gone rogue? Reply \'!SHUTDOWN\' to stop me.')
  58.  
  59. #checks comments in above listed subreddit for triggers
  60.  
  61.     print 'Searching last 25 comments...'
  62.  
  63.     for comment in subreddits.comments(limit=25):
  64.         for trigger in (trigger_phrases):
  65.              if (trigger in comment.body.lower()  and \
  66.                 comment.id not in comments_replied_to and \
  67.                 comment.author != (ignored_users)):
  68.  
  69.                     if comment.author != ('xenonauts'):
  70.  
  71.                         #print 'Im sad found in comment' + comment.id
  72.  
  73.                         comment.reply(random.choice([catgirl_reply, foxgirl_reply]))
  74.  
  75.                         print 'replied to comment ' + comment.id
  76.  
  77.                     if comment.author == ('xenonauts'):
  78.  
  79.                         comment.reply(xenonauts_reply)
  80.  
  81.                         print "Replied to xenonauts' comment " + comment.id
  82.      
  83.                     comments_replied_to.append(comment.id)
  84.          
  85.                     with open('comments_replied_to.txt', 'a') as f:
  86.                         f.write(comment.id + '\n')
  87.  
  88. #checks inbox mentions for triggers
  89.  
  90.     print 'searching mentions...'
  91.  
  92.     for mention in r.inbox.mentions(limit=5):
  93.         for trigger in (trigger_phrases):
  94.             if (trigger in mention.body.lower() and \
  95.                 mention.id not in comments_replied_to and \
  96.                 mention.author != (ignored_users)):
  97.  
  98.                     if mention.author != ('xenonauts'):
  99.  
  100.                         mention.reply(random.choice([catgirl_reply, foxgirl_reply]))
  101.  
  102.                         print 'Replied to mention ' + mention.id
  103.  
  104.                     if mention.author == ('xenonauts'):
  105.  
  106.                         mention.reply(xenonauts_reply)
  107.  
  108.                         print "Replied to xenonauts' mention " + mention.id
  109.  
  110.                     comments_replied_to.append(mention.id)
  111.  
  112.                     with open('comments_replied_to.txt', 'a') as f:
  113.                         f.write(mention.id + '\n')
  114.  
  115. #checks inbox replies for trigger phrases
  116.  
  117.     print 'searching replies...'
  118.  
  119.     for reply in r.inbox.comment_replies(limit=3):
  120.         if ('good bot' in reply.body.lower() and reply.id not in comments_replied_to):
  121.  
  122.             #print 'Good bot found in reply ' + reply.id
  123.  
  124.             reply.reply('[Thank You! :)](https://i.imgur.com/P3GRavv.gifv)')
  125.  
  126.             print 'Thanked reply ' + reply.id
  127.  
  128.             comments_replied_to.append(reply.id)
  129.  
  130.             with open('comments_replied_to.txt', 'a') as f:
  131.                 f.write(reply.id+ '\n')
  132.  
  133.         if ('bad bot' in reply.body.lower() and reply.id not in comments_replied_to):
  134.  
  135.             print 'TRASH WAIFU FOUND!!'
  136.  
  137.             reply.reply('[Your waifu](merriam-webster.com/dictionary/trash)')
  138.  
  139.             comments_replied_to.append(reply.id)
  140.  
  141.             with open('comments_replied_to.txt', 'a') as f:
  142.                 f.write(reply.id+ '\n')
  143.  
  144.         if ('catgirl' in reply.body.lower() and reply.id not in comments_replied_to):
  145.  
  146.             print 'catgirl requested in reply ' + reply.id
  147.  
  148.             reply.reply(catgirl_reply)
  149.  
  150.             comments_replied_to.append(reply.id)
  151.  
  152.                 with open('comments_replied_to.txt', 'a') as f:
  153.                     f.write(reply.id+ '\n')
  154.  
  155.         if ('foxgirl' in reply.body.lower() and reply.id not in comments_replied_to):
  156.  
  157.             print 'foxgirl requested in reply ' + reply.id
  158.  
  159.             reply.reply(foxgirl_reply)
  160.  
  161.             comments_replied_to.append(reply.id)
  162.  
  163.                 with open('comments_replied_to.txt', 'a') as f:
  164.                     f.write(reply.id+ '\n')
  165.  
  166.         for waifu_trigger in ['who is your waifu', 'whos your waifu', "who's your waifu"]:
  167.             if (waifu_trigger in reply.body.lower() and reply.id not in comments_replied_to):
  168.  
  169.                 reply.reply('[Fixed Artillery-San](https://i.imgur.com/qrsKQCH.jpg)')
  170.  
  171.                 print 'bragged about waifu'
  172.  
  173.                 comments_replied_to.append(reply.id)
  174.  
  175.                 with open('comments_replied_to.txt', 'a') as f:
  176.                     f.write(reply.id+ '\n')
  177.  
  178.         #This is to allow a shutdown when necessary.
  179.         #Times this has prevented to robot uprising: 1
  180.  
  181.         #print 'Checking for SHUTDOWN command'
  182.         if ('!SHUTDOWN' in reply.body and reply.id not in comments_replied_to):
  183.             print '!SHUTDOWN issued by ' + reply.author
  184.             reply.repl('')
  185.    
  186.     print 'Time for a cat-nap!...'
  187.  
  188.     time.sleep(60)
  189.  
  190. #Load catgirls from a file (defaults to catgirls.txt)
  191. def get_catgirls(fname='catgirls.txt'):
  192.  
  193.     print 'Reading catgirls.txt...'
  194.  
  195.     if not os.path.isfile(fname):
  196.         raise FileNotFoundError("Can't get catgirls from " + fname)
  197.  
  198.     catgirls = []
  199.  
  200.     with open(fname) as catgirls_file:
  201.         catgirls_contents = catgirls_file.read()
  202.  
  203.     catgirls = catgirls_contents.split('\n')
  204.     catgirls = filter(None, catgirls)
  205.  
  206.     catgirls = [catgirl.strip() for catgirl in catgirls]
  207.  
  208.     return catgirls
  209.  
  210. #load foxgirls from a file (defaults to foxgirls.txt)
  211. def get_foxgirls(fname='foxgirls.txt'):
  212.  
  213.     print 'reading foxgirls.txt...'
  214.  
  215.     if not os.path.isfile(fname):
  216.             raise FileNotFoundError("can't get foxgirls from " + fname)
  217.  
  218.     foxgirls = []
  219.  
  220.     with open(fname) as foxgirls_file:
  221.         foxgirls_contents = foxgirls_file.read()
  222.  
  223.     foxgirls = foxgirls_contents.split('\n')
  224.     foxgirls = filter(None, foxgirls)
  225.  
  226.     foxgirls = [foxgirl.strip() for foxgirl in foxgirls]
  227.  
  228.     return foxgirls
  229.  
  230. #reads file of comments bot has replied to
  231. def get_saved_comments():
  232.  
  233.     print 'reading comments_replied_to...'
  234.  
  235.     if not os.path.isfile('comments_replied_to.txt'):
  236.         comments_replied_to = []
  237.     else:
  238.         with open('comments_replied_to.txt', 'r') as f:
  239.             comments_replied_to = f.read()
  240.             comments_replied_to = comments_replied_to.split('\n')
  241.             comments_replied_to = filter(None, comments_replied_to)
  242.  
  243.     return comments_replied_to
  244.  
  245.  
  246. if __name__ == '__main__':
  247.  
  248.     r = bot_login()
  249.     catgirls = get_catgirls()
  250.     foxgirls = get_foxgirls()
  251.     comments_replied_to = get_saved_comments()
  252.  
  253.     while True:
  254.         run_bot(r, comments_replied_to, catgirls, foxgirls)
Add Comment
Please, Sign In to add comment