-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.py
77 lines (68 loc) · 2.36 KB
/
bot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import os
from discord.ext import commands
from dotenv import load_dotenv
import requests
import textwrap
import yfinance as yf
load_dotenv()
TOKEN = os.getenv('DISCORD_TOKEN')
GoldAPI_KEY = os.getenv("GoldAPI_KEY")
bot = commands.Bot(command_prefix="$")
@bot.command(name="quote", help="Fetches stock quote for provided ticker")
async def quote(ctx, symbol):
print("received")
CATEGORIES = ("shortName", "currentPrice", "open", "previousClose", "volume24Hr")
LABELS = {"shortName": "Name",
"currentPrice": "Current Price",
"open": "Market Open",
"previousClose": "Previous Close",
"volume24Hr": "24 Hour Volume"
}
ticker = yf.Ticker(symbol)
if not ticker.info["regularMarketPrice"]:
await ctx.send("`Invalid Ticker`")
return
response = "\n".join([f"{LABELS[category]}: {ticker.info[category]}" for category in CATEGORIES])
await ctx.send(f"```{response}```")
@bot.command(name="dd", help="Fetches information on provided ticker")
async def dd(ctx, symbol):
print("received")
ticker = yf.Ticker(symbol)
if not ticker.info["regularMarketPrice"]:
await ctx.send("`Invalid Ticker`")
return
response = "\n".join([f"{key}: {ticker.info[key]}" for key in ticker.info.keys()])
for line in textwrap.wrap(response, 1996):
await ctx.send(f"```{line}```")
@bot.command(name="spot", help="Returns spot price of provided precious metal")
async def spot(ctx, symbol):
print("recieved")
METALS = {
"gold": "XAU",
"au": "XAU",
"silver": "XAG",
"ag": "XAG",
"platinum": "XPT",
"pt": "XPT",
"palladium": "XPD",
"pd": "XPD"
}
if symbol.lower() not in METALS:
await ctx.send("`Invalid selection`")
return
url = f"https://www.goldapi.io/api/{METALS[symbol.lower()]}/USD/"
headers = {
"x-access-token": GoldAPI_KEY,
"Content-Type": "application/json"
}
payload = requests.get(url, headers=headers).json()
CATEGORIES = ("symbol", "prev_close_price", "open_price", "price")
LABELS = {
"symbol": "Symbol",
"prev_close_price": "Previous Close",
"open_price": "Open",
"price": "Price"
}
response = "\n".join([f"{LABELS[category]}: {payload[category]}" for category in CATEGORIES])
await ctx.send(f"```{response}```")
bot.run(TOKEN)