From 2dedfd6e27059b3972dbc8734b372fc4a7edfcfd Mon Sep 17 00:00:00 2001 From: Sami Laine Date: Tue, 2 Apr 2024 14:27:38 +0000 Subject: [PATCH] Use a placeholder essage if an error doesn't have one It seems that sometimes the Context passed to an on_command_error does not have the `message` attribute, despite the documentation. Falling back to a placeholder message if that is the case --- CHANGELOG.md | 6 ++++++ src/operationbot/commandListener.py | 10 ++++++++-- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d84b73c..e3e282a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,12 @@ The project uses semantic versioning (see [SemVer](https://semver.org)). ## [Unreleased] +### Fixed + +- Use a placeholder message if a command error doesn't include one. This + should fix some of the incorrect warnings about messages with >2000 + characters when trying to edit old events. + ## v0.45.0 - 2024-03-27 ### Added diff --git a/src/operationbot/commandListener.py b/src/operationbot/commandListener.py index fc35fcd..438f68b 100644 --- a/src/operationbot/commandListener.py +++ b/src/operationbot/commandListener.py @@ -1174,7 +1174,13 @@ async def on_command_error(ctx: Context, error: Exception): traceback.format_exception(type(error), error, error.__traceback__, 2) ) - lines = ctx.message.clean_content.split("\n") + if hasattr(ctx, "message"): + lines = ctx.message.clean_content.split("\n") + clean_content = ctx.message.clean_content + else: + lines = "No associated message" + clean_content = lines + logging.error(f"{lines=}") if len(lines) > 1: # Show only first line of the message @@ -1190,7 +1196,7 @@ async def on_command_error(ctx: Context, error: Exception): "Received error message that's over 2000 characters, check " "the log for the full error." ) - logging.error("Message:", ctx.message.clean_content) + logging.error("Message:", clean_content) msg = f"{msg[:1990]} [...]```" await ctx.send(msg)