"""
Core commands.
"""
import inspect

from discord.ext import commands
from discord.ext.commands import Command, CheckFailure

from joku.bot import Jokusoramame


class Core(object):
    """
    Core command class.
    """

    def __init__(self, bot: Jokusoramame):
        self.bot = bot

    def can_run_recursive(self, ctx, command: Command):
        # Check if the command has a parent.
        if command.parent is not None:
            rec = self.can_run_recursive(ctx, command.parent)
            if not rec:
                return False

        try:
            can_run = command.can_run(ctx)
        except CheckFailure:
            return False
        else:
            return can_run

    @commands.command(pass_context=True)
    async def help(self, ctx, command: str = None):
        """
        Help command.
        """
        prefix = ctx.prefix

        if command is None:
            # List the commands.
            base = "**Commands:**\nUse `{}help <command>` for more information about each command.\n\n".format(prefix)
            for n, (name, cls) in enumerate(self.bot.cogs.items()):
                # Increment N, so we start at 1 index instead of 0.
                n += 1

                cmds = []

                # Get a list of commands on the cog.
                members = inspect.getmembers(cls)
                for cname, m in members:
                    if isinstance(m, Command):
                        # Check if the author can run the command.
                        try:
                            if self.can_run_recursive(ctx, m):
                                cmds.append("`" + m.name + "`")
                        except CheckFailure:
                            pass

                base += "**{}. {}: ** {}\n".format(n, name, ' '.join(cmds) if cmds else "`No commands available to "
                                                                                        "you.`")

            await self.bot.say(base)
        else:
            # Check if the command is in the commands dict.
            if command not in self.bot.commands:
                await self.bot.say(":x: This command does not exist.")
                return
            # Use the default HelpFormatter to construct a nice message.
            fmtted = self.bot.formatter.format_help_for(ctx, self.bot.commands[command])
            for page in fmtted:
                await self.bot.say(page)


def setup(bot: Jokusoramame):
    bot.add_cog(Core(bot))

@@ -155,16 +155,59 @@
 oramame%0A
+from joku.redis import with_redis_cooldown%0A
 %0A%0Aclass 
@@ -736,16 +736,907 @@
 an_run%0A%0A
+    @commands.command(pass_context=True)%0A    async def stats(self, ctx):%0A        %22%22%22%0A        Shows stats about the bot.%0A        %22%22%22%0A        tmp = %7B%0A            %22shards%22: self.bot.manager.max_shards,%0A            %22servers%22: sum(1 for _ in self.bot.manager.get_all_servers()),%0A            %22members%22: sum(1 for _ in self.bot.manager.get_all_members()),%0A            %22unique_members%22: self.bot.manager.unique_member_count,%0A            %22channels%22: sum(1 for _ in self.bot.manager.get_all_channels()),%0A            %22shard%22: self.bot.shard_id%0A        %7D%0A%0A        await self.bot.say(%22Currently connected to %60%7Bservers%7D%60 servers, %22%0A                           %22with %60%7Bchannels%7D%60 channels %22%0A                           %22and %60%7Bmembers%7D%60 members (%60%7Bunique_members%7D%60 unique) %22%0A                           %22across %60%7Bshards%7D%60 shards.%5Cn%5Cn%22%0A                           %22This is shard ID **%7Bshard%7D**.%22.format(**tmp))%0A%0A
     @com

Add stats command to core.