Add a provisional API for replacing the help formatter (#4011)

* Adds an API for replacing the help formatter

* Apply suggestions from code review

Co-authored-by: jack1142 <6032823+jack1142@users.noreply.github.com>

* add note about provisionality

Co-authored-by: jack1142 <6032823+jack1142@users.noreply.github.com>
This commit is contained in:
Michael H
2020-08-05 20:12:14 -04:00
committed by GitHub
parent 6cef336417
commit 4f808306ba
4 changed files with 177 additions and 23 deletions

View File

@@ -208,6 +208,61 @@ class RedBase(
self._deletion_requests: MutableMapping[int, asyncio.Lock] = weakref.WeakValueDictionary()
def set_help_formatter(self, formatter: commands.help.HelpFormatterABC):
"""
Set's Red's help formatter.
.. warning::
This method is provisional.
The formatter must implement all methods in
``commands.help.HelpFormatterABC``
Cogs which set a help formatter should inform users of this.
Users should not use multiple cogs which set a help formatter.
This should specifically be an instance of a formatter.
This allows cogs to pass a config object or similar to the
formatter prior to the bot using it.
See ``commands.help.HelpFormatterABC`` for more details.
Raises
------
RuntimeError
If the default formatter has already been replaced
TypeError
If given an invalid formatter
"""
if not isinstance(formatter, commands.help.HelpFormatterABC):
raise TypeError(
"Help formatters must inherit from `commands.help.HelpFormatterABC` "
"and implement the required interfaces."
)
# do not switch to isinstance, we want to know that this has not been overriden,
# even with a subclass.
if type(self._help_formatter) is commands.help.RedHelpFormatter:
self._help_formatter = formatter
else:
raise RuntimeError("The formatter has already been overriden.")
def reset_help_formatter(self):
"""
Resets Red's help formatter.
.. warning::
This method is provisional.
This exists for use in ``cog_unload`` for cogs which replace the formatter
as well as for a rescue command in core_commands.
"""
self._help_formatter = commands.help.RedHelpFormatter()
def get_command(self, name: str) -> Optional[commands.Command]:
com = super().get_command(name)
assert com is None or isinstance(com, commands.Command)
@@ -786,12 +841,18 @@ class RedBase(
return await super().start(*args, **kwargs)
async def send_help_for(
self, ctx: commands.Context, help_for: Union[commands.Command, commands.GroupMixin, str]
self,
ctx: commands.Context,
help_for: Union[commands.Command, commands.GroupMixin, str],
*,
from_help_command: bool = False,
):
"""
Invokes Red's helpformatter for a given context and object.
"""
return await self._help_formatter.send_help(ctx, help_for)
return await self._help_formatter.send_help(
ctx, help_for, from_help_command=from_help_command
)
async def embed_requested(self, channel, user, command=None) -> bool:
"""