Example #1
0
    async def prices(ctx):
        db.check_user(ctx.message, conn)
        results = db.get_last_prices(conn, 13)
        previousPrice = 0
        resultHistory = []
        outputString = ''

        for result in reversed(results):
            price = result[0]
            diff = float("{:.2f}".format(price - previousPrice))
            previousPrice = price
            resultHistory.append((price, diff))
        del resultHistory[0]
        count = 0
        for result in reversed(resultHistory):
            hisPrice = result[0]
            hisDiff = result[1]
            if hisDiff < 0:
                symbol = '\U0001F534'
                diff = '-$' + '{:.2f}'.format(abs(hisDiff))
                perc = '-{:.2f}%'.format(abs(hisDiff / hisPrice))
            elif result[1] > 0:
                symbol = '\U0001F7E2'
                diff = '+$' + '{:.2f}'.format(abs(hisDiff))
                perc = '+{:.2f}%'.format(abs(hisDiff / hisPrice))
            else:
                symbol = '\U000026AA'
                diff = '$0.00'
            hisPrice = '{:.2f}'.format(float(hisPrice))
            outputString = f'{outputString}[{symbol} {perc}] **${hisPrice}** {diff} '
            if count < 1:
                outputString = f'{outputString} | **CURRENT**\n'
                count += 30
            else:
                if (count < 60):
                    outputString = f'{outputString} | {count%60} minutes ago.\n'
                elif (count % 60 == 0):
                    outputString = f'{outputString} | {int(math.floor(count/60))} hours ago.\n'
                else:
                    outputString = f'{outputString} | {int(math.floor(count/60))} hours and {count%60} minutes ago.\n'
                count += 30
        pricesEmbed = discord.Embed(title='Coffee Price History',
                                    description=outputString,
                                    colour=conf.colourCoffee)
        nextTickTime = 1800 - int(math.floor(time.time() % 1800))
        footerText = ''
        if (nextTickTime > 60):
            footerText = f'{footerText}Next tick : {int(math.floor(nextTickTime/60))} minutes and {int(math.floor(nextTickTime%60))} seconds.\n'
        else:
            footerText = f'{footerText}Next tick : {int(math.floor(nextTickTime%60))} seconds.\n'
        footerText = f'{footerText}Prices are updated every 30 minutes.'
        pricesEmbed.set_footer(text=footerText)
        await ctx.message.channel.send(embed=pricesEmbed)
Example #2
0
def runCoffeeStocks(conn):
    global tickRate
    while True:
        tickTime = time.time()
        if round(tickTime % tickRate) == 0:
            lastPrices = db.get_last_prices(conn, 1)
            oldPrice = lastPrices[0][0]
            newPrice = setSellPrice(oldPrice)
            db.insert_price_tick(tickTime, newPrice, conn)
            ''' Debug Only '''
            '''
            debugLastPrices = db.get_last_prices(conn, 1)
            debugNewestPrice = debugLastPrices[0][0]
            print(f'New stock price : {debugNewestPrice}')
            '''
            time.sleep(tickRate - 2)
        else:
            time.sleep(0.5)