stock-tracker-discord/main.py

138 lines
3.5 KiB
Python

import discord
import database, table, yfi
import table2 as t2
from discord.ext import commands
from config import config
from decimal import Decimal
import typing
intents = discord.Intents.default()
bot = commands.Bot(
command_prefix="$",
intents=intents,
)
@bot.command()
async def ping(ctx):
await ctx.send("pong!")
@bot.command(aliases=['pf'])
async def portfolio(ctx):
user = str(ctx.message.author)
return await ctx.send(str( "```" + table.generate_table(database.get_stocks(user)) + "```" ))
@bot.command()
async def pf2(ctx):
user = str(ctx.message.author)
portfolio = database.get_stocks(user)
data = []
for stock in portfolio.keys():
data.append([
stock,
Decimal(portfolio[stock]),
round(float(Decimal(portfolio[stock]) * Decimal(yfi.get_current_price(stock))), 2)
])
data.append(["Total", "", round( sum([x[2] for x in data]), 2) ] )
image = t2.generate_table(
headers = ["Ticker", "Shares", "Value ($)"],
data = data,
title = str(user + "'s Portfolio")
)
await ctx.send(file=discord.File(image, 'table.png'))
image.close()
@bot.command()
async def add(ctx, stock, amount):
user = str(ctx.message.author)
stock = stock.upper()
try:
float(amount)
except ValueError:
await ctx.send("amoutn not a number")
return
if database.add_stock(user, stock, amount):
return await ctx.send(str( "```" + table.generate_table(database.get_stocks(user)) + "```" ))
else:
return await ctx.send("Stock **{0}** does not exist!".format(stock))
@bot.command(aliases=['del'])
async def delete(ctx, stock):
stock = stock.upper()
user = str(ctx.message.author)
return await ctx.send(str( "```" + table.generate_table(database.delete_stock(user, stock)) + "```" ))
@bot.command(aliases=['wl'])
async def watchlist(ctx):
user = str(ctx.message.author)
wlist = database.get_watchlist(user)
return await ctx.send( str ( "```" + table.watchlist_table(wlist) + "```") )
@bot.command(aliases=['w'])
async def watch(ctx, stock, est_price: typing.Optional[int]):
if not est_price:
est_price = 0
user = str(ctx.message.author)
stock = stock.upper()
try:
float(est_price)
except ValueError:
await ctx.send("Estimated price not a number")
return
if database.watch(user, stock, est_price):
return await ctx.send("Updated watchlist")
else:
return await ctx.send("Stock **{0}** does not exist!".format(stock))
@bot.command(aliases=['uw'])
async def unwatch(ctx, *stocks):
user = str(ctx.message.author)
stocks = [stock.upper() for stock in stocks]
for stock in stocks:
database.unwatch(user, stock)
return await ctx.send("Updated watchlist")
@bot.command()
async def wl2(ctx, user: typing.Optional[discord.Member]):
if not user:
user = str(ctx.message.author)
else:
print(str(user))
user = str(user)
watchlist = database.get_watchlist(user)
data = []
for stock in watchlist.keys():
data.append([
stock,
yfi.get_current_price(stock),
Decimal(watchlist[stock]),
])
image = t2.generate_table(
headers = ["Ticker", "Value ($)", "Est. Price ($)"],
data = data,
title = str(user + "'s Watchlist"),
bg = 'lightgreen', border='limegreen'
)
await ctx.send(file=discord.File(image, 'table.png'))
image.close()
bot.run(config['apikey'])