improve help

This commit is contained in:
HgO 2024-04-18 11:58:51 +02:00
parent 3145f32812
commit 2baaf5e310
2 changed files with 27 additions and 37 deletions

View file

@ -238,22 +238,34 @@ class HelpCommand(BaseCommand):
async def process(self) -> None: async def process(self) -> None:
"""Show the help text""" """Show the help text"""
logger.debug(f"Displaying help to room {self.room.display_name}") logger.debug(f"Displaying help to room {self.room.display_name}")
if not self.args: if len(self.args) == 0:
text = ( text = (
"Hello, I am a bot made with matrix-nio! Use `help commands` to view " "Hello, I am a bot made with matrix-nio! Use 'help commands' to view "
"available commands." "available commands."
) )
await send_text_to_room(self.matrix_client, self.room.room_id, text)
return
topic = self.args[0]
if topic == "rules":
text = "These are the rules!"
elif topic == "commands":
text = "Available commands: ..."
else: else:
text = "Unknown help topic!" topic = self.args[0]
await send_text_to_room(self.matrix_client, self.room.room_id, text) if topic == "commands":
reactions = " ".join(
sorted(self.config.allowed_reactions - self.config.insult_reactions)
)
text = (
"Here is the list of available commands:\n"
"- help: Display this help message.\n"
"- ack: Create a silence for the alert that is replied to.\n"
"- unack: Remove a silence for the alert that is replied to.\n\n"
"You can also react with an emoji to an alert to create a silence. "
"Removing a reaction will remove the silence.\n"
f"Here is the list of allowed emoji to trigger a silence: {reactions}\n"
)
else:
text = (
"I'm sorry, I don't know much about this topic. "
"You can type 'help commands' to view a list of available commands."
)
await send_text_to_room(
self.matrix_client, self.room.room_id, text, notice=False
)
class AngryUserCommand(BaseCommand): class AngryUserCommand(BaseCommand):

View file

@ -109,6 +109,7 @@ class CommandTestCase(unittest.IsolatedAsyncioTestCase):
# We don't spec config, as it doesn't currently have well defined attributes # We don't spec config, as it doesn't currently have well defined attributes
self.fake_config = Mock() self.fake_config = Mock()
self.fake_config.allowed_rooms = [self.fake_room.room_id] self.fake_config.allowed_rooms = [self.fake_room.room_id]
self.fake_config.allowed_reactions = {"🤫", "😶", "🤐"}
@patch.object(matrix_alertbot.command.AckAlertCommand, "process") @patch.object(matrix_alertbot.command.AckAlertCommand, "process")
async def test_process_ack_command(self, fake_ack: Mock) -> None: async def test_process_ack_command(self, fake_ack: Mock) -> None:
@ -648,29 +649,6 @@ class CommandTestCase(unittest.IsolatedAsyncioTestCase):
_, _, text = fake_send_text_to_room.call_args.args _, _, text = fake_send_text_to_room.call_args.args
self.assertIn("help commands", text) self.assertIn("help commands", text)
@patch.object(matrix_alertbot.command, "send_text_to_room")
async def test_help_with_rules_topic(self, fake_send_text_to_room: Mock) -> None:
"""Tests the callback for InviteMemberEvents"""
# Tests that the bot attempts to join a room after being invited to it
command = HelpCommand(
self.fake_matrix_client,
self.fake_cache,
self.fake_alertmanager_client,
self.fake_config,
self.fake_room,
self.fake_sender,
self.fake_event_id,
("rules",),
)
await command.process()
# Check that we attempted to create silences
fake_send_text_to_room.assert_called_once()
_, _, text = fake_send_text_to_room.call_args.args
self.assertIn("rules!", text)
@patch.object(matrix_alertbot.command, "send_text_to_room") @patch.object(matrix_alertbot.command, "send_text_to_room")
async def test_help_with_commands_topic(self, fake_send_text_to_room: Mock) -> None: async def test_help_with_commands_topic(self, fake_send_text_to_room: Mock) -> None:
"""Tests the callback for InviteMemberEvents""" """Tests the callback for InviteMemberEvents"""
@ -692,7 +670,7 @@ class CommandTestCase(unittest.IsolatedAsyncioTestCase):
# Check that we attempted to create silences # Check that we attempted to create silences
fake_send_text_to_room.assert_called_once() fake_send_text_to_room.assert_called_once()
_, _, text = fake_send_text_to_room.call_args.args _, _, text = fake_send_text_to_room.call_args.args
self.assertIn("Available commands", text) self.assertIn("Here is the list of available commands", text)
@patch.object(matrix_alertbot.command, "send_text_to_room") @patch.object(matrix_alertbot.command, "send_text_to_room")
async def test_angry_user(self, fake_send_text_to_room: Mock) -> None: async def test_angry_user(self, fake_send_text_to_room: Mock) -> None:
@ -744,7 +722,7 @@ class CommandTestCase(unittest.IsolatedAsyncioTestCase):
# Check that we attempted to create silences # Check that we attempted to create silences
fake_send_text_to_room.assert_called_once() fake_send_text_to_room.assert_called_once()
_, _, text = fake_send_text_to_room.call_args.args _, _, text = fake_send_text_to_room.call_args.args
self.assertEqual("Unknown help topic!", text) self.assertIn("I'm sorry, I don't know much about this topic.", text)
@patch.object(matrix_alertbot.command, "send_text_to_room") @patch.object(matrix_alertbot.command, "send_text_to_room")
async def test_unknown_command(self, fake_send_text_to_room: Mock) -> None: async def test_unknown_command(self, fake_send_text_to_room: Mock) -> None: