"""
Information extension.
"""
import datetime

import discord
from discord.ext import commands

from dog import Cog
from dog.core import utils

cm = lambda v: utils.commas(v)

SERVER_INFO_MEMBERS = '''{} total member(s)
{} online, {} offline
{}% online'''

SERVER_INFO_COUNT = '''{} role(s)
{} text channel(s), {} voice channel(s)
{} channel(s)'''



class Info(Cog):
    @commands.command()
    @commands.guild_only()
    async def server(self, ctx):
        """ Shows information about the server. """
        g = ctx.guild
        embed = discord.Embed(title=g.name)

        # server icon
        if g.icon_url:
            embed.set_thumbnail(url=g.icon_url)

        # members
        total_members = len(g.members)
        num_online = len(list(filter(lambda m: m.status is discord.Status.online, g.members)))
        num_offline = len(list(filter(lambda m: m.status is discord.Status.offline, g.members)))
        embed.add_field(name='Members', value=SERVER_INFO_MEMBERS.format(
            cm(total_members), cm(num_online), cm(num_offline),
            round((num_online / total_members) * 100, 2)
        ))

        embed.add_field(name='Count', value=SERVER_INFO_COUNT.format(
            cm(len(g.roles)), cm(len(g.text_channels)), cm(len(g.voice_channels)),
            cm(len(g.channels))
        ))

        # guild owner
        embed.set_footer(text=f'Owned by {g.owner}', icon_url=g.owner.avatar_url)

        await ctx.send(embed=embed)

    @commands.command()
    @commands.guild_only()
    async def roles(self, ctx):
        """ Views the roles (and their IDs) in this server. """
        sorted_roles = sorted(ctx.guild.roles, key=lambda r: r.position, reverse=True)
        code = '```\n' + '\n'.join([f'\N{BULLET} {r.name} ({r.id})' for r in sorted_roles]) + '\n```'
        try:
            await ctx.send(code)
        except discord.HTTPException:
            await ctx.send(f'You have too many roles ({len(ctx.guild.roles)}) for me to list!')

    def _make_joined_embed(self, member):
        embed = utils.make_profile_embed(member)
        joined_dif = utils.ago(member.created_at)
        embed.add_field(name='Joined Discord',
                        value=(f'{joined_dif}\n' +
                               utils.american_datetime(member.created_at)))
        return embed


    @commands.command()
    @commands.guild_only()
    async def joined(self, ctx, target: discord.Member=None):
        """
        Shows when someone joined this server and Discord.

        If no arguments were passed, your information is shown.
        """
        if target is None:
            target = ctx.message.author

        embed = self._make_joined_embed(target)
        joined_dif = utils.ago(target.joined_at)
        embed.add_field(name='Joined this Server',
                        value=(f'{joined_dif}\n' +
                               utils.american_datetime(target.joined_at)),
                        inline=False)

        await ctx.send(embed=embed)

    @commands.command()
    @commands.guild_only()
    async def earliest(self, ctx):
        """ Shows who in this server had the earliest Discord join time. """
        members = {m: m.created_at for m in ctx.guild.members if not m.bot}
        earliest_time = min(members.values())
        for member, time in members.items():
            if earliest_time == time:
                await ctx.send(embed=self._make_joined_embed(member))


def setup(bot):
    bot.add_cog(Info(bot))

@@ -376,34 +376,1125 @@
 ommands.command(
+aliases=%5B'user'%5D)%0A    @commands.guild_only()%0A    async def profile(self, ctx, *, who: discord.Member = None):%0A        %22%22%22 Shows information about a user. %22%22%22%0A        who = who or ctx.author%0A%0A        embed = discord.Embed(title=f'%7Bwho%7D %5CN%7BEM DASH%7D %7Bwho.id%7D')%0A        embed.set_thumbnail(url=who.avatar_url_as(format='png'))%0A%0A        # roles%0A        embed.add_field(name='Roles', value=' '.join(%5Br.mention for r in who.roles if r != ctx.guild.default_role%5D))%0A%0A        # shared servers%0A        shared_servers = len(%5Bg for g in ctx.bot.guilds if who in g.members%5D)%0A        embed.add_field(name='Shared Servers', value=shared_servers)%0A%0A        # joined%0A        def add_joined_field(*, attr, name, **kwargs):%0A            dt = getattr(who, attr)%0A            embed.add_field(name=name, value=f'%7Butils.ago(dt)%7D%5Cn%7Butils.american_datetime(dt)%7D UTC', **kwargs)%0A        add_joined_field(attr='joined_at', name='Joined this Server', inline=False)%0A        add_joined_field(attr='created_at', name='Joined Discord', inline=False)%0A%0A        await ctx.send(embed=embed)%0A%0A    @commands.command(aliases=%5B'guild'%5D
 )%0A
-
     @commands.gu
@@ -1651,14 +1651,38 @@
 tle=
+f'%7B
 g.name
+%7D %5CN%7BEM DASH%7D %7Bg.id%7D'
 )%0A%0A 
@@ -2415,16 +2415,16 @@
 nnels))%0A
-
         
@@ -2419,32 +2419,164 @@
 s))%0A        ))%0A%0A
+        created = f'Created: %7Bg.created_at%7D UTC%5Cn%7Butils.ago(g.created_at)%7D'%0A        embed.add_field(name='Created', value=created)%0A%0A
         # guild 

Add d?profile for user info