Esempio n. 1
0
    def apropos(self, mess, args):
        """   Returns a help string listing available options.

        Automatically assigned to the "help" command."""
        if not args:
            return 'Usage: ' + self._bot.prefix + 'apropos search_term'

        description = 'Available commands:\n'

        cls_commands = {}
        for (name, command) in self._bot.all_commands.items():
            cls = get_class_that_defined_method(command)
            cls = str.__module__ + '.' + cls.__name__  # makes the fuul qualified name
            commands = cls_commands.get(cls, [])
            if not self.bot_config.HIDE_RESTRICTED_COMMANDS or self._bot.check_command_access(mess, name)[0]:
                commands.append((name, command))
                cls_commands[cls] = commands

        usage = ''
        for cls in sorted(cls_commands):
            usage += '\n'.join(sorted([
                '\t' + self._bot.prefix + '%s: %s' % (
                    name.replace('_', ' ', 1),
                    (command.__doc__ or '(undocumented)').strip().split('\n', 1)[0]
                )
                for (name, command) in cls_commands[cls]
                if args is not None and
                command.__doc__ is not None and
                args.lower() in command.__doc__.lower() and
                name != 'help' and not command._err_command_hidden
            ]))
        usage += '\n\n'

        return ''.join(filter(None, [description, usage])).strip()
Esempio n. 2
0
    def help(self, mess, args):
        """   Returns a help string listing available options.

        Automatically assigned to the "help" command."""
        def may_access_command(cmd):
            mess, _, _ = self._bot._process_command_filters(
                mess, cmd, None, True)
            return mess is not None

        usage = ''
        if not args:
            description = '### Available help\n\n'
            command_classes = sorted(set(self._bot.get_command_classes()),
                                     key=lambda c: c.__name__)
            usage = '\n'.join(
                '- **' + self._bot.prefix + 'help %s** \- %s' %
                (clazz.__name__, clazz.__errdoc__.strip() or '(undocumented)')
                for clazz in command_classes)
        elif args == 'full':
            description = '### Available commands\n\n'

            clazz_commands = {}
            for (name, command) in self._bot.commands.items():
                clazz = get_class_that_defined_method(command)
                commands = clazz_commands.get(clazz, [])
                if not self.bot_config.HIDE_RESTRICTED_COMMANDS or may_access_command(
                        name):
                    commands.append((name, command))
                    clazz_commands[clazz] = commands

            for clazz in sorted(set(clazz_commands), key=lambda c: c.__name__):
                usage += '\n\n**%s** \- %s\n' % (clazz.__name__,
                                                 clazz.__errdoc__ or '')
                usage += '\n'.join(
                    sorted([
                        '**' + self._bot.prefix + '%s** %s' %
                        (name.replace('_', ' ', 1),
                         (self._bot.get_doc(command).strip()).split('\n',
                                                                    1)[0])
                        for (name, command) in clazz_commands[clazz]
                        if name != 'help' and not command._err_command_hidden
                        and (not self.bot_config.HIDE_RESTRICTED_COMMANDS
                             or may_access_command(name))
                    ]))
            usage += '\n\n'
        elif args in (clazz.__name__
                      for clazz in self._bot.get_command_classes()):
            # filter out the commands related to this class
            commands = [
                (name, command)
                for (name, command) in self._bot.commands.items()
                if get_class_that_defined_method(command).__name__ == args
            ]
            description = '### Available commands for %s\n\n' % args
            usage += '\n'.join(
                sorted([
                    '- **' + self._bot.prefix + '%s** \- %s' %
                    (name.replace('_', ' ', 1),
                     (self._bot.get_doc(command).strip()).split('\n', 1)[0])
                    for (name, command) in commands
                    if not command._err_command_hidden and (
                        not self.bot_config.HIDE_RESTRICTED_COMMANDS
                        or may_access_command(name))
                ]))
        else:
            description = ''
            if args in self._bot.commands:
                usage = (self._bot.commands[args].__doc__
                         or 'undocumented').strip()
            else:
                usage = self.MSG_HELP_UNDEFINED_COMMAND

        top = self._bot.top_of_help_message()
        bottom = self._bot.bottom_of_help_message()
        return ''.join(filter(None, [top, description, usage, bottom]))
Esempio n. 3
0
    def help(self, mess, args):
        """Returns a help string listing available options.
        Automatically assigned to the "help" command."""

        def may_access_command(msg, cmd):
            msg, _, _ = self._bot._process_command_filters(
                msg=msg,
                cmd=cmd,
                args=None,
                dry_run=True
            )
            return msg is not None

        def get_name(named):
            return named.__name__.lower()

        # Normalize args to lowercase for ease of use
        args = args.lower() if args else ''
        usage = ''
        description = '### All commands\n'

        # show all
        if not args:
            cls_commands = {}
            for (name, command) in self._bot.all_commands.items():
                cls = get_class_that_defined_method(command)
                commands = cls_commands.get(cls, [])
                if not self.bot_config.HIDE_RESTRICTED_COMMANDS or may_access_command(mess, name):
                    commands.append((name, command))
                    cls_commands[cls] = commands

            for cls in sorted(set(cls_commands), key=lambda c: c.__name__):
                # shows class and description
                usage += '\n**{name}**\n\n*{doc}*\n\n'.format(
                    name=cls.__name__,
                    doc=cls.__errdoc__.strip() or '',
                )

                for (name, command) in cls_commands[cls]:
                    if command._err_command_hidden:
                        continue
                    # show individual commands
                    usage += self._cmd_help_line(name, command)
            usage += '\n\n'  # end cls section
        elif args in (get_name(cls) for cls in self._bot.get_command_classes()):
            # filter out the commands related to this class
            [cls] = {
                c for c in self._bot.get_command_classes()
                if get_name(c) == args
            }
            commands = [
                (name, command) for (name, command)
                in self._bot.all_commands.items() if
                get_name(get_class_that_defined_method(command)) == args]

            description = '\n**{name}**\n\n*{doc}*\n\n'.format(
                name=cls.__name__,
                doc=cls.__errdoc__.strip() or '',
            )
            pairs = sorted([
                (name, command)
                for (name, command) in commands
                if not command._err_command_hidden and
                (not self.bot_config.HIDE_RESTRICTED_COMMANDS or may_access_command(mess, name))
            ])

            for (name, command) in pairs:
                usage += self._cmd_help_line(name, command)
        else:
            description = ''
            all_commands = dict(self._bot.all_commands)
            all_commands.update(
                {k.replace('_', ' '): v for k, v in all_commands.items()})
            if args in all_commands:
                usage = self._cmd_help_line(args, all_commands[args], True)
            else:
                usage = self.MSG_HELP_UNDEFINED_COMMAND

        return ''.join(filter(None, [description, usage]))
Esempio n. 4
0
File: help.py Progetto: andrevdm/err
    def help(self, mess, args):
        """   Returns a help string listing available options.

        Automatically assigned to the "help" command."""

        def may_access_command(cmd):
            mess, _, _ = self._bot._process_command_filters(mess, cmd, None, True)
            return mess is not None

        usage = ''
        if not args:
            description = '### Available help\n\n'
            command_classes = sorted(set(self._bot.get_command_classes()), key=lambda c: c.__name__)
            usage = '\n'.join(
                '- **' + self._bot.prefix + 'help %s** \- %s' %
                (clazz.__name__, clazz.__errdoc__.strip() or '(undocumented)') for clazz in command_classes)
        elif args == 'full':
            description = '### Available commands\n\n'

            clazz_commands = {}
            for (name, command) in self._bot.all_commands.items():
                clazz = get_class_that_defined_method(command)
                commands = clazz_commands.get(clazz, [])
                if not self.bot_config.HIDE_RESTRICTED_COMMANDS or may_access_command(name):
                    commands.append((name, command))
                    clazz_commands[clazz] = commands

            for clazz in sorted(set(clazz_commands), key=lambda c: c.__name__):
                usage += '\n\n**%s** \- %s\n' % (clazz.__name__, clazz.__errdoc__ or '')
                usage += '\n'.join(sorted(['**' +
                                           self._bot.prefix +
                                           '%s** %s' % (name.replace('_', ' ', 1),
                                                        (self._bot.get_doc(command).strip()).split('\n', 1)[0])
                                           for (name, command) in clazz_commands[clazz]
                                           if name != 'help' and not command._err_command_hidden and
                                           (not self.bot_config.HIDE_RESTRICTED_COMMANDS or may_access_command(name))
                                           ]))
            usage += '\n\n'
        elif args in (clazz.__name__ for clazz in self._bot.get_command_classes()):
            # filter out the commands related to this class
            commands = [(name, command) for (name, command) in self._bot.all_commands.items() if
                        get_class_that_defined_method(command).__name__ == args]
            description = '### Available commands for %s\n\n' % args
            usage += '\n'.join(sorted([
                '- **' + self._bot.prefix + '%s** \- %s' % (name.replace('_', ' ', 1),
                                                            (self._bot.get_doc(command).strip()).split('\n', 1)[0])
                for (name, command) in commands
                if not command._err_command_hidden and
                (not self.bot_config.HIDE_RESTRICTED_COMMANDS or may_access_command(name))
            ]))
        else:
            description = ''
            if args in self._bot.all_commands:
                usage = (self._bot.all_commands[args].__doc__ or
                         'undocumented').strip()
            else:
                usage = self.MSG_HELP_UNDEFINED_COMMAND

        top = self._bot.top_of_help_message()
        bottom = self._bot.bottom_of_help_message()
        return ''.join(filter(None, [top, description, usage, bottom]))