Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import praw
- import config
- import time
- import os
- import random
- import re
- def bot_login():
- print ("Loggin in...")
- global r
- r = praw.Reddit(username = config.username,
- password = config.password,
- client_id = config.client_id,
- client_secret = config.client_secret,
- user_agent = config.user_agent)
- print ("Logged in!")
- def run_bot():
- global r
- global comments_dealt_with
- for subreddit in config.subreddits:
- print ("Obtaining 25 comments in " + subreddit)
- for comment in r.subreddit(subreddit).comments(limit=25):
- if ("!casino" in comment.body and comment.id not in comments_dealt_with and comment.author != r.user.me()):
- print ("String with \"!casino\" found in comment " + comment.id)
- thecomment = comment.body
- flair = next(r.subreddit(config.flair_subreddit).flair(comment.author))
- if flair["flair_text"] is None:
- r.subreddit(config.flair_subreddit).flair.set(comment.author, "1000")
- flair["flair_text"] = "1000"
- print ("New user, setting balance to 1000")
- try:
- currentMoney = int(flair["flair_text"])
- except ValueError:
- print ("Flair is not an integer, ignoring, flair = " + flair["flair_text"])
- comments_dealt_with.append(comment.id)
- with open ("comments_dealt_with.txt", "a") as a:
- a.write(comment.id + "\n")
- continue
- if "!casino resetbal" in thecomment:
- print ("resetbalance called, resetting balance")
- r.subreddit(config.flair_subreddit).flair.set(comment.author, "1000")
- comment.reply(config.reset_text + config.footer_text)
- if "!casino checkbal" in thecomment:
- print ("checkbalance called, checking balance")
- comment.reply(config.checkbal_text(currentMoney) + config.footer_text)
- try:
- betSize, betChoice = re.match(r".*!casino coinflip \[(\d+)] \(([HT])\)", thecomment).groups()
- betSize = int(betSize)
- except (AttributeError, ValueError):
- print ("Invalid formatting, ignoring")
- comment.reply("Incorrect formatting, try !casino coinflip [amount] (H or T)")
- comments_dealt_with.append(comment.id)
- with open ("comments_dealt_with.txt", "a") as a:
- a.write(comment.id + "\n")
- continue
- if currentMoney < betSize:
- comment.reply(config.low_money_text(currentMoney) + config.footer_text)
- print ("Not enough coins, replied")
- comments_dealt_with.append(comment.id)
- with open ("comments_dealt_with.txt", "a") as a:
- a.write(comment.id + "\n")
- continue
- flip = random.choice(["H", "T"])
- if flip == betChoice:
- r.subreddit(config.flair_subreddit).flair.set(comment.author, str(currentMoney + betSize))
- comment.reply(config.win_text(flip, betSize, currentMoney + betSize) + config.footer_text)
- else:
- r.subreddit(config.flair_subreddit).flair.set(comment.author, str(currentMoney - betSize))
- comment.reply(config.lose_text(flip, betSize, currentMoney - betSize) + config.footer_text)
- print ("Replied to comment " + comment.id)
- comments_dealt_with.append(comment.id)
- with open ("comments_dealt_with.txt", "a") as a:
- a.write(comment.id + "\n")
- print ("Sleeping for 10 seconds...")
- time.sleep(10)
- def get_saved_comments():
- global comments_dealt_with
- if not os.path.isfile("comments_dealt_with.txt"):
- comments_dealt_with = []
- else:
- with open("comments_dealt_with.txt", "r") as f:
- comments_dealt_with = f.read()
- comments_dealt_with = comments_dealt_with.split("\n")
- #comments_dealt_with = filter(None, comments_dealt_with)
- bot_login()
- get_saved_comments()
- print (comments_dealt_with)
- while True:
- run_bot()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement