diff --git a/stockBot.py b/stockBot.py index 311a19a..c0699e9 100644 --- a/stockBot.py +++ b/stockBot.py @@ -5,6 +5,7 @@ import discord import re import urllib.request import credentials +from datetime import datetime # Make sure to update credentials.py with your secrets TOKEN = credentials.secrets['TOKEN'] @@ -30,24 +31,57 @@ async def on_message(message): content = message.content channel = message.channel - # regex to find tickers in messages - tickers = re.findall('[$](\w{1,4})', content) + try: + # regex to find tickers in messages + tickers = re.findall('[$](\w{1,4})', content) - # get ticker information from bravos api - url = 'https://data.bravos.co/v1/quote?symbols=' + ",".join(tickers) + \ - '&apikey=' + BRAVOS_API + '&format=json' + # get ticker information from bravos api + url = 'https://data.bravos.co/v1/quote?symbols=' + ",".join(tickers) + \ + '&apikey=' + BRAVOS_API + '&format=json' - # load json data from url as an object - with urllib.request.urlopen(url) as url: - data = json.loads(url.read().decode()) - for ticker in tickers: # iterate through the tickers and print relevant info one message at a time - try: # checks if data is a valid ticker, if it is not tells the user - nameTicker = data[ticker.upper()]['name'] - priceTicker = data[ticker.upper()]['price'] - await client.send_message(channel, 'The current stock price of ' + nameTicker + ' is $' + str(priceTicker)) - except KeyError: - await client.send_message(channel, ticker.upper() + ' does not exist.') - pass + # load json data from url as an object + with urllib.request.urlopen(url) as url: + data = json.loads(url.read().decode()) + for ticker in tickers: # iterate through the tickers and print relevant info one message at a time + try: # checks if data is a valid ticker, if it is not tells the user + nameTicker = data[ticker.upper()]['name'] + priceTicker = data[ticker.upper()]['price'] + if message.content.startswith('!news'): + embed = displayembed(ticker, nameTicker, priceTicker) + await client.send_message(channel, embed=embed) + else: + await client.send_message(channel, 'The current stock price of ' + nameTicker + ' is $**' + str(priceTicker) + '**') + except KeyError: + await client.send_message(channel, ticker.upper() + ' does not exist.') + pass + except: + pass + + +def displayembed(ticker, nameTicker, priceTicker): + + embed = discord.Embed( + title='News for ' + nameTicker, + description='The current stock price of ' + + nameTicker + ' is $**' + str(priceTicker) + '**', + color=0x50bdfe + ) + + embed.set_thumbnail( + url='https://g.foolcdn.com/art/companylogos/mark/' + ticker + '.png') + embed.add_field(name='Seeking Alpha', + value='https://seekingalpha.com/symbol/' + ticker, inline=True) + embed.add_field( + name='MSN Money', value='https://www.msn.com/en-us/money/stockdetails?symbol=' + ticker, inline=False) + embed.add_field(name='Yahoo Finance', + value='https://finance.yahoo.com/quote/' + ticker, inline=False) + embed.add_field(name='Wall Street Journal', + value='https://quotes.wsj.com/' + ticker, inline=False) + embed.add_field( + name='The Street', value='https://www.thestreet.com/quote/' + ticker + '.html', inline=False) + embed.add_field( + name='Zacks', value='https://www.zacks.com/stock/quote/' + ticker, inline=False) + return embed async def list_servers():