1
0
mirror of https://gitlab.com/simple-stock-bots/simple-discord-stock-bot.git synced 2025-06-16 23:26:53 +00:00

added comments

This commit is contained in:
Anson 2019-01-08 18:20:05 -07:00
parent 549cd9ba6d
commit 074692c77a

View File

@ -6,14 +6,15 @@ import re
import urllib.request import urllib.request
import credentials import credentials
BOT_PREFIX = ("?", "!") # Make sure to update credentials.py with your secrets
TOKEN = credentials.secrets['TOKEN'] TOKEN = credentials.secrets['TOKEN']
BRAVOS_API = credentials.secrets['BRAVOS_API'] BRAVOS_API = credentials.secrets['BRAVOS_API']
BOT_PREFIX = ("?", "!")
client = commands.Bot(command_prefix=BOT_PREFIX) client = commands.Bot(command_prefix=BOT_PREFIX)
@client.event @client.event # Make bot say when its ready
async def on_ready(): async def on_ready():
print('Bot is Ready!!!') print('Bot is Ready!!!')
@ -21,21 +22,26 @@ async def on_ready():
@client.event @client.event
async def on_message(message): async def on_message(message):
if message.author == client.user: if message.author == client.user: # Prevent bot from reacting to its own messages
return return
# define information about the message
author = message.author author = message.author
content = message.content content = message.content
channel = message.channel channel = message.channel
# regex to find tickers in messages
tickers = re.findall('[$](\w{1,4})', content) tickers = re.findall('[$](\w{1,4})', content)
print(tickers)
# get ticker information from bravos api
url = 'https://data.bravos.co/v1/quote?symbols=' + ",".join(tickers) + \ url = 'https://data.bravos.co/v1/quote?symbols=' + ",".join(tickers) + \
'&apikey=' + BRAVOS_API + '&format=json' '&apikey=' + BRAVOS_API + '&format=json'
print(url)
# load json data from url as an object
with urllib.request.urlopen(url) as url: with urllib.request.urlopen(url) as url:
data = json.loads(url.read().decode()) data = json.loads(url.read().decode())
for ticker in tickers: for ticker in tickers: # iterate through the tickers and print relevant info one message at a time
try: try: # checks if data is a valid ticker, if it is not tells the user
nameTicker = data[ticker.upper()]['name'] nameTicker = data[ticker.upper()]['name']
priceTicker = data[ticker.upper()]['price'] priceTicker = data[ticker.upper()]['price']
await client.send_message(channel, 'The current stock price of ' + nameTicker + ' is $' + str(priceTicker)) await client.send_message(channel, 'The current stock price of ' + nameTicker + ' is $' + str(priceTicker))