Advertisement
Polaris117

Waifu-Claimer Source Code

Dec 28th, 2020
511
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.72 KB | None | 0 0
  1. #imports
  2.  
  3. import praw
  4. import time
  5. import os
  6. from datetime import date
  7. from os import environ
  8.  
  9. #get date
  10.  
  11. today = date.today().strftime("%m/%d/%Y")
  12. print (today)
  13.  
  14. #defining variables for later use
  15.  
  16. keyphrase = ("!claim ")
  17. space = (" ")
  18. extra_line = ("\n")
  19.  
  20. #bot login
  21.  
  22. def bot_login():
  23.     print("logging in")
  24.     r = praw.Reddit(username=environ["USERNAME"],
  25.                     password=environ["PASSWORD"],
  26.                     client_id=environ["CLIENT_ID"],
  27.                     client_secret=environ["CLIENT_SECRET"],
  28.                     user_agent="Waifu-Claimer.bot.v0.1")
  29.     print("logged in")
  30.    
  31.     return r
  32.  
  33. #bot run-process
  34.  
  35. def run_bot(r, claimed_waifus, previous_claimers):
  36.     print("obtaining comments...")
  37.    
  38.     for comment in r.subreddit('WaifuClaimer').comments(limit=70):
  39.         submission = comment
  40.         if "!claim " in comment.body and comment.id not in comments_replied_to and comment.author != r.user.me():
  41.             waifu = comment.body.replace(keyphrase, '')
  42.             waifu_official = waifu.lower().replace(space,'').replace(extra_line,'')
  43.             claimer = submission.author.name
  44.             print("claim for " + waifu_official)
  45.             print("comment replied to: " + comment.id + " by " + claimer)
  46.             try:
  47.                 if waifu_official not in claimed_waifus:
  48.                     previous_claimers.append(claimer)
  49.                     with open ("previous_claimers.txt", "a") as p:
  50.                         p.write(claimer + "\n")
  51.                     claimed = (previous_claimers.count(claimer))
  52.                     print("user has claimed " + str(claimed))
  53.                     if claimed == 1:
  54.                         comment.reply("[Congrats](https://i.imgur.com/HTQcCLn.gif), " + waifu + " was claimed! You have claimed " + str(claimed) + " waifu now. I sense the beginnings of an excellent harem." + "\n\n ^Click ^[here](https://www.reddit.com/r/WaifuClaimer/comments/jwohm8/waifuclaimer_bot_directory/?utm_source=share&utm_medium=web2x&context=3) ^for ^bot ^directory.")
  55.                     else:
  56.                         comment.reply("[Congrats](https://i.imgur.com/HTQcCLn.gif), " + waifu + " was claimed! You have claimed " + str(claimed) + " waifus now." + "\n\n ^Click ^[here](https://www.reddit.com/r/WaifuClaimer/comments/jwohm8/waifuclaimer_bot_directory/?utm_source=share&utm_medium=web2x&context=3) ^for ^bot ^directory.")
  57.                     claimed_waifus.append(waifu_official)
  58.                     r.subreddit('WaifuClaimer').submit(waifu + " has been claimed by u/" + claimer,selftext=("Claimer: u/" + claimer + "\n" + "\n" + "Waifu: " + waifu + "\n" + "\n" + "Date: " + today), url=None, flair_id="eebc0dc8-1d8d-11eb-be0c-0e3512b41733")
  59.                     with open ("claimed_waifus.txt", "a") as g:
  60.                         g.write(waifu_official + "\n")
  61.  
  62.                 else:
  63.                     comment.reply("Sorry, someone else has already claimed them." + "\n\n ^Click ^[here](https://youtu.be/L9EDYkn4oUU) ^for ^bot ^directory.")
  64.  
  65.             except:
  66.                 print("error")
  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.     print("sleeping for 10 seconds")
  74.     #sleep for 10 seconds...
  75.     time.sleep(10)
  76.  
  77. #read list of comments replied to
  78.  
  79. def get_saved_comments():
  80.     if not os.path.isfile("comments_replied_to.txt"):
  81.         comments_replied_to = []
  82.     else:
  83.         with open("comments_replied_to.txt", "r") as f:
  84.             comments_replied_to = f.read()
  85.             comments_replied_to = comments_replied_to.split("\n")
  86.        
  87.     return comments_replied_to
  88.  
  89. #read list of waifus claimed
  90.  
  91. def get_claimed_waifus():
  92.     if not os.path.isfile("claimed_waifus.txt"):
  93.         claimed_waifus = []
  94.     else:
  95.         with open("claimed_waifus.txt", "r") as g:
  96.             claimed_waifus = g.read()
  97.             claimed_waifus = claimed_waifus.split("\n")
  98.        
  99.     return claimed_waifus
  100.  
  101. #read list of users that have claimed a waifu
  102.  
  103. def get_previous_claimers():
  104.     if not os.path.isfile("previous_claimers.txt"):
  105.         previous_claimers = []
  106.     else:
  107.         with open("previous_claimers.txt", "r") as p:
  108.             previous_claimers = p.read()
  109.             previous_claimers = previous_claimers.split("\n")
  110.        
  111.     return previous_claimers
  112.    
  113. #defining more variables
  114.  
  115. r = bot_login()
  116. comments_replied_to = get_saved_comments()
  117. claimed_waifus = get_claimed_waifus()
  118. previous_claimers = get_previous_claimers()
  119. print ("comments_replied_to and claimed_waifus")
  120.  
  121. #active part of script
  122.  
  123. while True:
  124.     run_bot(r, claimed_waifus, previous_claimers)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement