import logging
from discord.ext import commands

LOGGER = logging.getLogger("Discord_Radio_Bot.LinkCop")


class ClearMessages(commands.Cog):
    def __init__(self, bot):
        self.Bot = bot

    @commands.command()
    async def clear(self, ctx, amount=2):
        member = ctx.author.display_name
        member_id = ctx.author.id

        mtn_member = f"<@{member_id}>"
        LOGGER.info(f"Clear {amount} messages requested by {member}")

        authors = {}
        async for message in ctx.channel.history(limit=amount):
            if message.author not in authors:
                authors[message.author] = 1
            else:
                authors[message.author] += 1
            await message.delete()

        msg = f"{mtn_member}, I deleted {sum(authors.values())} messages\n"
        LOGGER.debug(f"Deleted {sum(authors.values())} messages from {ctx.message.channel}")

        for author in authors.keys():
            msg += f"\t{str(author).split('#', 1)[0]}: {authors[author]}\n"
            LOGGER.debug(f"Deleted {authors[author]} messages from {author}")

        await ctx.channel.send(msg)


def setup(bot: commands.Bot):
    bot.add_cog(ClearMessages(bot))
