Advertisement
dmesticg

Untitled

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