diff --git a/README.md b/README.md index 5eab70ba..9c158dbb 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) -> None: + 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 78fb8775..6c0a1b64 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) -> None: + """A simple command to have 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 60ff50b6..c724cbfb 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 2f0afc3e..00000000 --- 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)