paginate error handling messages

This commit is contained in:
Infinidoge 2025-01-01 00:49:51 -05:00
parent 4fa6b25971
commit 3d90376535
Signed by: Infinidoge
SSH key fingerprint: SHA256:GT2StvPQMMfFHyiiFJymQxfTG/z6EWLJ6NWItf5K5sA

View file

@ -9,7 +9,7 @@ from contextlib import redirect_stdout
from disnake import Guild, Intents, Message from disnake import Guild, Intents, Message
from disnake.ext import commands from disnake.ext import commands
from disnake.ext.commands import Bot from disnake.ext.commands import Bot, Paginator
from dotenv import find_dotenv, load_dotenv from dotenv import find_dotenv, load_dotenv
from .db import run_db_migrations, setup_db from .db import run_db_migrations, setup_db
@ -142,13 +142,19 @@ async def on_command_error(ctx, error):
log.error(error, exc_info=error) log.error(error, exc_info=error)
timestamp = int(ctx.message.created_at.timestamp()) timestamp = int(ctx.message.created_at.timestamp())
paginator = Paginator()
ptraceback = "".join(traceback.format_exception(error))
for line in ptraceback.split("\n"):
paginator.add_line(line)
await ctx.bot.owner.send( await ctx.bot.owner.send(
f"# Command Error Occurred: {error}\n\n" f"# Command Error Occurred: {error}\n"
f"```\n{"".join(traceback.format_exception(error))}```\n\n"
f"## Context\n" f"## Context\n"
f"**From:** {ctx.author.mention} ({ctx.author.name})\n" f"**From:** {ctx.author.mention} ({ctx.author.name})\n"
f"<t:{timestamp}>, <t:{timestamp}:R> | [Jump to Command]({ctx.message.jump_url}) | <#{ctx.channel.id}>\n" f"<t:{timestamp}>, <t:{timestamp}:R> | [Jump to Command]({ctx.message.jump_url}) | <#{ctx.channel.id}>\n"
f"## Traceback"
) )
for page in paginator.pages:
await ctx.bot.owner.send(page)
await ctx.send( 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." f"Error: {error}\n\n**Note:** Infinidoge (Developer) has already been sent a notification that an error occurred and will come investigate when available."
) )
@ -156,18 +162,23 @@ async def on_command_error(ctx, error):
@bot.event @bot.event
async def on_error(event, *args, **kwargs): async def on_error(event, *args, **kwargs):
ptraceback = "".join(traceback.format_exc())
pargs = pprint.pformat(args) pargs = pprint.pformat(args)
pkwargs = pprint.pformat(kwargs) pkwargs = pprint.pformat(kwargs)
try: try:
paginator = Paginator()
for line in traceback.format_exc().split("\n"):
paginator.add_line(line)
await bot.owner.send( await bot.owner.send(
f"# Error Occurred: Event `{event}`" f"# Error Occurred: Event `{event[:100]}`\n"
f"```\n{ptraceback}```" f"## args\n"
f"## args" f"```\n{pargs}\n```\n"
f"```\n{pargs}\n```" f"## kwargs\n"
f"## kwargs" f"```\n{pkwargs}\n```\n"
f"```\n{pkwargs}\n```" f"## Traceback"
) )
for page in paginator.pages:
await bot.owner.send(page)
except: # Don't want recursive errors except: # Don't want recursive errors
pass pass
@ -226,7 +237,11 @@ async def admin_eval(ctx, *, body: str):
ret = await func() ret = await func()
except Exception: except Exception:
value = stdout.getvalue() value = stdout.getvalue()
await ctx.send("```py\n{}{}\n```".format(value, traceback.format_exc())) ret = traceback.format_exc()
try:
await ctx.message.add_reaction("\u2705")
except:
pass
else: else:
value = stdout.getvalue() value = stdout.getvalue()
try: try:
@ -234,11 +249,18 @@ async def admin_eval(ctx, *, body: str):
except: except:
pass pass
if ret is None: paginator = Paginator(prefix="```py", suffix="```")
if value: if value:
await ctx.send("```py\n{}\n```".format(value)) for line in str(value).split("\n"):
else: paginator.add_line(line)
await ctx.send("```py\n{}{}\n```".format(value, ret)) paginator.close_page()
if ret:
for line in str(ret).split("\n"):
paginator.add_line(line)
paginator.close_page()
for page in paginator.pages:
await ctx.send(page)
@bot.command() @bot.command()