-
Notifications
You must be signed in to change notification settings - Fork 9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Help module #300
Merged
Merged
Help module #300
Changes from all commits
Commits
Show all changes
28 commits
Select commit
Hold shift + click to select a range
4408846
list modules works
592bbec
s, help commands added
4bcb554
fixed message_id problems
9609ce0
changed help capabilities dict type to dict[tuple[str, ...], tuples[s…
6671e3c
added help for all command types in Questions
d512267
moved parsing module names from message text to utilities, help with …
765f024
moved ModuleHelp to utilities.help_utils.py and adjusted Questions mo…
bee23bd
cleaned up help_utils methods and added help command to HelpModule
14f7d87
fixed parsing command name and finished HelpModule docstring
c93e68a
moved test_longmessage to TestModule
d12f369
changed message.author.name to message.author.display_name except whe…
664b25b
added guard in discord.py around bot_dev_channel_id being None
6e51b70
changed '|' to ', ' for concatenating alt names
c5f980b
new QuestionSetter docstring
eab7bcf
changed STAMPY_HELP_MSG
562e581
wrote docstring for TestModule
0c4677b
reformatted help docstrings
ba29c0a
refactored help_utils and build_help
da0fb36
fixed mergeconflict
363c048
debugged getting list of modules
699e841
updated FILE_HEADER in build_help
98809b8
refactored cb_help and wrote tests for HelpModule
9cdf7bd
minor fixes
4f343eb
added tests for helpless modules and fixed regex in helped modules
7dd2f0d
refactored helpless_module_tests to use islice and sorted modules in …
29de7d9
fixed issue #297
87ce9cc
removed breakpoints
5a2513f
minor fixes of types and user names
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
""" | ||
Helps you interact with me | ||
|
||
List modules | ||
List what modules I have + short descriptions | ||
`s, list modules` | ||
|
||
Help | ||
You can ask me for help with (1) a particular module or (2) a particular command defined on a module | ||
`s, help <module-name>` - returns description of a module and lists all of its commands | ||
`s, help <command-name>` - returns description of a command | ||
""" | ||
from itertools import islice | ||
import re | ||
from textwrap import dedent | ||
|
||
from modules.module import IntegrationTest, Module, Response | ||
from utilities.help_utils import ModuleHelp | ||
from utilities.serviceutils import ServiceMessage | ||
|
||
|
||
class HelpModule(Module): | ||
STAMPY_HELP_MSG = dedent( | ||
"""\ | ||
If you'd like to get a general overview of my modules, say `s, list modules` | ||
For a description of a module and what commands are available for it, say `s, help <module-name>` | ||
For a detailed description of one of those commands say `s, help <command-name>` (where `command-name` is any of alternative names for that command)""" | ||
) | ||
|
||
def __init__(self): | ||
super().__init__() | ||
self.help = ModuleHelp.from_docstring(self.class_name, __doc__) | ||
self.re_help = re.compile(r"help \w+", re.I) | ||
|
||
def process_message(self, message: ServiceMessage) -> Response: | ||
if not (text := self.is_at_me(message)): | ||
return Response() | ||
if text == "list modules": | ||
return Response( | ||
confidence=10, | ||
text=self.list_modules(), | ||
why=f"{message.author.display_name} asked me to list my modules", | ||
) | ||
if text == "help": | ||
return Response( | ||
confidence=10, | ||
text=self.STAMPY_HELP_MSG, | ||
why=f"{message.author.display_name} asked me for generic help", | ||
) | ||
if self.re_help.match(text): | ||
return Response(confidence=10, callback=self.cb_help, args=[text, message]) | ||
|
||
return Response() | ||
|
||
def list_modules(self) -> str: | ||
msg_descrs = sorted( | ||
(mod.help.listed_descr for mod in self.utils.modules_dict.values()), | ||
key=str.casefold, | ||
) | ||
return "I have the following modules:\n" + "\n".join(msg_descrs) | ||
|
||
async def cb_help(self, msg_text: str, message: ServiceMessage) -> Response: | ||
help_content = msg_text[len("help ") :] | ||
# iterate over modules, sorted in reverse in order to put longer module names first to prevent overly eager matching | ||
for mod_name, mod in sorted(self.utils.modules_dict.items(), reverse=False): | ||
if cmd_help := mod.help.get_command_help(msg_text=help_content): | ||
return Response( | ||
confidence=10, | ||
text=cmd_help, | ||
why=f'{message.author.display_name} asked me for help with "{help_content}"', | ||
) | ||
# module help | ||
if mod_name.casefold() in help_content.casefold(): | ||
mod_help = mod.help.get_module_help(markdown=False) | ||
why = f"{message.author.display_name} asked me for help with module `{mod_name}`" | ||
if mod_help is None: | ||
msg_text = f"No help for module `{mod_name}`" | ||
why += " but help is not written for it" | ||
else: | ||
msg_text = mod_help | ||
return Response(confidence=10, text=msg_text, why=why) | ||
|
||
# nothing found | ||
return Response( | ||
confidence=10, | ||
text=f'I couldn\'t find any help info related to "{help_content}". Could you rephrase that?', | ||
why=f'{message.author.display_name} asked me for help with "{help_content}" but I found nothing.', | ||
) | ||
|
||
@property | ||
def test_cases(self) -> list[IntegrationTest]: | ||
module_help_tests = [ | ||
self.create_integration_test( | ||
test_message=f"help {mod_name}", | ||
expected_regex=rf"\*\*Module `{mod_name}`\*\*", | ||
) | ||
for mod_name, mod in self.utils.modules_dict.items() | ||
if not mod.help.empty | ||
] | ||
helpless_module_tests = list( | ||
islice( | ||
( | ||
self.create_integration_test( | ||
test_message=f"help {mod_name}", | ||
expected_regex=f"No help for module `{mod_name}`", | ||
) | ||
for mod_name, mod in self.utils.modules_dict.items() | ||
if mod.help.empty | ||
), | ||
3, | ||
) | ||
) | ||
return ( | ||
[ | ||
self.create_integration_test( | ||
test_message="list modules", | ||
expected_regex=r"I have the following modules:(\n- `\w+`[^\n]*)+", | ||
), | ||
self.create_integration_test( | ||
test_message="help", expected_response=self.STAMPY_HELP_MSG | ||
), | ||
] | ||
+ module_help_tests | ||
+ helpless_module_tests | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch! 😅