Advertisement
dmesticg

Untitled

Jul 2nd, 2017
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.81 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. print ("Obtaining 25 comments in " + subreddit)
  23. for comment in r.subreddit(subreddit).comments(limit=25):
  24. didsomething = False
  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. if ("!status" in comment.body and comment.id not in comments_dealt_with and comment.author != r.user.me()):
  41. print ("'!STATUS' FOUND IN COMMENT " + comment.id)
  42. comment.reply(config.status_text + config.footer_text)
  43. didsomething = True
  44.  
  45. if ("!resetbalance" in comment.body and comment.id not in comments_dealt_with and comment.author != r.user.me()):
  46. print ("resetbalance called, resetting balance")
  47. r.subreddit(config.flair_subreddit).flair.set(comment.author, "1000")
  48. comment.reply(config.reset_text + config.footer_text)
  49. didsomething = True
  50.  
  51. if ("!checkbalance" in comment.body and comment.id not in comments_dealt_with and comment.author != r.user.me()):
  52. print ("checkbalance called, checking balance")
  53. comment.reply(config.checkbal_text(currentMoney) + config.footer_text)
  54. didsomething = True
  55.  
  56. try:
  57. if "!coinflip" in thecomment:
  58. betSize, betChoice = re.match(r".*!coinflip \[(\d+)] \(([HT])\)", thecomment).groups()
  59. betSize = int(betSize)
  60. if currentMoney < betSize:
  61. comment.reply(config.low_money_text(currentMoney) + config.footer_text)
  62. print ("Not enough coins, replied")
  63. didsomething = True
  64. else:
  65. flip = random.choice(["Heads", "Tails"])
  66. if flip[0] == betChoice:
  67. r.subreddit(config.flair_subreddit).flair.set(comment.author, str(currentMoney + betSize))
  68. comment.reply(config.win_text(flip, betSize, currentMoney + betSize) + config.footer_text)
  69. else:
  70. r.subreddit(config.flair_subreddit).flair.set(comment.author, str(currentMoney - betSize))
  71. comment.reply(config.lose_text(flip, betSize, currentMoney - betSize) + config.footer_text)
  72. didsomething = True
  73. except (AttributeError, ValueError):
  74. print ("Invalid formatting, ignoring")
  75. comment.reply("Incorrect formatting, try !coinflip [amount] (H or T)" + config.footer_text)
  76. didsomething = True
  77.  
  78.  
  79. ## try:
  80. ## if "!setcoins" in thecomment:
  81. ## user, amount = re.match(r".*!sendcoins \[(\d+)] \(([HT])\)", thecomment).groups()
  82. ## except (AttributeError, ValueError):
  83. ## print("nope")
  84.  
  85.  
  86. if didsomething:
  87. print ("Replied to comment " + comment.id)
  88. comments_dealt_with.append(comment.id)
  89. with open ("comments_dealt_with.txt", "a") as a:
  90. a.write(comment.id + "\n")
  91.  
  92. print ("Sleeping for 10 seconds...")
  93. time.sleep(10)
  94.  
  95. def get_saved_comments():
  96. global comments_dealt_with
  97. if not os.path.isfile("comments_dealt_with.txt"):
  98. comments_dealt_with = []
  99. else:
  100. with open("comments_dealt_with.txt", "r") as f:
  101. comments_dealt_with = f.read()
  102. comments_dealt_with = comments_dealt_with.split("\n")
  103. #comments_dealt_with = filter(None, comments_dealt_with)
  104.  
  105.  
  106. bot_login()
  107. get_saved_comments()
  108.  
  109. while True:
  110. run_bot()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement