def trigger_clear_bets(chan): db = Database() bets_data = db.get_all_bets_data() try: #Get the oldest date oldest_date = datetime.strptime(bets_data[0][5], "%Y %m %d %H %M %S") now = datetime.utcnow() if now - oldest_date >= timedelta(30): if not os.path.exists(os.path.join('dump')): os.makedirs(os.path.join('dump')) path = os.path.join( "dump", "bets_data_" + datetime.strftime(now, "%Y_%m_%d_%H_%M_%S") + ".csv") with open(path, 'wb') as outfile: wr = csv.writer(outfile) header = ("id", "channel", "username", "amount", "outcome", "date") wr.writerow(header) for row in bets_data: wr.writerow(row) db.clear_bets() except Exception as error: print error
def trigger_less_than_200_for_all(chan): try: db = Database() channel = chan.lstrip("#") str_last_time = db.get_last_time_points_reset(channel)[0] if str_last_time: now = datetime.utcnow() last_time = datetime.strptime(str_last_time, "%Y %m %d %H %M %S") if now - last_time >= timedelta(1): usernames = db.get_all_usernames() for username in usernames: points = db.get_points(username)[0] if points < 200: db.set_points(username, 200) strnow = datetime.strftime(datetime.utcnow(), "%Y %m %d %H %M %S") db.set_last_time_points_reset(channel, strnow) return "Points have been reloaded for users who have less than 200!" else: strnow = datetime.strftime(datetime.utcnow(), "%Y %m %d %H %M %S") db.set_last_time_points_reset(channel, strnow) except Exception as error: print error
def __init__(self): self.irc = globals.irc self.channel = globals.CURRENT_CHANNEL self.chan = "#" + self.channel self.db = Database() #self.bets_started = datetime.strptime(self.db.get_bets_started(self.channel)[0], "%Y %m %d %H %M %S") #Generates list of tuples(username, amount, outcome, date) that only consists of bidders that bet after the bets have started self.bets_data = [x for x in self.db.get_bets_data(self.channel) if datetime.strptime(x[3], "%Y %m %d %H %M %S") > datetime.strptime(self.db.get_bets_started(self.channel)[0], "%Y %m %d %H %M %S")]
def __init__(self, irc, chan, delay): Thread.__init__(self, target=self.main) self.daemon = True self.delay = delay self.now = time.time() self.chan = chan self.channel = self.chan.lstrip("#") self.irc = irc self.db = Database()
def points(): username = globals.CURRENT_USER db = Database() try: points = int(db.get_points(username)[0]) except Exception as error: print error return "Couldn't show your points, sorry!" return "You have {0} points!".format(points)
def addquote(args): db = Database() user = globals.CURRENT_USER channel = globals.CURRENT_CHANNEL quote = unicode(args[0].strip().strip("\"").strip("\'"), 'utf-8') if len(quote) > 300: return "Let's keep it below 300 characters?" game = get_stream_game(channel) db.add_quote(channel, user, quote, game) return "{0} added!".format(quote)
def result(): db = Database() channel = globals.CURRENT_CHANNEL try: outcome = db.get_last_result(channel)[0] return "Last outcome was: {0}".format(outcome) except: print error return "There were no bets yet"
def top10(): db = Database() channel = globals.CURRENT_CHANNEL rank_data = db.get_top10(channel) # [(1, u'singlerider', 441, u'ravenbot007'), (2, u'nano_machina', 129, u'ravenbot007')] candidates = ", ".join([ str(i + 1) + ") " + x[1] + ": " + str(x[2]) for i, x in enumerate(rank_data) ]) resp = "The top 10 cash holders are... " + candidates return resp
def __init__(self, config): self.db = Database() self.db.initiate() self.config = config self.crons = crons src.lib.command_headers.initalizeCommands(config) self.irc = irc_.irc(config) # start threads for channels that have cron messages to run cron.initialize_crons(self.irc, self.crons.crons.get("crons", {})) cron.initialize_channel_id_check(self.config) globals.irc = self.irc
def rem(args): db = Database() command = args[0].lower() channel = globals.CURRENT_CHANNEL command_data = db.get_command(command, channel) if command_data: db.remove_command(command, channel) return "{0} removed from Ravenbot007's custom commands!".format( command) else: return "{0} not found.".format(command)
def quote(): db = Database() channel = globals.CURRENT_CHANNEL # (1, u'testchannel', u'testuser', u'quote', 1, u'testgame') quote_data = db.get_quote(channel) if quote_data is None: return "No quotes found. Why not add one with '!addquote [quote]'?" else: quote = str(quote_data[3]) quote_number = quote_data[4] game = quote_data[5] resp = "Quote #{0}: \"{1}\" [{2}]".format(quote_number, quote, game) return resp
def stop(): channel = globals.CURRENT_CHANNEL chan = "#" + channel db = Database() bets = db.are_bets(channel)[0] if bets: delay = 30 initialize(chan, delay) else: return "You haven't started a bet yet!" return "You have started the countdown"
def chance(): db = Database() channel = globals.CURRENT_CHANNEL user = globals.CURRENT_USER g = Gamble(channel) points = abs(g.rob_yield(multiplier=1)) db.add_user([user], channel) db.modify_points(user, channel, points) print user, points, channel if points == 0: resp = "Nothing this time! Try again in a half hour?" else: resp = "You got {0} cash!".format(points) return resp
def start(): db = Database() channel = globals.CURRENT_CHANNEL bets = db.are_bets(channel)[0] if not bets: chan = "#" + channel irc = globals.irc db.set_bets(channel, 1) date = datetime.strftime(datetime.utcnow(), "%Y %m %d %H %M %S") db.set_bets_started(channel, date) msg = "Bets are open! Type !win or !lose with the amount of points you want to bet, example: !win 200" irc.send_message(chan, msg) return "You have opened the bets" else: return "First, resolve current bets"
def win(args): db = Database() channel = globals.CURRENT_CHANNEL bets = db.are_bets(channel)[0] if bets: username = globals.CURRENT_USER if db.get_user(username) == None: db.add_user([username]) db.modify_points(username, 1000) current_points = db.get_points(username)[0] if (args[0][-1] == "%"): try: percentage = int(args[0].strip("%")) except Exception as error: print error return "Sorry, but your betting amount should be a number or a percentage" if percentage > 100: return "Sorry, but you can't bet more than a 100%" else: multiply = float(percentage) / 100 amount = int(current_points * multiply) else: try: amount = int(args[0]) except Exception as error: print error return "Sorry, but your betting amount should be a number or a percentage" if amount > current_points: return "You currently have {0} points, use them wisely.".format( current_points) date = datetime.strftime(datetime.utcnow(), "%Y %m %d %H %M %S") db.add_bidder(channel, username, amount, "win", date) db.modify_points(username, -amount) return "Bet successfully placed! You bet {0} that {1} will win".format( amount, channel) else: return "Sorry, bets were not started yet"
def add(args): db = Database() command = args[0].lower() user_level = args[1] response = " ".join(args[2:]) creator = globals.CURRENT_USER channel = globals.CURRENT_CHANNEL if command[0] is "!": if command not in command_headers.commands: if user_level == "reg" or user_level == "mod": db.add_command(creator, command, response, user_level, channel) return "{0} added to Ravenbot007's custom commands!".format( command) else: return "User level must be 'reg' or 'mod'" else: return "{0} already built in to Ravenbot007.".format(command) else: return "Command must begin with '!'"
def outcome(args): result = args[0] if result != "win" and result != "lose": return "Sorry, but you didn't specified the outcome. You should write !outcome win/lose" else: db = Database() channel = globals.CURRENT_CHANNEL bets = db.are_bets(channel)[0] if not bets: bts = Bets() bts.distribute_profit(result) db.set_last_result(channel, result) date = datetime.strftime(datetime.utcnow(), "%Y %m %d %H %M %S") #So the outcome won't be used twice for same bets db.set_bets_started(channel, date) else: return "The bets are still going on!" return "You have distributed the profit"
def edit(args): db = Database() command = args[0].lower() user_level = args[1] response = " ".join(args[2:]) creator = globals.CURRENT_USER channel = globals.CURRENT_CHANNEL if command[0] is "!": if command not in command_headers.commands: command_data = db.get_command(command, channel) if command_data: if user_level == "reg" or user_level == "mod": db.modify_command(command, response, channel, user_level) return "{0} has been changed!".format(command) else: return "User level must be 'reg' or 'mod'" else: return "{0} not found.".format(command) else: return "{0} already built in to Ravenbot007.".format(command) else: return "Command must begin with '!'"
def claim(): username = globals.CURRENT_USER channel = globals.CURRENT_CHANNEL db = Database() points = db.get_points(channel, username)[0] if points < 200: now = datetime.utcnow() str = db.get_last_changed_below_200(channel, username)[0] last_changed = datetime.strptime(str, "%Y %m %d %H %M %S") if now - last_changed >= timedelta(1): db.set_points(channel, username, 200) strnow = datetime.strftime(datetime.utcnow(), "%Y %m %d %H %M %S") db.set_last_changed_below_200(channel, username, strnow) return "Your points have been reloaded from {0} to 200 points. Points reload once every 24h".format( points) else: return "Sorry, but 24h have not yet passed" else: return "Sorry, but you have more than 200 points"
def join(): channel = globals.CURRENT_CHANNEL user = globals.CURRENT_USER db = Database() g = Gamble(channel) if g.check_gamble() is not None: user_is_already_gambling = g.get_gamble_user(user) if user_is_already_gambling: return "You're already gambling. Perhaps you need a 12 step program?" points = globals.channel_info[ globals.CURRENT_CHANNEL]['gamble']["points"] if db.get_user(user, channel): if db.get_user(user, channel)[2] < points: return "You don't have enough cash!" else: return "You've got no cash!" globals.channel_info[ globals.CURRENT_CHANNEL]['gamble']["users"][user] = True return "{0} has joined the action and is on the hook for {1} cash!".format( user, points) else: return "There's no gamble to join. Start one with '!gamble [amount]'!"
def gift(args): user = globals.CURRENT_USER recipient = args[0].lower().lstrip("@") channel = globals.CURRENT_CHANNEL try: amount = abs(int(args[1])) except: return "Amount has to be a number!" if recipient == user: return "You can't gift yourself cash!" db = Database() if db.get_user(user, channel): if db.get_user(user, channel)[2] >= amount and db.get_user( recipient, channel): db.modify_points(recipient, channel, amount) db.modify_points(user, channel, amount * -1) return "{0} cash has been debited to {1}!".format( amount, recipient) else: return "Those numbers just don't add up. Check your spelling!" else: return "You don't even have any cash!"
def cron(channel): try: channel = channel.lstrip("#") from src.lib.twitch import get_stream_status, get_hosts from src.lib.channel_data import ChannelData from src.lib.queries import Database cd = ChannelData(channel) if get_stream_status(): channel_id = cd.get_channel_id_from_db()[0] hosts = get_hosts(channel_id) unthanked_users = [] for host in hosts: host_data = cd.get_channel_data_by_user( host["host_login"], "host") if not host_data: cd.insert_channel_data(host["host_login"], "host") db = Database() db.add_user([host["host_login"]], channel) db.modify_points(host["host_login"], channel, 100) unthanked_users.append(host["host_login"]) if len(unthanked_users) == 1: user = unthanked_users[0] resp = "Thank you {0} for the host! Here's 100 cash!".format( user) globals.irc.send_message("#" + channel, resp) elif len(unthanked_users) > 1: import globals resp = "The following users are receiving 100 cash for hosting: " + ", ".join( unthanked_users) + "!" globals.irc.send_message("#" + channel, resp) elif len(unthanked_users) > 10: resp = "Thanks to the {0} people hosting! Each of you get 100 cash!".format( len(unthanked_users)) globals.irc.send_message("#" + channel, resp) else: cd.remove_channel_data("host") except Exception as error: print error
def gamble(args): db = Database() try: points = int(args[0]) except: return "The points you gamble have to be a number!" if points < 10: return "The minimum buy-in amount is 10 cash!" channel = globals.CURRENT_CHANNEL user = globals.CURRENT_USER delay = 60 if db.get_user(user, channel): if db.get_user(user, channel)[2] < points: return "You don't have enough cash!" else: return "You've got no cash!" g = Gamble(channel) if g.check_gamble() is None: globals.channel_info[channel]['gamble']["users"][ user] = points initialize(channel, user, delay, points) else: return "There is already a gamble in progress!"
def __init__(self, channel): self.channel = channel self.db = Database()
def __init__(self, channel="testchannel", user="******", points=0): self.db = Database() self.channel = channel self.user = user self.points = points