Advertisement
Guest User

Untitled

a guest
Nov 17th, 2016
1,340
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.95 KB | None | 0 0
  1. import praw #you probably know what this is
  2. import base64 #encoded passwords
  3. from urllib.request import quote #to turn the parent comment into a link
  4. r = praw.Reddit(user_agent='UNIQUE STRING NOT CONTAINING BOT AND CONTAINING THE OWNERS USERNAME',
  5.                          client_id='YOURIDHERE', client_secret="YOURSECRETHERE",
  6.                          username='YOURUSERNAMEHERE', password=base64.b64decode(b"YOURPASSWORDHERE").decode("utf-8", "ignore"))
  7. r.read_only = False #no idea if this is required. Kept it to be safe.
  8. subreddit = r.subreddit("ShrellexBotTesting") #get the subreddit. "all" for all comments
  9. f = open("C:\\repliedto.txt", "r+") #REPLACE THE PATH
  10.  
  11.    
  12.  
  13. comments = subreddit.stream.comments() # get the comment stream
  14. x = 1 #for the counter
  15. for comment in comments: #for each comment in the comments stream. the current comment being processed is called "comment"
  16.     print("found new comment! processing... (" + str(x) + ")") #the str(x) thing is printing the number of the comment being proccesed
  17.     x += 1 #add 1 to the number
  18.     text = str(comment.body) # Fetch body
  19.     try:
  20.         author = str(comment.author) # Fetch author
  21.     except AttributeError: #check if the author has been deleted
  22.         print("Author has been deleted")
  23.         #author was deleted
  24.         continue
  25.     print(text) #DEBUGGING, REMOVE WHEN WORKING
  26.     print(author) #SAME
  27.     if author.lower() == "YOURUSERNAMEHERE".lower(): #Don't reply to yourself
  28.         #myself
  29.         print("Comment is by myself")
  30.         continue
  31.  
  32.     if text == "!lmgtfy": #check to see the comment is "!lmgtfy". use if text.lower() == "!lmgtfy".lower() to be non-case sensitive, use if "!lmgtfy" in text if you want the comment to be anywhere
  33.         if comment.id in f.read(): #if the comment is already in the file, bot has replied to it
  34.             print("ALREADY IN FILE")
  35.         if comment.id not in f.read(): #^
  36.             # Generate a message
  37.             print("Attempting Answer")
  38.             parid = comment.parent_id #by /u/bboe
  39.             parbody = r.comment(comment.parent_id.rsplit('_', 1)[1])#by /u/bboe
  40.             parentbody = parbody.body #by /u/bboe, fetches the body of the parent comment
  41.             message = "http://lmgtfy.com/?q=" + quote(str(parentbody)) + "\n\n_____\n\n^by ^/u/Shrellex. ^Please ^only ^use ^when ^the ^answer ^is ^blatantly ^obvious. ^Call ^the ^bot ^with ^!lmgtfy ^and ^it ^will ^lmgtfy-ify ^the ^parent ^comment. ^Please ^don't ^downvote ^me."
  42.             #quote() url-ifies the text, self explanatory i hope
  43.             try:
  44.                 comment.reply(message) #reply to comment
  45.  
  46.             except praw.errors.Forbidden:
  47.                 print('403 error - is the bot banned from sub %s?' % "not impl")
  48.             print("Replied to comment by " + author)
  49.             f.write(comment.id + "\n")#write comment id to file so it doesn't reply to it again
  50.             if comment.id in f.read():
  51.                 print("Written Successfully!")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement