From 1573d0b04655afc84fd061885094374ddb624e01 Mon Sep 17 00:00:00 2001 From: Infinidoge Date: Tue, 31 Dec 2024 18:09:46 -0500 Subject: [PATCH] improve error logging --- nomen/main.py | 46 +++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 43 insertions(+), 3 deletions(-) diff --git a/nomen/main.py b/nomen/main.py index 214f69d..cd480bc 100644 --- a/nomen/main.py +++ b/nomen/main.py @@ -1,6 +1,7 @@ import io import logging import os +import pprint import sys import textwrap import traceback @@ -120,18 +121,57 @@ async def on_ready(): log.info(bot.user.id) log.info("-------------") + bot.app_info = await bot.application_info() + bot.owner = bot.app_info.owner + @bot.event async def on_command_error(ctx, error): if isinstance(error, commands.CommandNotFound): return - if isinstance(error, commands.NoPrivateMessage): + if isinstance( + error, + ( + commands.UserInputError, + commands.CheckFailure, + ), + ): await ctx.send(error) return - log.exception(error) - await ctx.send(error) + log.error(error, exc_info=error) + timestamp = int(ctx.message.created_at.timestamp()) + await ctx.bot.owner.send( + f"# Command Error Occurred: {error}\n\n" + f"```\n{"".join(traceback.format_exception(error))}```\n\n" + f"## Context\n" + f"**From:** {ctx.author.mention} ({ctx.author.name})\n" + f", | [Jump to Command]({ctx.message.jump_url}) | <#{ctx.channel.id}>\n" + ) + await ctx.send( + f"Error: {error}\n\n**Note:** Infinidoge (Developer) has already been sent a notification that an error occurred and will come investigate when available." + ) + + +@bot.event +async def on_error(event, *args, **kwargs): + ptraceback = "".join(traceback.format_exc()) + pargs = pprint.pformat(args) + pkwargs = pprint.pformat(kwargs) + try: + await bot.owner.send( + f"# Error Occurred: Event `{event}`" + f"```\n{ptraceback}```" + f"## args" + f"```\n{pargs}\n```" + f"## kwargs" + f"```\n{pkwargs}\n```" + ) + except: # Don't want recursive errors + pass + + log.error(f"Error in event {event}.\nargs={pargs}\nkwargs={pkwargs}", exc_info=sys.exc_info()) @bot.command(hidden=True)