Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import praw
- import config
- import random
- import os
- import time
- def bot_login():
- print "Logging in..."
- r = praw.Reddit(username = config.username,
- password = config.password,
- client_id = config.client_id,
- client_secret = config.client_secret,
- user_agent = "kemonomimi bot v0.5")
- print "Logged in!"
- return r
- def run_bot(r, comments_replied_to, catgirls):
- #print comments_replied_to
- #print catgirls
- print "Searching last 25 comments..."
- testsub = r.subreddit("kemonomimicheerupbot")
- subreddits = r.subreddit('+kemonomimicheerupbot+anime_irl+animemes')
- #print subreddits
- trigger_phrases = ["im sad", "i'm sad", "cheer me up"]
- #print trigger_phrases
- ignored_users = [r.user.me(), "thiscatmightcheeryou"]
- #print ignored_users
- comment_reply = ("[Here](" + random.choice(catgirls) + ") is a "
- "picture of a catgirl! Hopefully this will cheer you up!"
- "\n\n"
- "---"
- "\n\n"
- "I am a bot. For more info on me and how to use me, see r/KemonomimiCheerUpBot "
- "Have I gone rogue? Reply \"!SHUTDOWN\" to stop me.")
- xenonauts_reply = ("[Here](https://i.imgur.com/RcrLkpe.jpg) is a "
- "picture of a catgirl Sagiri! Hopefully this will cheer you up!"
- "\n\n"
- "---"
- "\n\n"
- "I am a bot. For more info on me and how to use me, see r/KemonomimiCheerUpBot "
- "Have I gone rogue? Reply \"!SHUTDOWN\" to stop me.")
- #checks comments in above listed subreddit for triggers
- for comment in subreddits.comments(limit=25):
- for trigger in (trigger_phrases):
- if (trigger in comment.body.lower() and \
- comment.id not in comments_replied_to and \
- comment.author != (ignored_users, "xenonauts")):
- #print "Im sad found in comment" + comment.id
- comment.reply(comment_reply)
- print "replied to comment " + comment.id
- comments_replied_to.append(comment.id)
- with open("comments_replied_to.txt", "a") as f:
- f.write(comment.id + "\n")
- #Patiently awaiting the return of u/xenonauts...
- for trigger in (trigger_phrases):
- if (trigger in comment.body.lower() and \
- comment.id not in comments_replied_to and \
- comment.author == (xenonauts)):
- print "XENONAUTS IS BACK!!! REPLIED TO COMMENT" + comment.id
- comment.reply(xenonauts_reply)
- print "replied to comment " + comment.id
- comments_replied_to.append(comment.id)
- with open("comments_replied_to.txt", "a") as f:
- f.write(comment.id + "\n")
- #checks inbox mentions for triggers
- print "searching mentions..."
- for mention in r.inbox.mentions(limit=5):
- if trigger in (trigger_phrases):
- if (trigger in mention.body.lower() and \
- mention.id not in comments_replied_to and \
- mention.author != (ignored_users, "xenonauts")):
- print "Mentioned in comment " + mention.id
- mention.reply(comment_reply)
- print "replied to mention " + mention.id
- comments_replied_to.append(comment.id)
- with open("comments_replied_to.txt", "a") as f:
- f.write(comment.id + "\n")
- if trigger in (trigger_phrases):
- if (trigger in mention.body.lower() and \
- mention.id not in comments_replied_to and \
- mention.author == ("xenonauts")):
- print "Mentioned in xenonauts' comment " + mention.id
- mention.reply(xenonauts_reply)
- print "replied to xenonauts' mention " + mention.id
- comments_replied_to.append(comment.id)
- with open("comments_replied_to.txt", "a") as f:
- f.write(comment.id + "\n")
- #checks inbox replies for trigger phrases
- print "searching inbox..."
- for reply in r.inbox.comment_replies(limit=3):
- if ("good bot" in reply.body.lower() and reply.id not in comments_replied_to):
- #print "Good bot found in reply " + reply.id
- reply.reply("[Thank You! :)](https://i.imgur.com/P3GRavv.gifv)")
- print "Thanked reply " + reply.id
- comments_replied_to.append(reply.id)
- with open("comments_replied_to.txt", "a") as f:
- f.write(reply.id+ "\n")
- if ("bad bot" in reply.body.lower() and reply.id not in comments_replied_to):
- print "TRASH WAIFU FOUND!!"
- reply.reply("[Your waifu](merriam-webster.com/dictionary/trash)")
- comments_replied_to.append(reply.id)
- with open("comments_replied_to.txt", "a") as f:
- f.write(reply.id+ "\n")
- for waifu_trigger in ["who is your waifu", "whos your waifu", "who's your waifu"]:
- if (waifu_trigger in reply.body.lower() and reply.id not in comments_replied_to):
- reply.reply("[Fixed Artillery-San](https://i.imgur.com/qrsKQCH.jpg)")
- print "bragged about waifu"
- comments_replied_to.append(reply.id)
- with open("comments_replied_to.txt", "a") as f:
- f.write(reply.id+ "\n")
- #This is to allow a shutdown when necessary.
- #Times this has prevented to robot uprising: 1
- #print "Checking for SHUTDOWN command"
- if ("!SHUTDOWN" in reply.body and reply.id not in comments_replied_to):
- print "!SHUTDOWN issued by " + reply.author
- reply.repl("")
- print "Time for a cat-nap!..."
- time.sleep(60)
- #Load catgirls from a file (defaults to catgirls.txt)
- def get_catgirls(fname='catgirls.txt'):
- print "collecting catgirls..."
- if not os.path.isfile(fname):
- raise FileNotFoundError("Can't get catgirls from " + fname)
- catgirls = []
- with open(fname) as catgirls_file:
- catgirls_contents = catgirls_file.read()
- catgirls = catgirls_contents.split("\n")
- catgirls = filter(None, catgirls)
- catgirls = [catgirl.strip() for catgirl in catgirls]
- return catgirls
- #reads file of comments bot has replied to
- def get_saved_comments():
- print "reading comments_replied_to..."
- if not os.path.isfile("comments_replied_to.txt"):
- comments_replied_to = []
- else:
- with open("comments_replied_to.txt", "r") as f:
- comments_replied_to = f.read()
- comments_replied_to = comments_replied_to.split("\n")
- comments_replied_to = filter(None, comments_replied_to)
- return comments_replied_to
- if __name__ == '__main__':
- r = bot_login()
- catgirls = get_catgirls()
- comments_replied_to = get_saved_comments()
- while True:
- run_bot(r, comments_replied_to, catgirls)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement