import discord
import aiohttp
import lxml.html
from lxml import html
from discord import reaction
from discord.ext.commands import bot
from discord.ext import commands, tasks

intents = discord.Intents.default()
intents.message_content = True
client = discord.Client(intents=intents)
bot = commands.Bot(command_prefix='!', intents=intents)

@client.event
async def on_ready() -> None:
    print(f'{client.user} is now running!')


@client.event
async def on_message(message):
    if str(message.channel) == "💵-bank-robber-nemesis":
        if '<@&1218616329775616090>' not in message.content:
            await message.delete()
    if str(message.channel) == "🍯-hive-outpost-nemesis":
        if '<@&1218616489268084846>' not in message.content:
            await message.delete()
    if str(message.channel) == "🔴-nemesis-reports":
        if "<@&1218617131176955904>" not in message.content:
            if "<@&1246779271230521355>" not in message.content:
                await message.delete()
    if str(message.channel) == "🟡-archfoe-reports":
        if '<@&1218617209807573063>' not in message.content:
            await message.delete()
    if str(message.channel) == "🟢-bane-reports":
        if '<@&1218617277692383303>' not in message.content:
            await message.delete()
    if str(message.channel) == "🐴-rare-bestiary":
        if '<@&1245487719207927871>' not in message.content:
            await message.delete()
    if str(message.channel) == "🧙-ferumbras":
        if '<@&1220310657413550150>' not in message.content:
            await message.delete()
    if str(message.channel) == "🔥-morgaroth":
        if '<@&1220310824485126186>' not in message.content:
            await message.delete()
    if str(message.channel) == "💙-ghazbaran":
        if '<@&1220310734173634621>' not in message.content:
            await message.delete()
    if str(message.channel) == "💔-orshabaal":
        if '<@&1220310886825332776>' not in message.content:
            await message.delete()
    if str(message.channel) == "💜-morshabaal":
        if '<@&1220310940080279622>' not in message.content:
            await message.delete()
    if str(message.channel) == "💚-abomination":
        if '<@&1220311008653082696>' not in message.content:
            await message.delete()
    if (message.content.startswith('<@&1218617131176955904>')):
        await message.add_reaction('🦵')
        await message.add_reaction('👍')
    if (message.content.startswith('<@&1246779271230521355>')):
        await message.add_reaction('🦵')
        await message.add_reaction('👍')
    if (message.content.startswith('<@&1218616329775616090>')):
        await message.add_reaction('🦵')
        await message.add_reaction('👍')
    if (message.content.startswith('<@&1218616489268084846>')):
        await message.add_reaction('🦵')
        await message.add_reaction('👍')
    if (message.content.startswith('<@&1218617209807573063>')):
        await message.add_reaction('🦵')
        await message.add_reaction('👍')
    if (message.content.startswith('<@&1218617277692383303>')):
        await message.add_reaction('🦵')
        await message.add_reaction('👍')
    if (message.content.startswith('<@&1245487719207927871>')):
        await message.add_reaction('🦵')
        await message.add_reaction('👍')
    if (message.content.startswith('<@&1220311008653082696>')):
        await message.add_reaction('🦵')
        await message.add_reaction('👍')
        await message.add_reaction('🌐')
    if (message.content.startswith('<@&1220310940080279622>')):
        await message.add_reaction('🦵')
        await message.add_reaction('👍')
        await message.add_reaction('🌐')
    if (message.content.startswith('<@&1220310886825332776>')):
        await message.add_reaction('🦵')
        await message.add_reaction('👍')
        await message.add_reaction('🌐')
    if (message.content.startswith('<@&1220310734173634621>')):
        await message.add_reaction('🦵')
        await message.add_reaction('👍')
        await message.add_reaction('🌐')
    if (message.content.startswith('<@&1220310824485126186>')):
        await message.add_reaction('🦵')
        await message.add_reaction('👍')
        await message.add_reaction('🌐')
    if (message.content.startswith('<@&1220310657413550150>')):
        await message.add_reaction('🦵')
        await message.add_reaction('👍')
        await message.add_reaction('🌐')
    if (message.content.startswith('!today')):
        async with aiohttp.ClientSession() as session:
            async with session.get(f'some site here') as r:
                channel = client.get_channel(1245022140156088421)  # replace with channel_id
                if r.status == 200:
                    text = await r.text()
                    tr = html.fromstring(text)
                    # buyers = tr.xpath("tbody/tr/td/a/text()")
                    tables = tr.xpath("//table")
                    table = tr.xpath("//table")[0]
                    answer = "**TEXT**\n" if len(tables) >=2 else "TEXT\n"
                    for row in table.xpath(".//tbody//tr"):
                        cells = [cell.text_content() for cell in row.xpath(".//td")]
                        answer += f"{cells[0]}: {cells[1]}\n"
                    if len(tables) > 1:
                        table2 = tr.xpath("//table")[1]
                        answer += "TEXT\n"
                        for row in table2.xpath(".//tbody//tr"):
                            cells = [cell.text_content() for cell in row.xpath(".//td")]
                            answer += f"{cells[0]}: {cells[1]}\n"
                    await channel.send(answer)
                else:
                    await channel.send("failed")
@client.event
async def on_reaction_add(reaction, user):
    if client.user == user:
        return  # Ignore whatever the bot reacts to ; this event is triggered when the bot reacts as well
    if user != reaction.message.author and reaction.emoji not in ('🦵','👍'):
        await reaction.remove(user)  # remove emoji if the reactor is not the author and emoji not in whitelisted emojis
    if user == reaction.message.author:
        await reaction.message.add_reaction(reaction.emoji)  # bot reacts with same emoji that the author reacts with
    if user == reaction.message.author and reaction.emoji in ('☠'):
        await reaction.message.channel.create_thread(reaction.message.channel)