mirror of https://github.com/stijndcl/didier
Make tasks log exceptions
parent
185aaadce1
commit
0a9f73af8c
|
@ -1,6 +1,5 @@
|
||||||
import datetime
|
import datetime
|
||||||
import random
|
import random
|
||||||
import traceback
|
|
||||||
|
|
||||||
import discord
|
import discord
|
||||||
from discord.ext import commands, tasks # type: ignore # Strange & incorrect Mypy error
|
from discord.ext import commands, tasks # type: ignore # Strange & incorrect Mypy error
|
||||||
|
@ -212,8 +211,7 @@ class Tasks(commands.Cog):
|
||||||
|
|
||||||
await member.send(embed=personal_schedule.to_embed(day=today))
|
await member.send(embed=personal_schedule.to_embed(day=today))
|
||||||
|
|
||||||
# @tasks.loop(time=SOCIALLY_ACCEPTABLE_TIME)
|
@tasks.loop(time=SOCIALLY_ACCEPTABLE_TIME)
|
||||||
@tasks.loop(hours=3)
|
|
||||||
async def reminders(self, **kwargs):
|
async def reminders(self, **kwargs):
|
||||||
"""Send daily reminders to people"""
|
"""Send daily reminders to people"""
|
||||||
_ = kwargs
|
_ = kwargs
|
||||||
|
@ -258,8 +256,7 @@ class Tasks(commands.Cog):
|
||||||
@reset_wordle_word.error
|
@reset_wordle_word.error
|
||||||
async def _on_tasks_error(self, error: BaseException):
|
async def _on_tasks_error(self, error: BaseException):
|
||||||
"""Error handler for all tasks"""
|
"""Error handler for all tasks"""
|
||||||
print("".join(traceback.format_exception(type(error), error, error.__traceback__)))
|
self.client.dispatch("task_error", error)
|
||||||
self.client.dispatch("task_error")
|
|
||||||
|
|
||||||
|
|
||||||
async def setup(client: Didier):
|
async def setup(client: Didier):
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
import traceback
|
import traceback
|
||||||
|
from typing import Optional
|
||||||
|
|
||||||
import discord
|
import discord
|
||||||
from discord.ext import commands
|
from discord.ext import commands
|
||||||
|
@ -18,7 +19,7 @@ def _get_traceback(exception: Exception) -> str:
|
||||||
if line.strip().startswith("The above exception was the direct cause of"):
|
if line.strip().startswith("The above exception was the direct cause of"):
|
||||||
break
|
break
|
||||||
|
|
||||||
# Escape Discord markdown formatting
|
# Escape Discord Markdown formatting
|
||||||
error_string += line.replace(r"*", r"\*").replace(r"_", r"\_")
|
error_string += line.replace(r"*", r"\*").replace(r"_", r"\_")
|
||||||
if line.strip():
|
if line.strip():
|
||||||
error_string += "\n"
|
error_string += "\n"
|
||||||
|
@ -26,23 +27,30 @@ def _get_traceback(exception: Exception) -> str:
|
||||||
return abbreviate(error_string, Limits.EMBED_FIELD_VALUE_LENGTH - 8)
|
return abbreviate(error_string, Limits.EMBED_FIELD_VALUE_LENGTH - 8)
|
||||||
|
|
||||||
|
|
||||||
def create_error_embed(ctx: commands.Context, exception: Exception) -> discord.Embed:
|
def create_error_embed(ctx: Optional[commands.Context], exception: Exception) -> discord.Embed:
|
||||||
"""Create an embed for the traceback of an exception"""
|
"""Create an embed for the traceback of an exception"""
|
||||||
|
message = str(exception)
|
||||||
|
|
||||||
# Wrap the traceback in a codeblock for readability
|
# Wrap the traceback in a codeblock for readability
|
||||||
description = _get_traceback(exception).strip()
|
description = _get_traceback(exception).strip()
|
||||||
description = f"```\n{description}\n```"
|
description = f"```\n{description}\n```"
|
||||||
|
|
||||||
if ctx.guild is None:
|
|
||||||
origin = "DM"
|
|
||||||
else:
|
|
||||||
origin = f"{ctx.channel.mention} ({ctx.guild.name})"
|
|
||||||
|
|
||||||
invocation = f"{ctx.author.display_name} in {origin}"
|
|
||||||
|
|
||||||
embed = discord.Embed(title="Error", colour=discord.Colour.red())
|
embed = discord.Embed(title="Error", colour=discord.Colour.red())
|
||||||
embed.add_field(name="Command", value=f"{ctx.message.content}", inline=True)
|
|
||||||
embed.add_field(name="Context", value=invocation, inline=True)
|
if ctx is not None:
|
||||||
embed.add_field(name="Exception", value=str(exception), inline=False)
|
if ctx.guild is None:
|
||||||
|
origin = "DM"
|
||||||
|
else:
|
||||||
|
origin = f"{ctx.channel.mention} ({ctx.guild.name})"
|
||||||
|
|
||||||
|
invocation = f"{ctx.author.display_name} in {origin}"
|
||||||
|
|
||||||
|
embed.add_field(name="Command", value=f"{ctx.message.content}", inline=True)
|
||||||
|
embed.add_field(name="Context", value=invocation, inline=True)
|
||||||
|
|
||||||
|
if message:
|
||||||
|
embed.add_field(name="Exception", value=message, inline=False)
|
||||||
|
|
||||||
embed.add_field(name="Traceback", value=description, inline=False)
|
embed.add_field(name="Traceback", value=description, inline=False)
|
||||||
|
|
||||||
return embed
|
return embed
|
||||||
|
|
|
@ -2,6 +2,9 @@ import logging
|
||||||
|
|
||||||
import discord
|
import discord
|
||||||
|
|
||||||
|
from didier.utils.discord.constants import Limits
|
||||||
|
from didier.utils.types.string import abbreviate
|
||||||
|
|
||||||
__all__ = ["create_logging_embed"]
|
__all__ = ["create_logging_embed"]
|
||||||
|
|
||||||
|
|
||||||
|
@ -16,6 +19,6 @@ def create_logging_embed(level: int, message: str) -> discord.Embed:
|
||||||
|
|
||||||
colour = colours.get(level, discord.Colour.red())
|
colour = colours.get(level, discord.Colour.red())
|
||||||
embed = discord.Embed(colour=colour, title="Logging")
|
embed = discord.Embed(colour=colour, title="Logging")
|
||||||
embed.description = message
|
embed.description = abbreviate(message, Limits.EMBED_DESCRIPTION_LENGTH)
|
||||||
|
|
||||||
return embed
|
return embed
|
||||||
|
|
|
@ -363,6 +363,13 @@ class Didier(commands.Bot):
|
||||||
"""Event triggered when the bot is ready"""
|
"""Event triggered when the bot is ready"""
|
||||||
print(settings.DISCORD_READY_MESSAGE)
|
print(settings.DISCORD_READY_MESSAGE)
|
||||||
|
|
||||||
|
async def on_task_error(self, exception: Exception):
|
||||||
|
"""Event triggered when a task raises an exception"""
|
||||||
|
if settings.ERRORS_CHANNEL is not None:
|
||||||
|
embed = create_error_embed(None, exception)
|
||||||
|
channel = self.get_channel(settings.ERRORS_CHANNEL)
|
||||||
|
await channel.send(embed=embed)
|
||||||
|
|
||||||
async def on_thread_create(self, thread: discord.Thread):
|
async def on_thread_create(self, thread: discord.Thread):
|
||||||
"""Event triggered when a new thread is created"""
|
"""Event triggered when a new thread is created"""
|
||||||
# Join threads automatically
|
# Join threads automatically
|
||||||
|
|
Loading…
Reference in New Issue