From dba60ddccd109af88688013c89471e8c1c9f5606 Mon Sep 17 00:00:00 2001 From: ajax146 <31014239+ajax146@users.noreply.github.com> Date: Fri, 2 May 2025 11:33:20 -0400 Subject: [PATCH 1/3] Refactors hello, removes worthless unit tests --- README.md | 23 ++++++----- techsupport_bot/commands/hello.py | 35 +++++++---------- techsupport_bot/core/auxiliary.py | 6 ++- .../commands_tests/test_extensions_hello.py | 38 ------------------- 4 files changed, 29 insertions(+), 73 deletions(-) delete mode 100644 techsupport_bot/tests/commands_tests/test_extensions_hello.py diff --git a/README.md b/README.md index 5eab70ba3..f82604597 100644 --- a/README.md +++ b/README.md @@ -68,22 +68,21 @@ On startup, the bot will load all extension files in the `techsupport_bot/extens A (very) simple example: ```python -from core import auxiliary, cogs -from discord.ext import commands -async def setup(bot): +import discord +from core import cogs +from discord import app_commands + +async def setup(bot: bot.TechSupportBot) -> None: await bot.add_cog(Greeter(bot=bot)) + class Greeter(cogs.BaseCog): - async def hello_command(self, ctx) -> None: - await auxiliary.add_list_of_reactions( - message=ctx.message, reactions=["🇭", "🇪", "🇾"] - ) - @commands.command( + @app_commands.command( name="hello", - brief="Says hello to the bot", description="Says hello to the bot (because they are doing such a great job!)", - usage="", + extras={"module": "hello"}, ) - async def hello(self, ctx): - await self.hello_command(ctx) + async def hello_app_command(self: Self, interaction: discord.Interaction): + await interaction.response.send_message("🇭 🇪 🇾") + ``` Extensions can be configured per-guild with settings saved on Postgres. There are several extensions included in the main repo, so please reference them for more advanced examples. diff --git a/techsupport_bot/commands/hello.py b/techsupport_bot/commands/hello.py index 78fb87753..3d1e468ae 100644 --- a/techsupport_bot/commands/hello.py +++ b/techsupport_bot/commands/hello.py @@ -1,15 +1,17 @@ """ -Module for the hello command on the discord bot. -This module has unit tests -This modules requires no config, no databases, and no APIs +Commands: /hello +Config: None +Databases: None +Unit tests: No need """ from __future__ import annotations from typing import TYPE_CHECKING, Self -from core import auxiliary, cogs -from discord.ext import commands +import discord +from core import cogs +from discord import app_commands if TYPE_CHECKING: import bot @@ -27,26 +29,15 @@ async def setup(bot: bot.TechSupportBot) -> None: class Greeter(cogs.BaseCog): """Class for the greeter command.""" - async def hello_command(self: Self, ctx: commands.Context) -> None: - """A simple function to add HEY reactions to the command invocation - - Args: - ctx (commands.Context): The context in which the command was run in - """ - await auxiliary.add_list_of_reactions( - message=ctx.message, reactions=["🇭", "🇪", "🇾"] - ) - - @commands.command( + @app_commands.command( name="hello", - brief="Says hello to the bot", description="Says hello to the bot (because they are doing such a great job!)", - usage="", + extras={"module": "hello"}, ) - async def hello(self: Self, ctx: commands.Context) -> None: - """Entry point for the .hello command on discord + async def hello_app_command(self: Self, interaction: discord.Interaction): + """A simple command to hace the bot say HEY to the invoker Args: - ctx (commands.Context): The context in which the command was run in + interaction (discord.Interaction): The interaction that called this command """ - await self.hello_command(ctx) + await interaction.response.send_message("🇭 🇪 🇾") diff --git a/techsupport_bot/core/auxiliary.py b/techsupport_bot/core/auxiliary.py index 60ff50b61..c724cbfbd 100644 --- a/techsupport_bot/core/auxiliary.py +++ b/techsupport_bot/core/auxiliary.py @@ -93,7 +93,11 @@ async def add_list_of_reactions(message: discord.Message, reactions: list) -> No reactions (list): A list of all unicode emojis to add """ for emoji in reactions: - await message.add_reaction(emoji) + try: + await message.add_reaction(emoji) + except discord.NotFound: + # Message was deleted, ignore and stop executing + return def construct_mention_string(targets: list[discord.User]) -> str: diff --git a/techsupport_bot/tests/commands_tests/test_extensions_hello.py b/techsupport_bot/tests/commands_tests/test_extensions_hello.py deleted file mode 100644 index 2f0afc3ed..000000000 --- a/techsupport_bot/tests/commands_tests/test_extensions_hello.py +++ /dev/null @@ -1,38 +0,0 @@ -""" -This is a file to test the extensions/hello.py file -This contains 1 test -""" - -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_Hello: - """A single test to test the hello command""" - - @pytest.mark.asyncio - async def test_hello_command(self: Self) -> None: - """This is a test to ensure that the proper reactions are called, - and in the proper order""" - # Step 1 - Setup env - discord_env = config_for_tests.FakeDiscordEnv() - auxiliary.add_list_of_reactions = AsyncMock() - discord_env.context.message = discord_env.message_person1_noprefix_1 - - # Step 2 - Call the function - await discord_env.hello.hello_command(discord_env.context) - - # Step 3 - Assert that everything works - auxiliary.add_list_of_reactions.assert_called_once_with( - message=discord_env.message_person1_noprefix_1, reactions=["🇭", "🇪", "🇾"] - ) - - # Step 4 - Cleanup - importlib.reload(auxiliary) From 79612a4afdf7c6071f0910de8ba57aeca3d42d24 Mon Sep 17 00:00:00 2001 From: ajax146 <31014239+ajax146@users.noreply.github.com> Date: Fri, 2 May 2025 11:36:43 -0400 Subject: [PATCH 2/3] Add return type None to function --- README.md | 2 +- techsupport_bot/commands/hello.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index f82604597..9c158dbb7 100644 --- a/README.md +++ b/README.md @@ -81,7 +81,7 @@ class Greeter(cogs.BaseCog): description="Says hello to the bot (because they are doing such a great job!)", extras={"module": "hello"}, ) - async def hello_app_command(self: Self, interaction: discord.Interaction): + async def hello_app_command(self: Self, interaction: discord.Interaction) -> None: await interaction.response.send_message("🇭 🇪 🇾") ``` diff --git a/techsupport_bot/commands/hello.py b/techsupport_bot/commands/hello.py index 3d1e468ae..30c12cbb0 100644 --- a/techsupport_bot/commands/hello.py +++ b/techsupport_bot/commands/hello.py @@ -34,7 +34,7 @@ class Greeter(cogs.BaseCog): description="Says hello to the bot (because they are doing such a great job!)", extras={"module": "hello"}, ) - async def hello_app_command(self: Self, interaction: discord.Interaction): + async def hello_app_command(self: Self, interaction: discord.Interaction) -> None: """A simple command to hace the bot say HEY to the invoker Args: From 0077431f9b2a4bd110ff0a98b6451b2ab00cd9e1 Mon Sep 17 00:00:00 2001 From: dkay Date: Sat, 3 May 2025 20:08:35 -0700 Subject: [PATCH 3/3] fix typo Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- techsupport_bot/commands/hello.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/techsupport_bot/commands/hello.py b/techsupport_bot/commands/hello.py index 30c12cbb0..6c0a1b646 100644 --- a/techsupport_bot/commands/hello.py +++ b/techsupport_bot/commands/hello.py @@ -35,7 +35,7 @@ class Greeter(cogs.BaseCog): extras={"module": "hello"}, ) async def hello_app_command(self: Self, interaction: discord.Interaction) -> None: - """A simple command to hace the bot say HEY to the invoker + """A simple command to have the bot say HEY to the invoker Args: interaction (discord.Interaction): The interaction that called this command