Skip to content

Commit

Permalink
Refactor HelpPlugin get_help_string
Browse files Browse the repository at this point in the history
  • Loading branch information
unode committed Aug 19, 2022
1 parent 226ad94 commit 2630978
Showing 1 changed file with 24 additions and 19 deletions.
43 changes: 24 additions & 19 deletions mmpy_bot/plugins/help_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,28 @@
from mmpy_bot.wrappers import Message


def _custom_help_sort(rec):
return (
rec.metadata.get("category", ""), # No categories first
rec.help_type,
rec.pattern.lstrip("^[(-"),
)


def _prepare_function_help_message(h, string):
cmd = h.metadata.get("human_description", h.pattern)
direct = "`(*)`" if h.direct else ""
mention = "`(+)`" if h.mention else ""

if h.help_type == "webhook":
string += f"- `{cmd}` {direct} {mention} - (webhook) {h.docheader}\n"
else:
if not h.docheader:
string += f"- `{cmd}` {direct} {mention}\n"
else:
string += f"- `{cmd}` {direct} {mention} - {h.docheader}\n"


class HelpPlugin(Plugin):
"""Provide a `help` command that lists functions provided by all plugins.
Expand Down Expand Up @@ -41,14 +63,7 @@ def get_help_string(self, message: Message) -> str:
Help information is presented in a condensed format, grouped into categories
"""

def custom_sort(rec):
return (
rec.metadata.get("category", ""), # No categories first
rec.help_type,
rec.pattern.lstrip("^[(-"),
)

help_function_info = sorted(self.get_help(message), key=custom_sort)
help_function_info = sorted(self.get_help(message), key=_custom_help_sort)

string = "### The following functions have been registered:\n\n"
string += "###### `(*)` require the use of `@botname`, "
Expand All @@ -63,17 +78,7 @@ def custom_sort(rec):
category = "uncategorized" if category is None else category
string += f"Category `{category}`:\n"

cmd = h.metadata.get("human_description", h.pattern)
direct = "`(*)`" if h.direct else ""
mention = "`(+)`" if h.mention else ""

if h.help_type == "webhook":
string += f"- `{cmd}` {direct} {mention} - (webhook) {h.docheader}\n"
else:
if not h.docheader:
string += f"- `{cmd}` {direct} {mention}\n"
else:
string += f"- `{cmd}` {direct} {mention} - {h.docheader}\n"
string = _prepare_function_help_message(h, string)

return string

Expand Down

0 comments on commit 2630978

Please sign in to comment.