Advertisement
dmesticg

Untitled

Jun 19th, 2017
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.86 KB | None | 0 0
  1. import praw
  2. import config
  3. import time
  4. import os
  5. import random
  6. import re
  7.  
  8. def bot_login():
  9. print "Loggin in..."
  10. r = praw.Reddit(username = config.username,
  11. password = config.password,
  12. client_id = config.client_id,
  13. client_secret = config.client_secret,
  14. user_agent = "busterronitest's dog comment responder v0.1")
  15. print "Logged in!"
  16.  
  17. return r
  18.  
  19. def run_bot(r, comments_replied_to):
  20. print "Obtaining 25 comments..."
  21.  
  22. for comment in r.subreddit('clearbot').comments(limit=10):
  23. if "!coinflip" in comment.body and comment.id not in comments_replied_to and comment.author != r.user.me():
  24. print "String with \"!coinflip\" found in comment " + comment.id
  25. thecomment = comment.body
  26. betSize, betChoice = re.match(r'!coinflip \[(\w+)] \((\d+)\)', thecomment).groups()
  27. betSize = int(betSize)
  28. whatside = random.randint(1,2)
  29. if whatside == 1:
  30. side = "Heads"
  31. else:
  32. side = "Tails"
  33. if side[0] == betChoice:
  34. win = "win"
  35. elif side[0] != betChoice:
  36. win = "lose"
  37. else:
  38. win = "ERROR!"
  39. comment.reply("Flipping a Coin... It landed on {}, Which means you {} {} coins!".format(side ,win, betSize))
  40. print "Replied to comment " + comment.id
  41.  
  42. comments_replied_to.append(comment.id)
  43.  
  44. with open ("comments_replied_to.txt", "a") as a:
  45. a.write(comment.id + "\n")
  46.  
  47. print "Sleeping for 10 seconds..."
  48. time.sleep(10)
  49.  
  50. def get_saved_comments():
  51. if not os.path.isfile("comments_replied_to.txt"):
  52. comments_replied_to = []
  53. else:
  54. with open("comments_replied_to.txt", "r") as f:
  55. comments_replied_to = f.read()
  56. comments_replied_to = comments_replied_to.split("\n")
  57. comments_replied_to = filter(None, comments_replied_to)
  58.  
  59. return comments_replied_to
  60. r = bot_login()
  61. comments_replied_to = get_saved_comments()
  62. print comments_replied_to
  63.  
  64. while True:
  65. run_bot(r, comments_replied_to)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement