import nextcord
from nextcord.ext import commands

from userinfo import parsedinfo

class Logger(commands.Cog):
    def __init__(self, bot: commands.Bot):
        self.mod = None
        self.client = bot

    @commands.Cog.listener()
    async def on_message_delete(self, ctx: nextcord.Message):

        embedlist = [nextcord.Embed(
            title="Message delete",
            description=ctx.content,
            url="https://git.squog.ru/entar/SquogBot"
        )]

        embedlist[0].set_author(name=ctx.author.name, icon_url=ctx.author.avatar.url)

        for image in ctx.attachments:
            newembed = nextcord.Embed(url="https://entarapi.xyz")
            newembed.set_image(image.url)  # Unfortunately i cant add multiple images.
            embedlist.append(newembed)

        guild = await parsedinfo.find("guild", "id", ctx.guild.id)

        if guild and guild["log-channel"] != 0:
            channel = ctx.guild.get_channel(guild["log-channel"])
            await channel.send(embeds=embedlist)

    @commands.Cog.listener()
    async def on_command(self, ctx: commands.Context):
        embed = nextcord.Embed(
            title="Command used",
            description=ctx.message.content
        )
        embed.set_author(name=ctx.author.name, icon_url=ctx.author.avatar.url)
        embed.add_field(name="Channel", value=ctx.channel.name)

        guild = await parsedinfo.find("guild", "id", ctx.guild.id)

        if guild and guild["log-channel"] != 0:
            channel = ctx.guild.get_channel(guild["log-channel"])
            await channel.send(embed=embed)

    @commands.Cog.listener()
    async def on_ready(self):
        print(f"Bot ready as {self.client.user.name}")
        for guild in parsedinfo.parsed["guild"]:
            if guild["log-channel"] != 1:
                await self.client.get_channel(guild["log-channel"]).send("SquogBot instance loaded!")

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