-
-
Notifications
You must be signed in to change notification settings - Fork 752
HelpFormatter #7
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
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
b46e048
added formatter.py
5aaa68c
added main help and modulestring
72ec2f8
added help for specific commands
5e66d20
fixed imports and added new constant
51deeeb
fixed broken import again
3b0aeb5
added example
71c5587
moved formatter.py to correct dir, and changed __main__.py to use the…
1ed8176
reordered imports on __main__.py
f6fe8c8
fixed pylint errors
swedgwood e9cbf2e
fixed more pylint
swedgwood 4b3eef1
changed commented examples to docstrings
swedgwood d02a4fa
fixed styling (again)
swedgwood 252fa75
changed the way command names are shown (due to the change in command…
swedgwood 3b54d65
added type hinting, and rearranged some code for readability
swedgwood 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 hidden or 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 hidden or 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 hidden or 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,102 @@ | ||
| # coding=utf-8 | ||
|
|
||
| """ | ||
| Credit to Rapptz's script used as an example: | ||
| https://github.com/Rapptz/discord.py/blob/rewrite/discord/ext/commands/formatter.py | ||
| Which falls under The MIT License. | ||
| """ | ||
|
|
||
| import itertools | ||
| from inspect import formatargspec, getfullargspec | ||
|
|
||
| from discord.ext.commands import Command, HelpFormatter, Paginator | ||
|
|
||
| from bot.constants import HELP_PREFIX | ||
|
|
||
|
|
||
| class Formatter(HelpFormatter): | ||
| def __init__(self, *args, **kwargs): | ||
| super().__init__(*args, **kwargs) | ||
|
|
||
| def _add_subcommands_to_page(self, max_width: int, commands: list): | ||
| """ | ||
| basically the same function from d.py but changed: | ||
| - to make the helptext appear as a comment | ||
| - to change the indentation to the PEP8 standard: 4 spaces | ||
| """ | ||
| for name, command in commands: | ||
| if name in command.aliases: | ||
| # skip aliases | ||
| continue | ||
|
|
||
| entry = " {0}{1:<{width}} # {2}".format(HELP_PREFIX, name, command.short_doc, width=max_width) | ||
| shortened = self.shorten(entry) | ||
| self._paginator.add_line(shortened) | ||
|
|
||
| async def format(self): | ||
| """ | ||
| rewritten help command to make it more python-y | ||
|
|
||
| example of specific command: | ||
| async def <command>(ctx, <args>): | ||
| \""" | ||
| <help text> | ||
| \""" | ||
| await do_<command>(ctx, <args>) | ||
|
|
||
| example of standard help page: | ||
| class <cog1>: | ||
| bot.<command1>() # <command1 help> | ||
| class <cog2>: | ||
| bot.<command2>() # <command2 help> | ||
|
|
||
| # <ending help note> | ||
| """ | ||
| self._paginator = Paginator(prefix="```py") | ||
|
|
||
| if isinstance(self.command, Command): | ||
| # strip the command of bot. and () | ||
| stripped_command = self.command.name.replace(HELP_PREFIX, "").replace("()", "") | ||
|
|
||
| # get the args using the handy inspect module | ||
| argspec = getfullargspec(self.command.callback) | ||
| arguments = formatargspec(*argspec) | ||
| args_no_type_hints = ", ".join(argspec[0]) | ||
|
|
||
| # remove self from the args | ||
| arguments = arguments.replace("self, ", "") | ||
| args_no_type_hints = args_no_type_hints.replace("self, ", "") | ||
|
|
||
| # prepare the different sections of the help output, and add them to the paginator | ||
| definition = f"async def {stripped_command}{arguments}:" | ||
| docstring = f" \"\"\"\n {self.command.help}\n \"\"\"" | ||
| invocation = f" await do_{stripped_command}({args_no_type_hints})" | ||
| self._paginator.add_line(definition) | ||
| self._paginator.add_line(docstring) | ||
| self._paginator.add_line(invocation) | ||
|
|
||
| return self._paginator.pages | ||
|
|
||
| max_width = self.max_name_size | ||
|
|
||
| def category_check(tup): | ||
| cog = tup[1].cog_name | ||
| # zero width character to make it appear last when put in alphabetical order | ||
| return cog if cog is not None else "\u200bNoCategory" | ||
|
|
||
| command_list = await self.filter_command_list() | ||
| data = sorted(command_list, key=category_check) | ||
|
|
||
| for category, commands in itertools.groupby(data, key=category_check): | ||
| commands = sorted(commands) | ||
| if len(commands) > 0: | ||
| self._paginator.add_line(f"class {category}:") | ||
| self._add_subcommands_to_page(max_width, commands) | ||
|
|
||
| self._paginator.add_line() | ||
| ending_note = self.get_ending_note() | ||
| # make the ending note appear as comments | ||
| ending_note = "# "+ending_note.replace("\n", "\n# ") | ||
| self._paginator.add_line(ending_note) | ||
|
|
||
| return self._paginator.pages | ||
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.
this blank line is, I believe, against the law