From 1ddf76707456e92c3693a7a577b167bd9d266563 Mon Sep 17 00:00:00 2001 From: nya~ <40436815+neko404notfound@users.noreply.github.com> Date: Thu, 21 Jun 2018 19:17:05 +0100 Subject: [PATCH] Logs and reports broken cogs on restart. --- sebimachine/__main__.py | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/sebimachine/__main__.py b/sebimachine/__main__.py index 4300b3b..1ff1195 100644 --- a/sebimachine/__main__.py +++ b/sebimachine/__main__.py @@ -61,6 +61,7 @@ class SebiMachine(commands.Bot, LoadConfig, Loggable): with open(in_here("config", "PrivateConfig.json")) as fp: self.bot_secrets = json.load(fp) self.db_con = database.DatabaseConnection(**self.bot_secrets["db-con"]) + self.failed_cogs_on_startup = {} self.book_emojis: Dict[str, str] = { "unlock": "🔓", "start": "⏮", @@ -78,17 +79,27 @@ class SebiMachine(commands.Bot, LoadConfig, Loggable): for cog in cogs: # Could this just be replaced with `strip()`? - cog = cog.replace("\n", "") - self.load_extension(f"src.cogs.{cog}") - self.logger.info(f"Loaded: {cog}") - - async def on_ready(self): + try: + cog = cog.replace("\n", "") + 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): """On ready function""" self.maintenance and self.logger.warning("MAINTENANCE ACTIVE") with open(f"src/config/reboot", "r") as f: reboot = f.readlines() if int(reboot[0]) == 1: 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: f.write(f"0")