From 573cb4adde6e62b37fcaed65efa772c7e9be458e Mon Sep 17 00:00:00 2001 From: dkay0670 Date: Sat, 17 Feb 2024 17:54:13 -0800 Subject: [PATCH 1/4] Add more friendly error when corrected message is too long --- techsupport_bot/commands/correct.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/techsupport_bot/commands/correct.py b/techsupport_bot/commands/correct.py index c3b733dc6..d99f0b043 100644 --- a/techsupport_bot/commands/correct.py +++ b/techsupport_bot/commands/correct.py @@ -55,6 +55,13 @@ async def correct_command( updated_message = self.prepare_message( message_to_correct.content, to_replace, replacement ) + + if len(updated_message) > 4096: + await auxiliary.send_deny_embed( + message="The corrected message is too long to send", channel=ctx.channel + ) + return + embed = auxiliary.generate_basic_embed( title="Correction!", description=f"{updated_message} :white_check_mark:", From c8478198d2ef64bb48a57366e08b7c968d907df6 Mon Sep 17 00:00:00 2001 From: dkay0670 Date: Sat, 17 Feb 2024 18:16:42 -0800 Subject: [PATCH 2/4] Add maximum line count --- techsupport_bot/commands/correct.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/techsupport_bot/commands/correct.py b/techsupport_bot/commands/correct.py index d99f0b043..5845cee5a 100644 --- a/techsupport_bot/commands/correct.py +++ b/techsupport_bot/commands/correct.py @@ -62,6 +62,13 @@ async def correct_command( ) return + if updated_message.count("\n") > 15: + await auxiliary.send_deny_embed( + message="The corrected message has too many lines to send", + channel=ctx.channel, + ) + return + embed = auxiliary.generate_basic_embed( title="Correction!", description=f"{updated_message} :white_check_mark:", From 0da6ec88f6d584779a133177bb7011b89e100dfe Mon Sep 17 00:00:00 2001 From: dkay Date: Fri, 2 May 2025 11:28:54 -0700 Subject: [PATCH 3/4] Remove unit tests --- .../commands_tests/test_extensions_correct.py | 190 ------------------ 1 file changed, 190 deletions(-) delete mode 100644 techsupport_bot/tests/commands_tests/test_extensions_correct.py diff --git a/techsupport_bot/tests/commands_tests/test_extensions_correct.py b/techsupport_bot/tests/commands_tests/test_extensions_correct.py deleted file mode 100644 index d153e7694..000000000 --- a/techsupport_bot/tests/commands_tests/test_extensions_correct.py +++ /dev/null @@ -1,190 +0,0 @@ -""" -This is a file to test the extensions/correct.py file -This contains 9 tests -""" - -from __future__ import annotations - -import importlib -from typing import Self -from unittest.mock import AsyncMock - -import pytest -from core import auxiliary -from tests import config_for_tests - - -class Test_PrepareMessage: - """A set of tests to test the prepare_message function""" - - def test_prepare_message_success(self: Self) -> None: - """Test to ensure that replacement when the entire message needs to be replaced works""" - # Step 1 - Setup env - discord_env = config_for_tests.FakeDiscordEnv() - - # Step 2 - Call the function - new_content = discord_env.correct.prepare_message( - discord_env.message_person2_noprefix_1.content, "message", "bbbb" - ) - - # Step 3 - Assert that everything works - assert new_content == "**bbbb**" - - def test_prepare_message_multi(self: Self) -> None: - """Test to ensure that replacement works if multiple parts need to be replaced""" - # Step 1 - Setup env - discord_env = config_for_tests.FakeDiscordEnv() - - # Step 2 - Call the function - new_content = discord_env.correct.prepare_message( - discord_env.message_person2_noprefix_1.content, "e", "bbbb" - ) - - # Step 3 - Assert that everything works - assert new_content == "m**bbbb**ssag**bbbb**" - - def test_prepare_message_partial(self: Self) -> None: - """Test to ensure that replacement works if multiple - parts of the message need to be replaced""" - # Step 1 - Setup env - discord_env = config_for_tests.FakeDiscordEnv() - - # Step 2 - Call the function - new_content = discord_env.correct.prepare_message( - discord_env.message_person2_noprefix_1.content, "mes", "bbbb" - ) - - # Step 3 - Assert that everything works - assert new_content == "**bbbb**sage" - - def test_prepare_message_fail(self: Self) -> None: - """Test to ensure that replacement doesnt change anything if needed - This should never happen, but test it here anyway""" - # Step 1 - Setup env - discord_env = config_for_tests.FakeDiscordEnv() - - # Step 2 - Call the function - new_content = discord_env.correct.prepare_message( - discord_env.message_person2_noprefix_1.content, "asdf", "bbbb" - ) - - # Step 3 - Assert that everything works - assert new_content == "message" - - -class Test_HandleCorrect: - """Tests to test the handle_correct function""" - - @pytest.mark.asyncio - async def test_handle_calls_search_for_message(self: Self) -> None: - """This ensures that the search_channel_for_message function is called, - with the correct args""" - # Step 1 - Setup env - discord_env = config_for_tests.FakeDiscordEnv() - discord_env.context.message = discord_env.message_person2_noprefix_1 - discord_env.correct.prepare_message = AsyncMock() - auxiliary.search_channel_for_message = AsyncMock() - auxiliary.generate_basic_embed = AsyncMock() - discord_env.context.send = AsyncMock() - - # Step 2 - Call the function - await discord_env.correct.correct_command(discord_env.context, "a", "b") - - # Step 3 - Assert that everything works - auxiliary.search_channel_for_message.assert_called_once_with( - channel=discord_env.channel, - prefix=config_for_tests.PREFIX, - content_to_match="a", - allow_bot=False, - ) - - # Step 4 - Cleanup - importlib.reload(auxiliary) - - @pytest.mark.asyncio - async def test_handle_calls_prepare_message(self: Self) -> None: - """This ensures that the prepare_message function is called, with the correct args""" - # Step 1 - Setup env - discord_env = config_for_tests.FakeDiscordEnv() - discord_env.context.message = discord_env.message_person2_noprefix_1 - discord_env.correct.prepare_message = AsyncMock() - auxiliary.search_channel_for_message = AsyncMock( - return_value=discord_env.message_person2_noprefix_1 - ) - auxiliary.generate_basic_embed = AsyncMock() - discord_env.context.send = AsyncMock() - - # Step 2 - Call the function - await discord_env.correct.correct_command(discord_env.context, "a", "b") - - # Step 3 - Assert that everything works - discord_env.correct.prepare_message.assert_called_once_with( - discord_env.message_person2_noprefix_1.content, "a", "b" - ) - - # Step 4 - Cleanup - importlib.reload(auxiliary) - - @pytest.mark.asyncio - async def test_handle_calls_generate_embed(self: Self) -> None: - """This ensures that the generate_basic_embed function is called, with the correct args""" - # Step 1 - Setup env - discord_env = config_for_tests.FakeDiscordEnv() - discord_env.context.message = discord_env.message_person2_noprefix_1 - discord_env.correct.prepare_message = AsyncMock() - auxiliary.search_channel_for_message = AsyncMock( - return_value=discord_env.message_person2_noprefix_1 - ) - auxiliary.generate_basic_embed = AsyncMock() - discord_env.context.send = AsyncMock() - - # Step 2 - Call the function - await discord_env.correct.correct_command(discord_env.context, "a", "b") - - # Step 3 - Assert that everything works - auxiliary.generate_basic_embed.assert_called_once() - - # Step 4 - Cleanup - importlib.reload(auxiliary) - - @pytest.mark.asyncio - async def test_handle_calls_send(self: Self) -> None: - """This ensures that the ctx.send function is called, with the correct args""" - # Step 1 - Setup env - discord_env = config_for_tests.FakeDiscordEnv() - discord_env.context.message = discord_env.message_person2_noprefix_1 - discord_env.correct.prepare_message = AsyncMock() - auxiliary.search_channel_for_message = AsyncMock( - return_value=discord_env.message_person2_noprefix_1 - ) - auxiliary.generate_basic_embed = AsyncMock() - discord_env.context.send = AsyncMock() - - # Step 2 - Call the function - await discord_env.correct.correct_command(discord_env.context, "a", "b") - - # Step 3 - Assert that everything works - discord_env.context.send.assert_called_once() - - # Step 4 - Cleanup - importlib.reload(auxiliary) - - @pytest.mark.asyncio - async def test_handle_no_message_found(self: Self) -> None: - """This test ensures that a deny embed is sent if no message could be found""" - # Step 1 - Setup env - discord_env = config_for_tests.FakeDiscordEnv() - auxiliary.search_channel_for_message = AsyncMock(return_value=None) - auxiliary.send_deny_embed = AsyncMock() - - # Step 2 - Call the function - await discord_env.correct.correct_command(discord_env.context, "a", "b") - - # Step 3 - Assert that everything works - auxiliary.send_deny_embed.assert_called_once_with( - message="I couldn't find any message to correct", - channel=discord_env.channel, - ) - - # Step 4 - Cleanup - importlib.reload(auxiliary) From be5cb8e8bbeff048e8126d757e59bdabd386c8cd Mon Sep 17 00:00:00 2001 From: dkay Date: Fri, 2 May 2025 22:40:37 -0700 Subject: [PATCH 4/4] Update correct.py --- techsupport_bot/commands/correct.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/techsupport_bot/commands/correct.py b/techsupport_bot/commands/correct.py index 5845cee5a..fd3852936 100644 --- a/techsupport_bot/commands/correct.py +++ b/techsupport_bot/commands/correct.py @@ -56,6 +56,8 @@ async def correct_command( message_to_correct.content, to_replace, replacement ) + updated_message += " :white_check_mark:" + if len(updated_message) > 4096: await auxiliary.send_deny_embed( message="The corrected message is too long to send", channel=ctx.channel @@ -71,7 +73,7 @@ async def correct_command( embed = auxiliary.generate_basic_embed( title="Correction!", - description=f"{updated_message} :white_check_mark:", + description=updated_message, color=discord.Color.green(), ) await ctx.send(