Advertisement
dmesticg

Untitled

Jul 2nd, 2017
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.58 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 ("Logging in...")
  10. global r
  11. r = praw.Reddit(username = config.username,
  12. password = config.password,
  13. client_id = config.client_id,
  14. client_secret = config.client_secret,
  15. user_agent = config.user_agent)
  16. print ("Logged in!")
  17.  
  18. def run_bot():
  19. global r
  20. global comments_dealt_with
  21. for subreddit in config.subreddits:
  22. didsomething = False
  23. print ("Obtaining 25 comments in " + subreddit)
  24. for comment in r.subreddit(subreddit).comments(limit=25):
  25. thecomment = comment.body
  26. flair = next(r.subreddit(config.flair_subreddit).flair(comment.author))
  27. if flair["flair_text"] is None:
  28. r.subreddit(config.flair_subreddit).flair.set(comment.author, "1000")
  29. flair["flair_text"] = "1000"
  30. print ("New user, setting balance to 1000")
  31. try:
  32. currentMoney = int(flair["flair_text"])
  33. except ValueError:
  34. print ("Flair is not an integer, ignoring, flair = " + flair["flair_text"])
  35. comments_dealt_with.append(comment.id)
  36. with open ("comments_dealt_with.txt", "a") as a:
  37. a.write(comment.id + "\n")
  38. continue
  39.  
  40.  
  41. if ("!status" in comment.body and comment.id not in comments_dealt_with and comment.author != r.user.me()):
  42. print ("'!STATUS' FOUND IN COMMENT " + comment.id)
  43. comment.reply(config.status_text + config.footer_text)
  44. didsomething = True
  45.  
  46. if ("!resetbalance" in comment.body and comment.id not in comments_dealt_with and comment.author != r.user.me()):
  47. print ("resetbalance called, resetting balance")
  48. r.subreddit(config.flair_subreddit).flair.set(comment.author, "1000")
  49. comment.reply(config.reset_text + config.footer_text)
  50. didsomething = True
  51.  
  52. if ("!checkbalance" in comment.body and comment.id not in comments_dealt_with and comment.author != r.user.me()):
  53. print ("checkbalance called, checking balance")
  54. comment.reply(config.checkbal_text(currentMoney) + config.footer_text)
  55. didsomething = True
  56.  
  57. try:
  58. if "!coinflip" in thecomment:
  59. betSize, betChoice = re.match(r".*!coinflip \[(\d+)] \(([HT])\)", thecomment).groups()
  60. betSize = int(betSize)
  61. if currentMoney < betSize:
  62. comment.reply(config.low_money_text(currentMoney) + config.footer_text)
  63. print ("Not enough coins, replied")
  64. didsomething = True
  65. else:
  66. flip = random.choice(["Heads", "Tails"])
  67. if flip(0) == betChoice:
  68. r.subreddit(config.flair_subreddit).flair.set(comment.author, str(currentMoney + betSize))
  69. comment.reply(config.win_text(flip, betSize, currentMoney + betSize) + config.footer_text)
  70. else:
  71. r.subreddit(config.flair_subreddit).flair.set(comment.author, str(currentMoney - betSize))
  72. comment.reply(config.lose_text(flip, betSize, currentMoney - betSize) + config.footer_text)
  73. didsomething = True
  74. except (AttributeError, ValueError):
  75. print ("Invalid formatting, ignoring")
  76. comment.reply("Incorrect formatting, try !coinflip [amount] (H or T)" + config.footer_text)
  77. didsomething = True
  78.  
  79. if didsomething:
  80. print ("Replied to comment " + comment.id)
  81. comments_dealt_with.append(comment.id)
  82. with open ("comments_dealt_with.txt", "a") as a:
  83. a.write(comment.id + "\n")
  84.  
  85. print ("Sleeping for 10 seconds...")
  86. time.sleep(10)
  87.  
  88. def get_saved_comments():
  89. global comments_dealt_with
  90. if not os.path.isfile("comments_dealt_with.txt"):
  91. comments_dealt_with = []
  92. else:
  93. with open("comments_dealt_with.txt", "r") as f:
  94. comments_dealt_with = f.read()
  95. comments_dealt_with = comments_dealt_with.split("\n")
  96. #comments_dealt_with = filter(None, comments_dealt_with)
  97.  
  98.  
  99. bot_login()
  100. get_saved_comments()
  101. print (comments_dealt_with)
  102.  
  103. while True:
  104. run_bot()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement