Guest User

Bot Code

a guest
Jun 3rd, 2016
752
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.70 KB | None | 0 0
  1. import praw, re, os
  2. from conf import *
  3.  
  4. # Check that the file that contains our username exists
  5. if not os.path.isfile("conf.py"):
  6.     print("Config Required!")
  7.     exit(1)
  8.  
  9. # Create the Reddit instance
  10. user_agent = ("SupremeRedditB0t 0.1")
  11. r = praw.Reddit(user_agent=user_agent)
  12.  
  13. # and login
  14. r.login(REDDIT_USERNAME, REDDIT_PASS, disable_warning=True)
  15.  
  16. # Have we run this code before? If not, create an empty list
  17. if not os.path.isfile("posts_replied_to.txt"):
  18.     posts_replied_to = []
  19.  
  20. # If we have run the code before, load the list of posts we have replied to
  21. else:
  22.     # Read the file into a list and remove any empty values
  23.     with open("posts_replied_to.txt", "r") as f:
  24.         posts_replied_to = f.read()
  25.         posts_replied_to = posts_replied_to.split("\n")
  26.         posts_replied_to = list(set(posts_replied_to))
  27.  
  28. # Get all posts from subreddit
  29. subreddit = r.get_subreddit('CodingHelp')
  30. for submission in subreddit.get_new():
  31.  
  32.     # If we haven't replied to this post before
  33.     if submission.id not in posts_replied_to:
  34.  
  35.         # Check if has a flair
  36.         if submission.link_flair_text == None:
  37.  
  38.             # Default suggested flair
  39.             flair = "Random"
  40.  
  41.             # Detect any languages in post/title
  42.             if re.search("HTML", submission.title, re.IGNORECASE) or re.search("HTML", submission.selftext, re.IGNORECASE):
  43.                 flair = "HTML"
  44.             elif re.search("CSS", submission.title, re.IGNORECASE) or re.search("CSS", submission.selftext, re.IGNORECASE):
  45.                 flair = "CSS"
  46.             elif re.search("Java", submission.title, re.IGNORECASE) or re.search("Java", submission.selftext, re.IGNORECASE):
  47.                 flair = "Java"
  48.             elif re.search("Javascript", submission.title, re.IGNORECASE) or re.search("Javascript", submission.selftext, re.IGNORECASE):
  49.                 flair = "Javascript"
  50.             elif re.search("PHP", submission.title, re.IGNORECASE) or re.search("PHP", submission.selftext, re.IGNORECASE):
  51.                 flair = "PHP"
  52.             elif re.search("Code", submission.title, re.IGNORECASE) or re.search("Code", submission.selftext, re.IGNORECASE):
  53.                 flair = "Other Code"
  54.            
  55.             # Reply to the post with suggested flair
  56.             submission.add_comment("Please Add A Flair To Your Post!\n\nSuggested Flair: ["+flair+"]")
  57.             print("Bot replying to:", submission.title, "with flair", flair)
  58.  
  59.             # Store the current id into our list
  60.             posts_replied_to.append(submission.id)
  61.            
  62. # Write our updated list back to the file
  63. with open("posts_replied_to.txt", "w") as f:
  64.     for post_id in posts_replied_to:
  65.         f.write(post_id + "\n")
Advertisement
Add Comment
Please, Sign In to add comment