diff --git a/.vscode/launch.json b/.vscode/launch.json index 7dd7d90..f20797a 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -9,7 +9,8 @@ "type": "python", "request": "launch", "program": "src/bot.py", - "console": "integratedTerminal" + "console": "integratedTerminal", + "justMyCode": false }, { "name": "pytest", diff --git a/requirements.txt b/requirements.txt index 99068a4..a667e18 100644 --- a/requirements.txt +++ b/requirements.txt @@ -33,4 +33,4 @@ toml==0.10.2 typing-extensions==3.10.0.2 wrapt==1.12.1 yarl==1.6.3 -youtube-dl==2021.6.6 +youtube-dl==2021.12.17 diff --git a/src/error.py b/src/error.py index 7bca386..17249e8 100644 --- a/src/error.py +++ b/src/error.py @@ -1,4 +1,5 @@ import logging +import re from discord.ext import commands @@ -27,8 +28,11 @@ class ErrorHandler(commands.Cog): else: message = "Oh no! Something went wrong while running the command!" + ansi_escape = re.compile(r'\x1B(?:[@-Z\\-_]|\[[0-?]*[ -/]*[@-~])') + message = ansi_escape.sub("", message) + self.logger.error('Error during command "%s": %s', command_name, message) - embed = ErrorMessage(message) + embed = ErrorMessage(message, command_name=command_name) await ctx.send(embed=embed) diff --git a/src/message.py b/src/message.py index 6998ef0..61908e7 100644 --- a/src/message.py +++ b/src/message.py @@ -1,14 +1,31 @@ +import re + import discord class BotMessage(discord.Embed): - pass + def __init__(self, **kwargs): + title = kwargs.pop("title", None)[:256] + if title is not None: + # Max embed title length is 256 + title = title[:256] + super().__init__( + **kwargs, + title=title + ) class ErrorMessage(BotMessage): - def __init__(self, message): + def __init__(self, message, *, command_name: str = None): + title = message + if command_name: + title = f'Error during command "{command_name}"' + description = None + if match := re.search(r"(?P\w+Error): ?(ERROR: ?)?(?P.*): ?Traceback", message): + description = f"{match.group('error')}: {match.group('message')}" super().__init__( - title=f"Error: {message}", + title=title, + description=description, color=discord.Color.red() )