diff options
| author | Rapptz <[email protected]> | 2016-01-11 23:54:38 -0500 |
|---|---|---|
| committer | Rapptz <[email protected]> | 2016-01-11 23:59:11 -0500 |
| commit | 958d278771d719c32780c100e4c96a30830f8f95 (patch) | |
| tree | 3f60d2b65593d02b52141ec9effe07da8f28bd6e /discord/ext/commands/bot.py | |
| parent | Handle cases where people put False-like values for game presences. (diff) | |
| download | discord.py-958d278771d719c32780c100e4c96a30830f8f95.tar.xz discord.py-958d278771d719c32780c100e4c96a30830f8f95.zip | |
[commands] Initial implementation of help command.
Diffstat (limited to 'discord/ext/commands/bot.py')
| -rw-r--r-- | discord/ext/commands/bot.py | 49 |
1 files changed, 47 insertions, 2 deletions
diff --git a/discord/ext/commands/bot.py b/discord/ext/commands/bot.py index e09362ea..2b2ad625 100644 --- a/discord/ext/commands/bot.py +++ b/discord/ext/commands/bot.py @@ -29,11 +29,13 @@ import discord import inspect import importlib import sys +import functools -from .core import GroupMixin, Command +from .core import GroupMixin, Command, command from .view import StringView from .context import Context from .errors import CommandNotFound +from .formatter import HelpFormatter def _get_variable(name): stack = inspect.stack() @@ -50,6 +52,28 @@ def when_mentioned(bot, msg): to being mentioned, e.g. ``@bot ``.""" return '{0.user.mention} '.format(bot) +@command(pass_context=True, name='help') +def _default_help_command(ctx, *commands : str): + """Shows this message.""" + bot = ctx.bot + destination = ctx.message.channel if not bot.pm_help else ctx.message.author + # help by itself just lists our own commands. + if len(commands) == 0: + pages = bot.formatter.format_help_for(ctx, bot) + else: + try: + command = functools.reduce(dict.__getitem__, commands, bot.commands) + except KeyError as e: + yield from bot.send_message(destination, 'No command called {} found.'.format(e)) + return + + pages = bot.formatter.format_help_for(ctx, command) + + for page in pages: + yield from bot.send_message(destination, page) + + class Bot(GroupMixin, discord.Client): """Represents a discord bot. @@ -74,13 +98,34 @@ class Bot(GroupMixin, discord.Client): multiple checks for the prefix should be used and the first one to match will be the invocation prefix. You can get this prefix via :attr:`Context.prefix`. + description : str + The content prefixed into the default help message. + formatter : :class:`HelpFormatter` + The formatter used to format the help message. By default, it uses a + the :class:`HelpFormatter`. Check it for more info on how to override it. + If you want to change the help command completely (add aliases, etc) then + a call to :meth:`remove_command` with 'help' as the argument would do the + trick. + pm_help : bool + A boolean that indicates if the help command should PM the user instead of + sending it to the channel it received it from. Defaults to ``False``. """ - def __init__(self, command_prefix, **options): + def __init__(self, command_prefix, formatter=None, description=None, pm_help=False, **options): super().__init__(**options) self.command_prefix = command_prefix self.extra_events = {} self.cogs = {} self.extensions = {} + self.description = description + self.pm_help = pm_help + if formatter is not None: + if not isinstance(formatter, HelpFormatter): + raise discord.ClientException('Formatter must be a subclass of HelpFormatter') + self.formatter = formatter + else: + self.formatter = HelpFormatter() + + self.add_command(_default_help_command) # internal helpers |