Advertisement
Guest User

Untitled

a guest
Sep 14th, 2017
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 10.45 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.7')
  14.     print 'Logged in!'
  15.  
  16.     return r
  17.  
  18. def run_bot(r, comments_replied_to, catgirls, foxgirls, wolfgirls):
  19.     #print comments_replied_to
  20.     #print catgirls
  21.  
  22.     testsub = r.subreddit('kemonomimicheerupbot')
  23.  
  24.     subreddits = r.subreddit('+kemonomimicheerupbot+anime_irl+animemes+awwnime')
  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', 'sneakpeekbot']
  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 or wolfgirl? 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 or wolfgirl? 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.     wolfgirl_reply = ('[Here](' + random.choice(wolfgirls) + ') is a '
  51.                           'picture of a wolfgirl! Hopefully this will cheer you up!'
  52.                           '\n\n'
  53.                           '---'
  54.                           '\n\n'
  55.                           'Did you want a catgirl or foxgirl? Just reply saying so. '
  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.     replies = [catgirl_reply, foxgirl_reply, wolfgirl_reply]
  60.  
  61.     #Patiently awaiting the return of u/xenonauts...
  62.     xenonauts_reply = ('[Here](https://i.imgur.com/RcrLkpe.jpg) is a '
  63.                           'picture of a catgirl Sagiri! Hopefully this will cheer you up!'
  64.                           '\n\n'
  65.                           '---'
  66.                           '\n\n'
  67.                           'I am a bot. For more info on me and how to use me, see r/KemonomimiCheerUpBot '
  68.                           'Have I gone rogue? Reply \'!SHUTDOWN\' to stop me.')
  69.  
  70. #checks comments in above listed subreddit for triggers
  71.  
  72.     print 'Searching last 25 comments...'
  73.  
  74.     for comment in subreddits.comments(limit=25):
  75.         for trigger in (trigger_phrases):
  76.              if (trigger in comment.body.lower()  and \
  77.                 comment.id not in comments_replied_to and \
  78.                 comment.author not in (ignored_users)):
  79.  
  80.                     if comment.author != ('xenonauts'):
  81.  
  82.                         #print 'Im sad found in comment' + comment.id
  83.  
  84.                         comment.reply(random.choice(replies))
  85.  
  86.                         print 'replied to comment ' + comment.id
  87.  
  88.                     if comment.author == ('xenonauts'):
  89.  
  90.                         comment.reply(xenonauts_reply)
  91.  
  92.                         print "Replied to xenonauts' comment " + comment.id
  93.      
  94.                     comments_replied_to.append(comment.id)
  95.          
  96.                     with open('comments_replied_to.txt', 'a') as f:
  97.                         f.write(comment.id + '\n')
  98.  
  99. #checks inbox mentions for triggers
  100.  
  101.     print 'searching mentions...'
  102.  
  103.     for mention in r.inbox.mentions(limit=5):
  104.         for trigger in (trigger_phrases):
  105.             if (trigger in mention.body.lower() and \
  106.                 mention.id not in comments_replied_to and \
  107.                 mention.author not in (ignored_users)):
  108.  
  109.                     if mention.author != ('xenonauts'):
  110.  
  111.                         mention.reply(random.choice(replies))
  112.  
  113.                         print 'Replied to mention ' + mention.id
  114.  
  115.                     if mention.author == ('xenonauts'):
  116.  
  117.                         mention.reply(xenonauts_reply)
  118.  
  119.                         print "Replied to xenonauts' mention " + mention.id
  120.  
  121.                     comments_replied_to.append(mention.id)
  122.  
  123.                     with open('comments_replied_to.txt', 'a') as f:
  124.                         f.write(mention.id + '\n')
  125.  
  126. #checks inbox replies for trigger phrases
  127.  
  128.     print 'searching replies...'
  129.  
  130.     for reply in r.inbox.comment_replies(limit=5):
  131.         if ('good bot' in reply.body.lower() and reply.id not in comments_replied_to):
  132.  
  133.             #print 'Good bot found in reply ' + reply.id
  134.  
  135.             reply.reply('[Thank You! :)](https://i.imgur.com/P3GRavv.gifv)')
  136.  
  137.             print 'Thanked reply ' + reply.id
  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 ('bad bot' in reply.body.lower() and reply.id not in comments_replied_to
  145.                 and reply.author not in (ignored_users)):
  146.  
  147.             print 'TRASH WAIFU FOUND!!'
  148.  
  149.             reply.reply('[Your waifu](www.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 catgirl_trigger in ['catgirl', 'cat girl']:
  157.             if (catgirl_trigger in reply.body.lower() and reply.id not in comments_replied_to
  158.                     and reply.author not in (ignored_users)):
  159.  
  160.  
  161.                 print 'catgirl requested in reply ' + reply.id
  162.  
  163.                 reply.reply(catgirl_reply)
  164.  
  165.                 comments_replied_to.append(reply.id)
  166.  
  167.                 with open('comments_replied_to.txt', 'a') as f:
  168.                     f.write(reply.id+ '\n')
  169.  
  170.         for foxgirl_trigger in ['foxgirl', 'foxgirl']:
  171.             if (foxgirl_trigger in reply.body.lower() and reply.id not in comments_replied_to
  172.                     and reply.author not in (ignored_users)):
  173.  
  174.                 print 'foxgirl requested in reply ' + reply.id
  175.  
  176.                 reply.reply(foxgirl_reply)
  177.  
  178.                 comments_replied_to.append(reply.id)
  179.  
  180.                 with open('comments_replied_to.txt', 'a') as f:
  181.                     f.write(reply.id+ '\n')
  182.  
  183.         for wolfgirl_trigger in ['wolfgirl', 'wolf girl', 'awoo']:
  184.             if (wolfgirl_trigger in reply.body.lower() and reply.id not in comments_replied_to
  185.                     and reply.author not in (ignored_users)):
  186.  
  187.                 print 'wolfgirl requested in reply ' + reply.id
  188.  
  189.                 reply.reply(wolfgirl_reply)
  190.  
  191.                 comments_replied_to.append(reply.id)
  192.  
  193.                 with open('comments_replied_to.txt', 'a') as f:
  194.                     f.write(reply.id+ '\n')
  195.  
  196.         for waifu_trigger in ['who is your waifu', 'whos your waifu', "who's your waifu"]:
  197.             if (waifu_trigger in reply.body.lower() and reply.id not in comments_replied_to
  198.                     and reply.author not in (ignored_users)):
  199.  
  200.                 reply.reply('[Fixed Artillery-San](https://i.imgur.com/qrsKQCH.jpg)')
  201.  
  202.                 print 'bragged about waifu'
  203.  
  204.                 comments_replied_to.append(reply.id)
  205.  
  206.                 with open('comments_replied_to.txt', 'a') as f:
  207.                     f.write(reply.id+ '\n')
  208.  
  209.         #This is to allow a shutdown when necessary.
  210.         #Times this has prevented to robot uprising: 1
  211.  
  212.         #print 'Checking for SHUTDOWN command'
  213.         if ('!SHUTDOWN' in reply.body and reply.id not in comments_replied_to):
  214.             print '!SHUTDOWN issued by ' + reply.author
  215.             reply.repl('')
  216.    
  217.     print 'Time for a cat-nap!...'
  218.  
  219.     time.sleep(60)
  220.  
  221. #Load catgirls from a file (defaults to catgirls.txt)
  222. def get_catgirls(fname='catgirls.txt'):
  223.  
  224.     print 'Reading catgirls.txt...'
  225.  
  226.     if not os.path.isfile(fname):
  227.         raise FileNotFoundError("Can't get catgirls from " + fname)
  228.  
  229.     catgirls = []
  230.  
  231.     with open(fname) as catgirls_file:
  232.         catgirls_contents = catgirls_file.read()
  233.  
  234.     catgirls = catgirls_contents.split('\n')
  235.     catgirls = filter(None, catgirls)
  236.  
  237.     catgirls = [catgirl.strip() for catgirl in catgirls]
  238.  
  239.     return catgirls
  240.  
  241. #load foxgirls from a file (defaults to foxgirls.txt)
  242. def get_foxgirls(fname='foxgirls.txt'):
  243.  
  244.     print 'reading foxgirls.txt...'
  245.  
  246.     if not os.path.isfile(fname):
  247.             raise FileNotFoundError("can't get foxgirls from " + fname)
  248.  
  249.     foxgirls = []
  250.  
  251.     with open(fname) as foxgirls_file:
  252.         foxgirls_contents = foxgirls_file.read()
  253.  
  254.     foxgirls = foxgirls_contents.split('\n')
  255.     foxgirls = filter(None, foxgirls)
  256.  
  257.     foxgirls = [foxgirl.strip() for foxgirl in foxgirls]
  258.  
  259.     return foxgirls
  260.  
  261. #load wolfgirls from a file (defaults to foxgirls.txt
  262. def get_wolfgirls(fname='wolfgirls.txt'):
  263.  
  264.     print 'reading wolfgirls.txt...'
  265.  
  266.     if not os.path.isfile(fname):
  267.         raise FileNotFoundError("can't get wolfgirls from " + fname)
  268.  
  269.     wolfgirls = []
  270.  
  271.     with open(fname) as wolfgirls_file:
  272.         wolfgirls_contents = wolfgirls_file.read()
  273.  
  274.     wolfgirls = wolfgirls_contents.split('\n')
  275.     wolfgirls = [wolfgirl.strip() for wolfgirl in wolfgirls]
  276.  
  277.     return wolfgirls
  278.  
  279. #reads file of comments bot has replied to
  280. def get_saved_comments():
  281.  
  282.     print 'reading comments_replied_to...'
  283.  
  284.     if not os.path.isfile('comments_replied_to.txt'):
  285.         comments_replied_to = []
  286.     else:
  287.         with open('comments_replied_to.txt', 'r') as f:
  288.             comments_replied_to = f.read()
  289.             comments_replied_to = comments_replied_to.split('\n')
  290.             comments_replied_to = filter(None, comments_replied_to)
  291.  
  292.     return comments_replied_to
  293.  
  294.  
  295. if __name__ == '__main__':
  296.  
  297.     r = bot_login()
  298.     catgirls = get_catgirls()
  299.     foxgirls = get_foxgirls()
  300.     wolfgirls = get_wolfgirls()
  301.     comments_replied_to = get_saved_comments()
  302.  
  303.     while True:
  304.         run_bot(r, comments_replied_to, catgirls, foxgirls, wolfgirls)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement