Browse Source

Better exception reporting

master
noirscape 2 years ago
parent
commit
01214316d7
Signed by: noirscape GPG Key ID: 38216818B8894B9F
3 changed files with 19 additions and 2 deletions
  1. +2
    -0
      config.yml.example
  2. +13
    -2
      main.py
  3. +4
    -0
      required_cogs/testing.py

+ 2
- 0
config.yml.example View File

@ -17,3 +17,5 @@ custom_commands:
shortcut_prefix: nts!
topgg_api_key: ""
exception_url: ""

+ 13
- 2
main.py View File

@ -23,6 +23,7 @@
# requiring that modified versions of such material be marked in
# reasonable ways as different from the original version
import aiohttp
import yaml
from discord.ext import commands
import discord
@ -229,11 +230,16 @@ async def on_command_error(ctx, error):
await ctx.send("You do not have permission to use this command.")
return
# ignore all other exception types, but print them to stderr
# ignore all other exception types, but print them to stderr and notify using webhook.
print('Ignoring exception in command {}:'.format(ctx.command), file=sys.stderr)
traceback.print_exception(type(error), error, error.__traceback__, file=sys.stderr)
exception = traceback.format_exception(type(error), error, error.__traceback__)
print("\n".join(exception), file=sys.stderr)
async with aiohttp.ClientSession() as session:
webhook = discord.Webhook.from_url(config["exception_url"], adapter=discord.AsyncWebhookAdapter(session))
embed = discord.Embed(title=f"Exception {type(error)} occurred in command. Traceback below.", description="\n".join(exception))
await webhook.send(embed=embed)
@bot.event
async def on_error(event, *args, **kwargs):
@ -258,8 +264,13 @@ async def on_error(event, *args, **kwargs):
# This is purely for unhandled checks
print('Ignoring exception in {}'.format(event), file=sys.stderr)
traceback = traceback.format_exc()
traceback.print_exc()
async with aiohttp.ClientSession() as session:
webhook = discord.Webhook.from_url(config["exception_url"], adapter=discord.AsyncWebhookAdapter(session))
embed = discord.Embed(title=f"Exception {exception_type} occurred in command. Traceback below.", description="\n".join(traceback))
await webhook.send(embed=embed)
@bot.event
async def on_ready():


+ 4
- 0
required_cogs/testing.py View File

@ -39,6 +39,10 @@ class Testing(commands.Cog):
description="[Invite the real bot to your guild!](https://discordapp.com/oauth2/authorize?&client_id=518576005108203525&scope=bot&permissions=8)")
await ctx.send(embed=embed)
@commands.command()
async def raise_error(self, ctx):
"""Raise an exception."""
raise Exception("Some generic exception!")
def setup(bot):
bot.add_cog(Testing(bot))

Loading…
Cancel
Save