Logs and reports broken cogs on restart.

This commit is contained in:
nya~ 2018-06-21 19:17:05 +01:00 committed by GitHub
parent f4b0f03a23
commit 1ddf767074
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -61,6 +61,7 @@ class SebiMachine(commands.Bot, LoadConfig, Loggable):
with open(in_here("config", "PrivateConfig.json")) as fp: with open(in_here("config", "PrivateConfig.json")) as fp:
self.bot_secrets = json.load(fp) self.bot_secrets = json.load(fp)
self.db_con = database.DatabaseConnection(**self.bot_secrets["db-con"]) self.db_con = database.DatabaseConnection(**self.bot_secrets["db-con"])
self.failed_cogs_on_startup = {}
self.book_emojis: Dict[str, str] = { self.book_emojis: Dict[str, str] = {
"unlock": "🔓", "unlock": "🔓",
"start": "", "start": "",
@ -78,17 +79,27 @@ class SebiMachine(commands.Bot, LoadConfig, Loggable):
for cog in cogs: for cog in cogs:
# Could this just be replaced with `strip()`? # Could this just be replaced with `strip()`?
cog = cog.replace("\n", "") try:
self.load_extension(f"src.cogs.{cog}") cog = cog.replace("\n", "")
self.logger.info(f"Loaded: {cog}") self.load_extension(f"src.cogs.{cog}")
self.logger.info(f"Loaded: {cog}")
except (ModuleNotFoundError, ImportError) as ex:
logging.exception(f'Could not load {cog}', exc_info=(type(ex), ex, ex.__traceback__))
self.failed_cogs_on_startup[cog] = ex
async def on_ready(self): async def on_ready(self):
"""On ready function""" """On ready function"""
self.maintenance and self.logger.warning("MAINTENANCE ACTIVE") self.maintenance and self.logger.warning("MAINTENANCE ACTIVE")
with open(f"src/config/reboot", "r") as f: with open(f"src/config/reboot", "r") as f:
reboot = f.readlines() reboot = f.readlines()
if int(reboot[0]) == 1: if int(reboot[0]) == 1:
await self.get_channel(int(reboot[1])).send("Restart Finished.") await self.get_channel(int(reboot[1])).send("Restart Finished.")
for cog, ex in self.failed_cogs_on_startup.items():
tb = ''.join(traceback.format_exception(type(ex), ex, ex.__traceback__))[-1500:]
await ctx.send(
f'FAILED TO LOAD {cog} BECAUSE OF {type(ex).__name__}: {ex}\n'
f'{tb}'
)
with open(f"src/config/reboot", "w") as f: with open(f"src/config/reboot", "w") as f:
f.write(f"0") f.write(f"0")