💰 Invest

This commit is contained in:
NikolajDanger
2021-04-18 14:06:39 +02:00
committed by Nikolaj
parent 9948f03913
commit 883f84f569
2 changed files with 220 additions and 81 deletions

View File

@ -1,111 +1,243 @@
import discord """
Contains functions relating to invest commands.
*Classes*
---------
Invest
Contains all the code for the invest commands.
"""
import discord # Used for embeds
from discord_slash.context import SlashContext # Used for type hints
class Invest(): class Invest():
"""
Contains all the invest functions.
*Methods*
---------
getPrice(symbol: str) -> int
getPortfolio(user: str) -> str
buyStock(user: str, stock: str: buyAmount: int) -> str
sellStock(user: str, stock: str, buyAmount: int) -> str
parseInvest(ctx: discord_slash.context.SlashContext,
parameters: str)
"""
def __init__(self, bot): def __init__(self, bot):
"""Initialize the class."""
self.bot = bot self.bot = bot
def getPrice(self, symbol : str): def getPrice(self, symbol: str):
"""
Get the price of a stock.
*Parameters*
------------
symbol: str
The symbol of the stock to get the price of.
*Returns*
---------
price: int
The price of the stock.
"""
res = self.bot.finnhubClient.quote(symbol.upper()) res = self.bot.finnhubClient.quote(symbol.upper())
if res == {}: if res == {}:
return 0 return 0
else: else:
return int(res["c"] * 100) return int(res["c"] * 100)
def getPortfolio(self, user : str): def getPortfolio(self, user: str):
userInvestments = self.bot.database["investments"].find_one({"_id":user}) """
Get the stock portfolio of a user.
if userInvestments in [None,{}]: *Parameters*
return f"{self.bot.databaseFuncs.getName(user)} does not have a stock portfolio." ------------
user: str
The id of the user to get the portfolio of.
*Returns*
---------
portfolio: str
The portfolio.
"""
investmentsDatabase = self.bot.database["investments"]
userInvestments = investmentsDatabase.find_one({"_id": user})
userName = self.bot.databaseFuncs.getName(user)
if userInvestments in [None, {}]:
return f"{userName} does not have a stock portfolio."
else: else:
portfolio = f"**Stock portfolio for {self.bot.databaseFuncs.getName(user)}**" portfolio = f"**Stock portfolio for {userName}**"
for key, value in list(userInvestments["investments"].items()): for key, value in list(userInvestments["investments"].items()):
purchaseValue = value["purchased for"] purchaseValue = value["purchased for"]
currentValue = int((self.getPrice(key) / value["value at purchase"]) * value["purchased"]) stockPrice = self.getPrice(key)
if purchaseValue == "?": valueAtPurchase = value["value at purchase"]
portfolio += f"\n**{key}**: ___{str(currentValue)} GwendoBucks___" purchasedStock = value["purchased"]
else: valueChange = (stockPrice / valueAtPurchase)
portfolio += f"\n**{key}**: ___{str(currentValue)} GwendoBucks___ (purchased for {str(purchaseValue)})" currentValue = int(valueChange * purchasedStock)
portfolio += f"\n**{key}**: ___{currentValue} GwendoBucks___"
if purchaseValue != "?":
portfolio += f" (purchased for {purchaseValue})"
return portfolio return portfolio
def buyStock(self, user : str, stock : str, buyAmount : int): def buyStock(self, user: str, stock: str, buyAmount: int):
if buyAmount >= 100: """
if self.bot.money.checkBalance(user) >= buyAmount: Buy an amount of a specific stock.
stockPrice = self.getPrice(stock)
if stockPrice > 0:
userInvestments = self.bot.database["investments"].find_one({"_id":user})
self.bot.money.addMoney(user,-1*buyAmount) *Paramaters*
stock = stock.upper() ------------
user: str
The id of the user buying.
stock: str
The symbol of the stock to buy.
buyAmount: int
The amount of GwendoBucks to use to buy the stock.
if userInvestments != None: *Returns*
userInvestments = userInvestments["investments"] ---------
if stock in userInvestments: sendMessage: str
value = userInvestments[stock] The message to return to the user.
newAmount = int((stockPrice / value["value at purchase"]) * value["purchased"]) + buyAmount """
if buyAmount < 100:
self.bot.database["investments"].update_one({"_id":user},
{"$set":{"investments."+stock+".value at purchase" : stockPrice}})
self.bot.database["investments"].update_one({"_id":user},
{"$set":{"investments."+stock+".purchased" : newAmount}})
if value["purchased for"] != "?":
self.bot.database["investments"].update_one({"_id":user},
{"$set":{"investments."+stock+".purchased for" : buyAmount}})
else:
self.bot.database["investments"].update_one({"_id":user},
{"$set":{"investments."+stock : {"purchased" : buyAmount, "value at purchase" : stockPrice,
"purchased for" : buyAmount}}})
else:
newUser = {"_id":user,"investments":{stock : {"purchased" : buyAmount, "value at purchase" : stockPrice, "purchased for" : buyAmount}}}
self.bot.database["investments"].insert_one(newUser)
return f"{self.bot.databaseFuncs.getName(user)} bought {buyAmount} GwendoBucks worth of {stock} stock"
else:
return f"{stock} is not traded on the american market."
else:
return "You don't have enough money for that"
else:
return "You cannot buy stocks for less than 100 GwendoBucks" return "You cannot buy stocks for less than 100 GwendoBucks"
elif self.bot.money.checkBalance(user) < buyAmount:
return "You don't have enough money for that"
elif self.getPrice(stock) <= 0:
return f"{stock} is not traded on the american market."
else:
investmentsDatabase = self.bot.database["investments"]
stockPrice = self.getPrice(stock)
userInvestments = investmentsDatabase.find_one({"_id": user})
def sellStock(self, user : str, stock : str, sellAmount : int): self.bot.money.addMoney(user, -1*buyAmount)
if sellAmount > 0: stock = stock.upper()
userInvestments = self.bot.database["investments"].find_one({"_id":user})["investments"] if userInvestments is not None:
userInvestments = userInvestments["investments"]
if stock in userInvestments:
value = userInvestments[stock]
valueChange = (stockPrice / value["value at purchase"])
currentValue = int(valueChange * value["purchased"])
newAmount = currentValue + buyAmount
valuePath = f"investments.{stock}.value at purchase"
updater = {"$set": {valuePath: stockPrice}}
investmentsDatabase.update_one({"_id": user}, updater)
purchasedPath = f"investments.{stock}.purchased"
updater = {"$set": {purchasedPath: newAmount}}
investmentsDatabase.update_one({"_id": user}, updater)
if value["purchased for"] != "?":
purchasedForPath = f"investments.{stock}.purchased for"
updater = {"$set": {purchasedForPath: buyAmount}}
investmentsDatabase.update_one({"_id": user}, updater)
else:
updater = {
"$set": {
"investments.{stock}": {
"purchased": buyAmount,
"value at purchase": stockPrice,
"purchased for": buyAmount
}
}
}
investmentsDatabase.update_one({"_id": user}, updater)
else:
newUser = {
"_id": user,
"investments": {
stock: {
"purchased": buyAmount,
"value at purchase": stockPrice,
"purchased for": buyAmount
}
}
}
investmentsDatabase.insert_one(newUser)
userName = self.bot.databaseFuncs.getName(user)
sendMessage = "{} bought {} GwendoBucks worth of {} stock"
sendMessage = sendMessage.format(userName, buyAmount, stock)
return sendMessage
def sellStock(self, user: str, stock: str, sellAmount: int):
"""
Sell an amount of a specific stock.
*Paramaters*
------------
user: str
The id of the user selling.
stock: str
The symbol of the stock to sell.
buyAmount: int
The amount of GwendoBucks to sell for.
*Returns*
---------
sendMessage: str
The message to return to the user.
"""
if sellAmount <= 0:
return "no"
else:
investmentsDatabase = self.bot.database["investments"]
userData = investmentsDatabase.find_one({"_id": user})
userInvestments = userData["investments"]
stock = stock.upper() stock = stock.upper()
if userInvestments != None and stock in userInvestments: if userInvestments is not None and stock in userInvestments:
value = userInvestments[stock] value = userInvestments[stock]
stockPrice = self.getPrice(stock) stockPrice = self.getPrice(stock)
self.bot.database["investments"].update_one({"_id":user}, priceChange = (stockPrice / value["value at purchase"])
{"$set":{"investments."+stock+".purchased" : purchasedAmount = int(priceChange * value["purchased"])
int((stockPrice / value["value at purchase"]) * value["purchased"])}}) purchasedPath = f"investments.{stock}.purchased"
self.bot.database["investments"].update_one({"_id":user}, updater = {"$set": {purchasedPath: purchasedAmount}}
{"$set":{"investments."+stock+".value at purchase" : stockPrice}}) investmentsDatabase.update_one({"_id": user}, updater)
valueAtPurchasePath = f"investments.{stock}.value at purchase"
updater = {"$set": {valueAtPurchasePath: stockPrice}}
investmentsDatabase.update_one({"_id": user}, updater)
if value["purchased"] >= sellAmount: if value["purchased"] >= sellAmount:
self.bot.money.addMoney(user,sellAmount) self.bot.money.addMoney(user, sellAmount)
if sellAmount < value["purchased"]: if sellAmount < value["purchased"]:
self.bot.database["investments"].update_one({"_id":user}, purchasedPath = f"investments.{stock}.purchased"
{"$inc":{"investments."+stock+".purchased" : -sellAmount}}) updater = {"$inc": {purchasedPath: -sellAmount}}
investmentsDatabase.update_one({"_id": user}, updater)
self.bot.database["investments"].update_one({"_id":user}, purchasedForPath = f"investments.{stock}.purchased for"
{"$set":{"investments."+stock+".purchased for" : "?"}}) updater = {"$set": {purchasedForPath: "?"}}
investmentsDatabase.update_one({"_id": user}, updater)
else: else:
self.bot.database["investments"].update_one({"_id":user}, updater = {"$unset": {f"investments.{stock}": ""}}
{"$unset":{"investments."+stock:""}}) investmentsDatabase.update_one({"_id": user}, updater)
return f"{self.bot.databaseFuncs.getName(user)} sold {sellAmount} GwendoBucks worth of {stock} stock" userName = self.bot.databaseFuncs.getName(user)
sendMessage = "{} sold {} GwendoBucks worth of {} stock"
return sendMessage.format(userName, sellAmount, stock)
else: else:
return f"You don't have enough {stock} stocks to do that" return f"You don't have enough {stock} stocks to do that"
else: else:
return f"You don't have any {stock} stock" return f"You don't have any {stock} stock"
else:
return "no"
async def parseInvest(self, ctx, parameters): async def parseInvest(self, ctx: SlashContext, parameters: str):
"""
Parse an invest command. TO BE DELETED.
*Parameters*
------------
ctx: discord_slash.context.SlashContext
The context of the slash command.
parameters: str
The parameters of the command.
"""
await self.bot.defer(ctx) await self.bot.defer(ctx)
user = f"#{ctx.author.id}" user = f"#{ctx.author.id}"
@ -116,34 +248,39 @@ class Invest():
else: else:
price = self.getPrice(commands[1]) price = self.getPrice(commands[1])
if price == 0: if price == 0:
response = f"{commands[1].upper()} is not traded on the american market." response = "{} is not traded on the american market."
response = response.format(commands[0].upper())
else: else:
price = f"{price:,}".replace(",",".") price = f"{price:,}".replace(",", ".")
response = f"The current {commands[1].upper()} stock is valued at **{price}** GwendoBucks" response = self.bot.longStrings["Stock value"]
response = response.format(commands[1].upper(), price)
elif parameters.startswith("buy"): elif parameters.startswith("buy"):
commands = parameters.split(" ") commands = parameters.split(" ")
if len(commands) == 3: if len(commands) == 3:
response = self.buyStock(user,commands[1],int(commands[2])) response = self.buyStock(user, commands[1], int(commands[2]))
else: else:
response = "You must give both a stock name and an amount of gwendobucks you wish to spend." response = self.bot.longStrings["Stock parameters"]
elif parameters.startswith("sell"): elif parameters.startswith("sell"):
commands = parameters.split(" ") commands = parameters.split(" ")
if len(commands) == 3: if len(commands) == 3:
try: response = self.sellStock(user, commands[1], int(commands[2]))
response = self.sellStock(user,commands[1],int(commands[2]))
except:
response = "The command must be given as \"/invest sell [stock] [amount of GwendoBucks to sell stocks for]\""
else: else:
response = "You must give both a stock name and an amount of GwendoBucks you wish to sell stocks for." response = self.bot.longStrings["Stock parameters"]
else: else:
response = "Incorrect parameters" response = "Incorrect parameters"
if response.startswith("**"): if response.startswith("**"):
responses = response.split("\n") responses = response.split("\n")
em = discord.Embed(title=responses[0],description="\n".join(responses[1:]),colour=0x00FF00) text = "\n".join(responses[1:])
embedParams = {
"title": responses[0],
"description": text,
"colour": 0x00FF00
}
em = discord.Embed(*embedParams)
await ctx.send(embed=em) await ctx.send(embed=em)
else: else:
await ctx.send(response) await ctx.send(response)

View File

@ -8,5 +8,7 @@
"Blackjack different cards": "You can only split if your cards have the same value", "Blackjack different cards": "You can only split if your cards have the same value",
"Blackjack split": "Splitting {}'s hand into 2. Adding their original bet to the second hand. You can use \"/blackjack hit/stand/double 1\" and \"/blackjack hit/stand/double 2\" to play the different hands.", "Blackjack split": "Splitting {}'s hand into 2. Adding their original bet to the second hand. You can use \"/blackjack hit/stand/double 1\" and \"/blackjack hit/stand/double 2\" to play the different hands.",
"Blackjack started": "Blackjack game started. Use \"/blackjack bet [amount]\" to enter the game within the next 30 seconds.", "Blackjack started": "Blackjack game started. Use \"/blackjack bet [amount]\" to enter the game within the next 30 seconds.",
"Blackjack going on": "There's already a blackjack game going on. Try again in a few minutes." "Blackjack going on": "There's already a blackjack game going on. Try again in a few minutes.",
"Stock value": "The current {} stock is valued at **{}** GwendoBucks",
"Stock parameters": "You must give both a stock name and an amount of GwendoBucks you wish to spend."
} }