Ejemplo n.º 1
0
def buy(bot, trigger):
    name = trigger.nick
    try:
        shares = int(trigger.group(3))
        ticker = str(trigger.group(4).upper())
        
    except:
        try:
            shares = int(trigger.group(4))
            ticker = str(trigger.group(3).upper())
            
        except:
            bot.say('Enter which stock you\'d like to buy, and how many shares.')
            return
    
    if shares < 0:
        bot.say('Enter a valid amount of stocks')
        return
    if ticker not in stocks:
        bot.say('%s is not a valid stock.' % (ticker))
        return


    market = StockMarket()
    market.open_db(file_Name)
    price = market.get_balance(ticker)
    if price < 1:
            bot.say('Stock has crashed. Please wait until it is restored')
            return


    price_total = market.get_balance(ticker) * shares
    their_balance = gravecoin.check_balance(name)
    if int(price_total) > int(their_balance):
        max_shares = (int(their_balance) / int(price))
        bot.say('The price for %s shares of %s is ɷ %s. You only have ɷ %s. At most you can buy %s shares' % (shares,ticker,price_total,their_balance,max_shares))
        return
    bot.say('Buying %s shares of %s for ɷ %s ' %(shares,ticker,price_total))
    trade = StockTrade()
    try:
        trade.open_db(saved_stocks)
        trade.buy_stocks(name,ticker,shares,price)
        gravecoin.debit_user(name,price_total)
    except:
        bot.say('Something broken, you have probably not been charged. Probably')