Better credentials

This commit is contained in:
NikolajDanger
2020-08-09 13:02:04 +02:00
parent 9f683034c5
commit 84e332fa68
8 changed files with 56 additions and 49 deletions

View File

@ -3,16 +3,14 @@ import finnhub, json
from funcs import getName
from .money import checkBalance, addMoney
finnhubClient = finnhub.Client(api_key = "bsm16nvrh5rdb4ara3j0")
def getPrice(symbol : str):
def getPrice(symbol : str,finnhubClient):
res = finnhubClient.quote(symbol.upper())
if res == {}:
return 0
else:
return int(res["c"] * 100)
def getPortfolio(user : str):
def getPortfolio(user : str,finnhubClient):
with open("resources/games/investments.json") as f:
data = json.load(f)
@ -23,7 +21,7 @@ def getPortfolio(user : str):
for key, value in list(data[user].items()):
purchaseValue = value["purchased for"]
currentValue = int((getPrice(key) / value["value at purchase"]) * value["purchased"])
currentValue = int((getPrice(key,finnhubClient) / value["value at purchase"]) * value["purchased"])
if purchaseValue == "?":
portfolio += f"\n**{key}**: ___{str(currentValue)} GwendoBucks___"
else:
@ -31,10 +29,10 @@ def getPortfolio(user : str):
return portfolio
def buyStock(user : str, stock : str, buyAmount : int):
def buyStock(user : str, stock : str, buyAmount : int,finnhubClient):
if buyAmount >= 100:
if checkBalance(user) >= buyAmount:
stockPrice = getPrice(stock)
stockPrice = getPrice(stock,finnhubClient)
if stockPrice > 0:
with open("resources/games/investments.json", "r") as f:
data = json.load(f)
@ -68,7 +66,7 @@ def buyStock(user : str, stock : str, buyAmount : int):
else:
return "You cannot buy stocks for less than 100 GwendoBucks"
def sellStock(user : str, stock : str, sellAmount : int):
def sellStock(user : str, stock : str, sellAmount : int,finnhubClient):
if sellAmount > 0:
with open("resources/games/investments.json", "r") as f:
data = json.load(f)
@ -77,7 +75,7 @@ def sellStock(user : str, stock : str, sellAmount : int):
if user in data and stock in data[user]:
value = data[user][stock]
stockPrice = getPrice(stock)
stockPrice = getPrice(stock,finnhubClient)
data[user][stock]["purchased"] = int((stockPrice / value["value at purchase"]) * value["purchased"])
data[user][stock]["value at purchase"] = stockPrice
if value["purchased"] >= sellAmount:
@ -100,13 +98,13 @@ def sellStock(user : str, stock : str, sellAmount : int):
else:
return "no"
def parseInvest(content: str, user : str):
def parseInvest(content: str, user : str,finnhubClient):
if content.startswith("check"):
commands = content.split(" ")
if len(commands) == 1:
return getPortfolio(user)
return getPortfolio(user,finnhubClient)
else:
price = getPrice(commands[1])
price = getPrice(commands[1],finnhubClient)
if price == 0:
return f"{commands[1].upper()} is not traded on the american market."
else:
@ -117,7 +115,7 @@ def parseInvest(content: str, user : str):
commands = content.split(" ")
if len(commands) == 3:
try:
return buyStock(user,commands[1],int(commands[2]))
return buyStock(user,commands[1],int(commands[2]),finnhubClient)
except:
return "The command must be given as \"!invest buy [stock] [amount of GwendoBucks to purchase with]\""
else:
@ -127,7 +125,7 @@ def parseInvest(content: str, user : str):
commands = content.split(" ")
if len(commands) == 3:
try:
return sellStock(user,commands[1],int(commands[2]))
return sellStock(user,commands[1],int(commands[2]),finnhubClient)
except:
return "The command must be given as \"!invest sell [stock] [amount of GwendoBucks to sell stocks for]\""
else: