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:
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,9 +79,13 @@ class SebiMachine(commands.Bot, LoadConfig, Loggable):
for cog in cogs:
# Could this just be replaced with `strip()`?
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"""
@ -89,6 +94,12 @@ class SebiMachine(commands.Bot, LoadConfig, Loggable):
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")