feat: seperate commands into cogs (categories)
This commit is contained in:
0
cogs/__init__.py
Normal file
0
cogs/__init__.py
Normal file
60
cogs/finance.py
Normal file
60
cogs/finance.py
Normal file
@@ -0,0 +1,60 @@
|
|||||||
|
import os
|
||||||
|
import requests
|
||||||
|
import json
|
||||||
|
from discord.ext import commands
|
||||||
|
|
||||||
|
LUNAR_CRUSH_API_KEY = os.getenv('LUNAR_CRUSH_API_KEY')
|
||||||
|
ALPHAVANTAGE_API_KEY = os.getenv('ALPHAVANTAGE_API_KEY')
|
||||||
|
|
||||||
|
class Finance(commands.Cog):
|
||||||
|
"""Commands to query stock and crypto prices"""
|
||||||
|
|
||||||
|
@commands.command()
|
||||||
|
async def stock(ctx, *tickers):
|
||||||
|
"""!stock [tickers] - example usage '!stock TSLA GOOG'"""
|
||||||
|
|
||||||
|
if len(tickers) == 0:
|
||||||
|
tickers = ["AAPL", "GOOG", "MSFT", "AMZN"]
|
||||||
|
|
||||||
|
# msg response setup
|
||||||
|
msg_response = "Stock prices for today!\n"
|
||||||
|
stock_found = False
|
||||||
|
|
||||||
|
# make the call for stock prices
|
||||||
|
for ticker in tickers:
|
||||||
|
http_response = requests.get(f'https://www.alphavantage.co/query?function=GLOBAL_QUOTE&symbol={ticker}&apikey={ALPHAVANTAGE_API_KEY}')
|
||||||
|
stock_data = json.loads(http_response.text)
|
||||||
|
|
||||||
|
# check if stock exists
|
||||||
|
if 'Global Quote' not in stock_data:
|
||||||
|
continue
|
||||||
|
|
||||||
|
# get the stock price
|
||||||
|
stock_price = stock_data['Global Quote']['05. price']
|
||||||
|
msg_response += f'**{ticker}**: ${round(float(stock_price), 2)}\n'
|
||||||
|
stock_found = True
|
||||||
|
|
||||||
|
|
||||||
|
if not stock_found:
|
||||||
|
await ctx.send("No stock data found for given tickers!")
|
||||||
|
return
|
||||||
|
|
||||||
|
await ctx.send(msg_response)
|
||||||
|
|
||||||
|
@commands.command()
|
||||||
|
async def crypto(ctx, *tickers):
|
||||||
|
"""!crypto [tickers] - example usage '!crypto BTC LTC'"""
|
||||||
|
|
||||||
|
# default tickers
|
||||||
|
if len(tickers) == 0:
|
||||||
|
tickers = ["BTC", "ETH", "LTC"]
|
||||||
|
|
||||||
|
# make the call for prices
|
||||||
|
http_response = requests.get(f"https://api.lunarcrush.com/v2?data=assets&key={LUNAR_CRUSH_API_KEY}&symbol={','.join(tickers)}")
|
||||||
|
data = json.loads(http_response.text)
|
||||||
|
|
||||||
|
# extract prices and derive message response
|
||||||
|
msg_response = "Cryptocurrency prices for today!\n"
|
||||||
|
msg_response += "\n".join([f"**{item['symbol']}**: ${round(float(item['price']), 2)}" for item in data["data"]])
|
||||||
|
|
||||||
|
await ctx.send(msg_response)
|
||||||
9
cogs/python.py
Normal file
9
cogs/python.py
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
from discord.ext import commands
|
||||||
|
|
||||||
|
class Python(commands.Cog):
|
||||||
|
"""General commands about python programming"""
|
||||||
|
|
||||||
|
@commands.command()
|
||||||
|
async def resources(self, ctx):
|
||||||
|
"""!resources - resources for python programming"""
|
||||||
|
await ctx.send("Some resources for you here!")
|
||||||
65
pyvis.py
65
pyvis.py
@@ -1,16 +1,16 @@
|
|||||||
import discord
|
import discord
|
||||||
import requests
|
|
||||||
import json
|
|
||||||
import os
|
import os
|
||||||
|
from cogs.finance import Finance
|
||||||
|
from cogs.python import Python
|
||||||
from discord.ext import commands
|
from discord.ext import commands
|
||||||
from dotenv import load_dotenv
|
from dotenv import load_dotenv
|
||||||
|
from pretty_help import DefaultMenu, PrettyHelp
|
||||||
|
|
||||||
# load environment variables from .env file
|
# load environment variables from .env file
|
||||||
load_dotenv()
|
load_dotenv()
|
||||||
|
|
||||||
DISCORD_BOT_TOKEN = os.getenv('DISCORD_BOT_TOKEN')
|
DISCORD_BOT_TOKEN = os.getenv('DISCORD_BOT_TOKEN')
|
||||||
DISCORD_GUILD_NAME = os.getenv('DISCORD_GUILD_NAME')
|
DISCORD_GUILD_NAME = os.getenv('DISCORD_GUILD_NAME')
|
||||||
LUNAR_CRUSH_API_KEY = os.getenv('LUNAR_CRUSH_API_KEY')
|
|
||||||
ALPHAVANTAGE_API_KEY = os.getenv('ALPHAVANTAGE_API_KEY')
|
|
||||||
|
|
||||||
bot = commands.Bot(command_prefix='!')
|
bot = commands.Bot(command_prefix='!')
|
||||||
|
|
||||||
@@ -23,54 +23,17 @@ async def on_ready():
|
|||||||
f'{guild.name}(id: {guild.id})'
|
f'{guild.name}(id: {guild.id})'
|
||||||
)
|
)
|
||||||
|
|
||||||
@bot.command(name="resources")
|
@bot.event
|
||||||
async def resources(ctx):
|
async def on_member_join(member):
|
||||||
await ctx.send("Some resources for you here!")
|
pass # TODO
|
||||||
|
|
||||||
@bot.command(name="stock")
|
# override default help command
|
||||||
async def stock(ctx, *tickers):
|
menu = DefaultMenu('◀️', '▶️', '❌')
|
||||||
if len(tickers) == 0:
|
bot.help_command = PrettyHelp(navigation=menu, color=discord.Colour.green())
|
||||||
tickers = ["AAPL", "GOOG", "MSFT", "AMZN"]
|
|
||||||
|
|
||||||
# msg response setup
|
# commands by category a.k.a. cogs
|
||||||
msg_response = "Stock prices for today!\n"
|
bot.add_cog(Python())
|
||||||
stock_found = False
|
bot.add_cog(Finance())
|
||||||
|
|
||||||
# make the call for stock prices
|
|
||||||
for ticker in tickers:
|
|
||||||
http_response = requests.get(f'https://www.alphavantage.co/query?function=GLOBAL_QUOTE&symbol={ticker}&apikey={ALPHAVANTAGE_API_KEY}')
|
|
||||||
stock_data = json.loads(http_response.text)
|
|
||||||
|
|
||||||
# check if stock exists
|
|
||||||
if 'Global Quote' not in stock_data:
|
|
||||||
continue
|
|
||||||
|
|
||||||
# get the stock price
|
|
||||||
stock_price = stock_data['Global Quote']['05. price']
|
|
||||||
msg_response += f'**{ticker}**: ${round(float(stock_price), 2)}\n'
|
|
||||||
stock_found = True
|
|
||||||
|
|
||||||
|
|
||||||
if not stock_found:
|
|
||||||
await ctx.send("No stock data found for given tickers!")
|
|
||||||
return
|
|
||||||
|
|
||||||
await ctx.send(msg_response)
|
|
||||||
|
|
||||||
@bot.command(name="crypto")
|
|
||||||
async def crypto(ctx, *tickers):
|
|
||||||
# default tickers
|
|
||||||
if len(tickers) == 0:
|
|
||||||
tickers = ["BTC", "ETH", "LTC"]
|
|
||||||
|
|
||||||
# make the call for prices
|
|
||||||
http_response = requests.get(f"https://api.lunarcrush.com/v2?data=assets&key={LUNAR_CRUSH_API_KEY}&symbol={','.join(tickers)}")
|
|
||||||
data = json.loads(http_response.text)
|
|
||||||
|
|
||||||
# extract prices and derive message response
|
|
||||||
msg_response = "Cryptocurrency prices for today!\n"
|
|
||||||
msg_response += "\n".join([f"**{item['symbol']}**: ${round(float(item['price']), 2)}" for item in data["data"]])
|
|
||||||
|
|
||||||
await ctx.send(msg_response)
|
|
||||||
|
|
||||||
|
# start up bot
|
||||||
bot.run(DISCORD_BOT_TOKEN)
|
bot.run(DISCORD_BOT_TOKEN)
|
||||||
@@ -4,6 +4,7 @@ attrs==21.2.0
|
|||||||
certifi==2021.5.30
|
certifi==2021.5.30
|
||||||
chardet==4.0.0
|
chardet==4.0.0
|
||||||
charset-normalizer==2.0.3
|
charset-normalizer==2.0.3
|
||||||
|
discord-pretty-help==1.3.2
|
||||||
discord.py==1.7.3
|
discord.py==1.7.3
|
||||||
idna==3.2
|
idna==3.2
|
||||||
multidict==5.1.0
|
multidict==5.1.0
|
||||||
|
|||||||
Reference in New Issue
Block a user