diff --git a/api/nasa.py b/api/nasa.py new file mode 100644 index 0000000..df508e8 --- /dev/null +++ b/api/nasa.py @@ -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 \ No newline at end of file diff --git a/bot.py b/bot.py index b9117b2..1939712 100644 --- a/bot.py +++ b/bot.py @@ -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) \ No newline at end of file diff --git a/cogs/api/inspirobot.py b/cogs/api/inspirobot.py index 50a0b27..06c1984 100644 --- a/cogs/api/inspirobot.py +++ b/cogs/api/inspirobot.py @@ -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) diff --git a/cogs/api/nasa.py b/cogs/api/nasa.py new file mode 100644 index 0000000..3911b1c --- /dev/null +++ b/cogs/api/nasa.py @@ -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)) \ No newline at end of file diff --git a/cogs/fun/autoconvert.py b/cogs/fun/autoconvert.py index c10f276..7e517b5 100644 --- a/cogs/fun/autoconvert.py +++ b/cogs/fun/autoconvert.py @@ -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 diff --git a/util/config.py b/util/config.py new file mode 100644 index 0000000..f4db680 --- /dev/null +++ b/util/config.py @@ -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