From 5ebbb9fba2983b087f3737ce728a6db7f8f1e20a Mon Sep 17 00:00:00 2001 From: Mega-JC <65417594+Mega-JC@users.noreply.github.com> Date: Sat, 27 Jul 2024 13:39:59 +0200 Subject: [PATCH] Improve bot message creation error handling in 'showcase' extension --- pcbot/exts/showcase/cogs.py | 50 ++++++++++++++++++++++++------------- 1 file changed, 33 insertions(+), 17 deletions(-) diff --git a/pcbot/exts/showcase/cogs.py b/pcbot/exts/showcase/cogs.py index 42fa93c..4283b61 100644 --- a/pcbot/exts/showcase/cogs.py +++ b/pcbot/exts/showcase/cogs.py @@ -403,12 +403,18 @@ async def on_thread_create(self, thread: discord.Thread): deletion_datetime = datetime.datetime.now( datetime.timezone.utc ) + datetime.timedelta(minutes=5) - warn_msg = await message.reply( - "### Invalid showcase message\n\n" - f"{reason}\n\n" - " If no changes are made, your message (and its thread/post) will be " - f"deleted {snakecore.utils.create_markdown_timestamp(deletion_datetime, 'R')}." - ) + + try: + warn_msg = await message.reply( + "### Invalid showcase message\n\n" + f"{reason}\n\n" + " If no changes are made, your message (and its thread/post) will be " + f"deleted {snakecore.utils.create_markdown_timestamp(deletion_datetime, 'R')}." + ) + except discord.HTTPException as e: + if e.status == 400: + return + self.entry_message_deletion_dict[message.id] = ( asyncio.create_task( self.delete_bad_message_with_thread(message, delay=300) @@ -432,11 +438,15 @@ async def prompt_author_for_feedback_thread(self, message: discord.Message): datetime.timezone.utc ) + datetime.timedelta(minutes=1) - alert_msg = await message.reply( - content=f"Need a feedback thread?\n\n-# This message will be deleted " - + snakecore.utils.create_markdown_timestamp(deletion_datetime, "R") - + ".", - ) + try: + alert_msg = await message.reply( + content=f"Need a feedback thread?\n\n-# This message will be deleted " + + snakecore.utils.create_markdown_timestamp(deletion_datetime, "R") + + ".", + ) + except discord.HTTPException as e: + if e.status == 400: + return await alert_msg.add_reaction("✅") await alert_msg.add_reaction("❌") @@ -522,12 +532,18 @@ async def on_message(self, message: discord.Message): deletion_datetime = datetime.datetime.now( datetime.timezone.utc ) + datetime.timedelta(minutes=5) - warn_msg = await message.reply( - "### Invalid showcase message\n\n" - f"{reason}\n\n" - " If no changes are made, your message (and its thread/post) will be " - f"deleted {snakecore.utils.create_markdown_timestamp(deletion_datetime, 'R')}." - ) + + try: + warn_msg = await message.reply( + "### Invalid showcase message\n\n" + f"{reason}\n\n" + " If no changes are made, your message (and its thread/post) will be " + f"deleted {snakecore.utils.create_markdown_timestamp(deletion_datetime, 'R')}." + ) + except discord.HTTPException as e: + if e.status == 400: + return + self.entry_message_deletion_dict[message.id] = ( asyncio.create_task( self.delete_bad_message_with_thread(message, delay=300)