From 71c558776628bd010fb90abe9a8191f92b3865a9 Mon Sep 17 00:00:00 2001 From: univ Date: Tue, 13 Feb 2018 16:06:36 +0000 Subject: moved formatter.py to correct dir, and changed __main__.py to use the formatter --- bot/__main__.py | 4 ++- bot/cogs/formatter.py | 94 --------------------------------------------------- bot/formatter.py | 94 +++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 97 insertions(+), 95 deletions(-) delete mode 100644 bot/cogs/formatter.py create mode 100644 bot/formatter.py diff --git a/bot/__main__.py b/bot/__main__.py index 6d5c2c063..306e33ff5 100644 --- a/bot/__main__.py +++ b/bot/__main__.py @@ -5,6 +5,7 @@ from discord import Game from discord.ext.commands import AutoShardedBot, when_mentioned_or from bot.utils import CaseInsensitiveDict +from bot.formatter import Formatter bot = AutoShardedBot( command_prefix=when_mentioned_or( @@ -14,7 +15,8 @@ bot = AutoShardedBot( ">>>", ">>", ">" ), # Order matters (and so do commas) game=Game(name="Help: bot.help()"), - help_attrs={"aliases": ["help()"]} + help_attrs={"aliases": ["help()"]}, + formatter=Formatter() ) bot.cogs = CaseInsensitiveDict() diff --git a/bot/cogs/formatter.py b/bot/cogs/formatter.py deleted file mode 100644 index 56f25fbd6..000000000 --- a/bot/cogs/formatter.py +++ /dev/null @@ -1,94 +0,0 @@ -# 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 discord.ext.commands import HelpFormatter, Paginator, Command -from inspect import getfullargspec, formatargspec -from bot.constants import HELP_PREFIX - -class Formatter(HelpFormatter): - def __init__(self, *args, **kwargs): - super().__init__(*args, **kwargs) - - # basically the same function but changed: - # - to make the helptext appear as a comment - # - to change the indentation to the PEP8 standard: 4 spaces - def _add_subcommands_to_page(self, max_width, commands): - for name, command in commands: - if name in command.aliases: - # skip aliases - continue - - entry = " {0:<{width}} # {1}".format(name, command.short_doc, width=max_width) - shortened = self.shorten(entry) - self._paginator.add_line(shortened) - - async def format(self): - self._paginator = Paginator(prefix="```py") - - if isinstance(self.command, Command): - # help on a specific command to look like an async function - - # example: - # async def (ctx, ): - # """ - # - # """ - # await do_(ctx, ) - - # strip the command of bot. and () - stripped_command = self.command.name.replace(HELP_PREFIX, "").replace("()", "") - # getting 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 arguments - arguments = arguments.replace("self, ", "") - args_no_type_hints = args_no_type_hints.replace("self, ", "") - # first line of help containing the command name and arguments - definition = f"async def {stripped_command}{arguments}:" - self._paginator.add_line(definition) - # next few lines containing the help text formatted as a docstring - self._paginator.add_line(f" \"\"\"\n {self.command.help}\n \"\"\"") - # last line 'invoking' the command - self._paginator.add_line(f" await do_{stripped_command}({args_no_type_hints})") - - return self._paginator.pages - - # if not a specific command, help page will be in the format: - # class : - # bot.() # - # class : - # bot.() # - # - # # - - 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 \ No newline at end of file diff --git a/bot/formatter.py b/bot/formatter.py new file mode 100644 index 000000000..56f25fbd6 --- /dev/null +++ b/bot/formatter.py @@ -0,0 +1,94 @@ +# 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 discord.ext.commands import HelpFormatter, Paginator, Command +from inspect import getfullargspec, formatargspec +from bot.constants import HELP_PREFIX + +class Formatter(HelpFormatter): + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + + # basically the same function but changed: + # - to make the helptext appear as a comment + # - to change the indentation to the PEP8 standard: 4 spaces + def _add_subcommands_to_page(self, max_width, commands): + for name, command in commands: + if name in command.aliases: + # skip aliases + continue + + entry = " {0:<{width}} # {1}".format(name, command.short_doc, width=max_width) + shortened = self.shorten(entry) + self._paginator.add_line(shortened) + + async def format(self): + self._paginator = Paginator(prefix="```py") + + if isinstance(self.command, Command): + # help on a specific command to look like an async function + + # example: + # async def (ctx, ): + # """ + # + # """ + # await do_(ctx, ) + + # strip the command of bot. and () + stripped_command = self.command.name.replace(HELP_PREFIX, "").replace("()", "") + # getting 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 arguments + arguments = arguments.replace("self, ", "") + args_no_type_hints = args_no_type_hints.replace("self, ", "") + # first line of help containing the command name and arguments + definition = f"async def {stripped_command}{arguments}:" + self._paginator.add_line(definition) + # next few lines containing the help text formatted as a docstring + self._paginator.add_line(f" \"\"\"\n {self.command.help}\n \"\"\"") + # last line 'invoking' the command + self._paginator.add_line(f" await do_{stripped_command}({args_no_type_hints})") + + return self._paginator.pages + + # if not a specific command, help page will be in the format: + # class : + # bot.() # + # class : + # bot.() # + # + # # + + 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 \ No newline at end of file -- cgit v1.2.3