import logging
from asyncio import sleep

import discord
from discord.ext import commands
from config import SETTINGS
from crew import crew_embed
from diary import diary_embed
from film import film_embed
from helpers import LetterboxdError
from list_ import list_embed
from review import review_embed
from user import user_embed

logging.basicConfig(
    level=logging.INFO,
    format='%(asctime)s | %(message)s',
    datefmt='%m/%d %H:%M:%S')

bot = commands.Bot(command_prefix='!', case_insensitive=True)
bot.remove_command('help')


@bot.event
async def on_ready():
    logging.info(
        'Logged in %d servers as %s' % (len(bot.guilds), bot.user.name))
    bot.loop.create_task(update_stats())


@bot.event
async def on_message(message):
    if message.content.startswith('!'):
        message.content = message.content.replace('’', '').replace('‘', '')
        await bot.process_commands(message)


async def update_stats():
    while True:
        await bot.change_presence(
            activity=discord.Game('!helplb - {} servers'.format(
                len(bot.guilds))))
        await sleep(900)


@bot.event
async def on_command_error(ctx, error):
    if isinstance(error, commands.MissingRequiredArgument):
        await ctx.send('This command requires a parameter.')
    elif isinstance(error, commands.BotMissingPermissions):
        await ctx.send('This command requires the {} permission.'.format(
                ', '.join(err for err in error.missing_perms)))
    elif isinstance(error, (commands.CommandNotFound, commands.CheckFailure)):
        return
    elif isinstance(error, commands.CommandInvokeError):
        if isinstance(error.original, discord.HTTPException):
            return
    else:
        await ctx.send('Sorry, the command crashed. :/')
        logging.error(ctx.message.content)
        raise error


async def send_msg(ctx, msg):
    if isinstance(msg, discord.Embed):
        await ctx.send(embed=msg)
    else:
        await ctx.send(msg)


# Commands


@bot.command()
async def helplb(ctx):
    help_embed = discord.Embed(colour=discord.Color.from_rgb(54, 57, 62))
    help_embed.set_thumbnail(url='https://i.imgur.com/Kr1diFu.png')
    help_embed.set_author(
        name='Letterboxd Bot', icon_url='https://i.imgur.com/5VALKVy.jpg')
    help_embed.set_footer(
        text='Created by Porkepik#2664',
        icon_url='https://i.imgur.com/li4cLpd.png')
    for key, value in SETTINGS['help'].items():
        help_embed.add_field(name=key, value=value, inline=False)
    help_embed.description = 'Invite Bot | '\
        + '[GitHub](https://github.com/Porkepik/Letterboxd-Bot)'
    await ctx.send(embed=help_embed)


@bot.command()
async def user(ctx, username):
    try:
        msg = await user_embed(username)
    except LetterboxdError as err:
        msg = err
    await send_msg(ctx, msg)


@bot.command()
async def diary(ctx, username):
    try:
        msg = await diary_embed(username)
    except LetterboxdError as err:
        msg = err
    await send_msg(ctx, msg)


@bot.command(aliases=['actor', 'actress', 'director'])
async def crew(ctx, *, arg):
    try:
        msg = await crew_embed(arg, ctx.invoked_with)
    except LetterboxdError as err:
        msg = err
    await send_msg(ctx, msg)


@bot.command(aliases=['movie'])
async def film(ctx, *, arg):
    try:
        # eiga.me ratings for specific servers
        if ctx.guild and ctx.guild.id in SETTINGS['mkdb_servers']:
            msg = await film_embed(arg, True)
        else:
            msg = await film_embed(arg)
    except LetterboxdError as err:
        msg = err
    await send_msg(ctx, msg)


async def check_if_two_args(ctx):
    msg = ctx.message.content.split()
    if len(msg) < 3:
        await ctx.send('This command requires 2 parameters.')
    return len(msg) > 2


@bot.command(name='list')
@commands.check(check_if_two_args)
async def list_(ctx, username, *args):
    try:
        msg = await list_embed(username, ' '.join(str(i) for i in args))
    except LetterboxdError as err:
        msg = err
    await send_msg(ctx, msg)


@bot.command(aliases=['entry'])
@commands.check(check_if_two_args)
async def review(ctx, username, *args):
    try:
        msg = await review_embed(username, ' '.join(str(i) for i in args))
    except LetterboxdError as err:
        msg = err
    await send_msg(ctx, msg)


@bot.command(name='del')
@commands.bot_has_permissions(manage_messages=True)
async def delete(ctx):
    await ctx.message.delete()
    found_bot_msg = False
    found_usr_cmd = False
    cmd_list = list()
    for command in bot.commands:
        cmd_list.append('!' + command.name)
        for alias in command.aliases:
            cmd_list.append('!' + alias)

    async for log_message in ctx.channel.history(limit=30):
        if log_message.author.id == bot.user.id and not found_bot_msg:
            bot_message = log_message
            found_bot_msg = True
        elif found_bot_msg:
            if log_message.content:
                first_word = log_message.content.split()[0]
            else:
                continue
            if first_word in cmd_list:
                found_usr_cmd = True
                cmd_message = log_message
                break

    if found_usr_cmd:
        if not ctx.author.permissions_in(ctx.channel).manage_messages:
            if not cmd_message.author.id == ctx.author.id:
                return
        await cmd_message.delete()
        await bot_message.delete()


bot.run(SETTINGS['discord'])

[((15, 0, 18, 29), 'logging.basicConfig', 'logging.basicConfig', (), '', False, 'import logging\n'), ((20, 6, 20, 61), 'discord.ext.commands.Bot', 'commands.Bot', (), '', False, 'from discord.ext import commands\n'), ((138, 1, 138, 34), 'discord.ext.commands.check', 'commands.check', ({(138, 16, 138, 33): 'check_if_two_args'}, {}), '(check_if_two_args)', False, 'from discord.ext import commands\n'), ((148, 1, 148, 34), 'discord.ext.commands.check', 'commands.check', ({(148, 16, 148, 33): 'check_if_two_args'}, {}), '(check_if_two_args)', False, 'from discord.ext import commands\n'), ((158, 1, 158, 51), 'discord.ext.commands.bot_has_permissions', 'commands.bot_has_permissions', (), '', False, 'from discord.ext import commands\n'), ((43, 14, 43, 24), 'asyncio.sleep', 'sleep', ({(43, 20, 43, 23): '(900)'}, {}), '(900)', False, 'from asyncio import sleep\n'), ((76, 38, 76, 72), 'discord.Color.from_rgb', 'discord.Color.from_rgb', ({(76, 61, 76, 63): '54', (76, 65, 76, 67): '57', (76, 69, 76, 71): '62'}, {}), '(54, 57, 62)', False, 'import discord\n'), ((93, 20, 93, 40), 'user.user_embed', 'user_embed', ({(93, 31, 93, 39): 'username'}, {}), '(username)', False, 'from user import user_embed\n'), ((102, 20, 102, 41), 'diary.diary_embed', 'diary_embed', ({(102, 32, 102, 40): 'username'}, {}), '(username)', False, 'from diary import diary_embed\n'), ((111, 20, 111, 53), 'crew.crew_embed', 'crew_embed', ({(111, 31, 111, 34): 'arg', (111, 36, 111, 52): 'ctx.invoked_with'}, {}), '(arg, ctx.invoked_with)', False, 'from crew import crew_embed\n'), ((122, 24, 122, 45), 'film.film_embed', 'film_embed', ({(122, 35, 122, 38): 'arg', (122, 40, 122, 44): '(True)'}, {}), '(arg, True)', False, 'from film import film_embed\n'), ((124, 24, 124, 39), 'film.film_embed', 'film_embed', ({(124, 35, 124, 38): 'arg'}, {}), '(arg)', False, 'from film import film_embed\n'), ((60, 8, 60, 42), 'logging.error', 'logging.error', ({(60, 22, 60, 41): 'ctx.message.content'}, {}), '(ctx.message.content)', False, 'import logging\n')]