Added NASA APOD. Added Config Module

This commit is contained in:
Robert 2020-01-08 02:08:07 +01:00
parent 06cd25eb96
commit d2622e25e8
6 changed files with 83 additions and 15 deletions

15
api/nasa.py Normal file
View file

@ -0,0 +1,15 @@
import requests
from util import config, logging
api_key = config.settings["api_keys"]["nasa"]
def image_of_the_day():
url = f"https://api.nasa.gov/planetary/apod?api_key={api_key}"
response = requests.get(url)
if not response.ok:
logging.error(f"Failed to fetch Image of the day (NASA API): {response.status_code}")
return
image_link = response.json()["hdurl"]
return image_link

36
bot.py
View file

@ -2,15 +2,25 @@ import discord
import os
import json
from discord.ext import commands
from util import logging
from util import logging, config
# Bot URL https://discordapp.com/api/oauth2/authorize?client_id=657709911337074698&permissions=314432&scope=bot
config = open("config.json", "r")
json = json.load(config)
config.close()
total = 0
failed = 0
client = commands.Bot(command_prefix=json["prefix"], case_insensitive=True)
logging.info("Starting up...\n")
logging.info("--- Loading configs ---\n")
if not config.load("config.json"):
failed += 1
total += 1
logging.info("Finished loading configs.")
logging.info(f"Total {total}, Failed {failed}\n")
client = commands.Bot(command_prefix=config.settings["prefix"], case_insensitive=True)
@client.command()
@ -43,8 +53,8 @@ async def reload(ctx, extension):
@client.event
async def on_ready():
logging.info("Starting up...\n")
logging.info("Trying to load cogs...\n")
logging.info("--- Loading cogs ---\n")
total = 0
failed = 0
# Load all cogs on startup
@ -57,8 +67,9 @@ async def on_ready():
try:
client.load_extension(cog)
logging.info(f"Trying {cog}.....Success!")
except:
logging.error(f"Trying {cog}.....Failed!")
except Exception as e:
logging.info(f"Trying {cog}.....Failed!")
logging.error(str(e))
failed += 1
@ -71,8 +82,9 @@ async def on_ready():
try:
client.load_extension(cog)
logging.info(f"Trying {cog}.....Success!")
except:
logging.error(f"Trying {cog}.....Failed!")
except Exception as e:
logging.info(f"Trying {cog}.....Failed!")
logging.error(str(e))
failed += 1
logging.info("Finished loading cogs.")
@ -82,5 +94,5 @@ async def on_ready():
key = json["client_key"]
key = config.settings["client_key"]
client.run(key)

View file

@ -2,6 +2,7 @@ import discord
from discord.ext import commands
from api import inspirobot
from util import config
class Inspirobot(commands.Cog):
@ -15,7 +16,7 @@ class Inspirobot(commands.Cog):
if image is None:
await ctx.message.add_reaction("⚠️")
else:
embed = discord.Embed(title="InspiroBot", color=0x111387)
embed = discord.Embed(title="InspiroBot", color=int(config.settings["color"], 16))
embed.set_image(url=image)
await ctx.send(embed=embed)

22
cogs/api/nasa.py Normal file
View file

@ -0,0 +1,22 @@
import discord
from discord.ext import commands
from api import nasa
from util import config
class Nasa(commands.Cog):
def __init__(self, client):
self.client = client
@commands.command(name="APOD", description="Posts NASA's picture of the day.")
@commands.cooldown(1, 30)
async def apod(self, ctx):
url = nasa.image_of_the_day()
embed = discord.Embed(title="Astronomy Picture of the day", color=int(config.settings["color"], 16))
embed.set_image(url=url)
await ctx.send(embed=embed)
def setup(client):
client.add_cog(Nasa(client))

View file

@ -1,6 +1,6 @@
import discord
from discord.ext import commands
from util import logging, checking
from util import logging, checking, config
import re
class AutoConvert(commands.Cog):
@ -51,7 +51,7 @@ class AutoConvert(commands.Cog):
empty = True
currencies = self.contains_currency(message)
embed = discord.Embed(title="Here are some useful conversions:", color=0x111387)
embed = discord.Embed(title="Here are some useful conversions:", color=int(config.settings["color"], 16))
if len(currencies) != 0:
empty = False

18
util/config.py Normal file
View file

@ -0,0 +1,18 @@
import json
from util import logging
filepath = ""
settings = {}
def load(file):
global settings
filepath = file
try:
with open(file, "r") as sett_file:
settings = json.load(sett_file)
logging.info(f"Trying {filepath}.....Success!")
return True
except Exception as e:
logging.info(f"Trying {filepath}.....Failed!")
logging.error(str(e))
return False