import discord
from discord.ext import commands
import sys
import os
import traceback
from discord.utils import find
import git
from pyfiglet import Figlet
from dep import is_dev
from dep import owner_id
from dep import initial_extensions

bot = commands.Bot(command_prefix='>')


@bot.event
async def on_ready():
    f = Figlet(font='lean')
    print(f.renderText('Japbot'))
    print('Logged in as')
    print(bot.user.name)
    print(bot.user.id)
    print('------')
    await bot.change_presence(activity=discord.Game(name='Japanimatie'))  # laat rin dit ook maar bedenken wat die speelt


@bot.event
async def on_member_join(member):
    guild = member.guild
    general = find(lambda x: x.name == 'testen', guild.text_channels)
    await general.send(f'{member.name} ')  # laat rin bedenken


@bot.event
async def on_command_error(ctx, error):
    await ctx.send(f'Oeps: `{error}`')


@bot.command(aliases=['rs'])
@commands.check(is_dev)
async def restart(ctx):
    """Start de bot opnieuw op"""
    await ctx.send('Bot restarting')
    os.execv(sys.executable, ['python3.6'] + sys.argv)
    sys.exit(0)


@bot.command(aliases=['pull'])
@commands.check(is_dev)
async def gitpull(ctx):
    """Laadt een nieuwe versie uit de Git repo"""
    repo = git.Repo(os.path.dirname(os.path.realpath(__file__)))
    msg = repo.git.pull()
    usr_s = ctx.message.author.display_name
    usr_r = ctx.bot.get_user(owner_id)
    if usr_r is not None:
        await usr_r.send(f'Git has been pulled by: {usr_s}\n`{msg}`')
    else:
        await ctx.send('`Could not message owner`')
    await ctx.send(f'`{msg}`')


@bot.command()
@commands.check(is_dev)
async def update(ctx):
    """Laad een nieuwe versie van de bot en start hem opnieuw op"""
    await ctx.send('Bot is updating, this will take a few seconds.')
    repo = git.Repo(os.path.dirname(os.path.realpath(__file__)))
    msg = repo.git.pull()
    usr_s = ctx.message.author.display_name
    usr_r = ctx.bot.get_user(owner_id)
    if usr_r is not None:
        await usr_r.send(f'Bot has been uodated by: {usr_s}\n`{msg}`')
    else:
        await ctx.send('`Could not message owner`')
    await ctx.send(f'`{msg}`')
    os.execv(sys.executable, ['python3.6'] + sys.argv)
    sys.exit(0)


@bot.command(aliases=['t'])
async def test(ctx):
    """Nutteloze test"""
    await ctx.send("Test succesful")


@bot.command()
@commands.check(is_dev)
async def load(ctx, ext):  # ext stands for extension
    """Loads an extension."""
    bot.load_extension(ext)
    await ctx.send(f'{ext} loaded')


@bot.command()
@commands.check(is_dev)
async def unload(ctx, ext):  # ext stands for extension
    """Unloads an extension."""
    bot.unload_extension(ext)
    await ctx.send(f'{ext} unloaded')


@bot.command()
@commands.check(is_dev)
async def reload(ctx, ext):  # ext stands for extension
    """Reloads an extension."""
    bot.unload_extension(ext)
    bot.load_extension(ext)
    await ctx.send(f'{ext} reloaded')


if __name__ == '__main__':
    for extension in initial_extensions:
        try:
            bot.load_extension(extension)
        except Exception as e:
            print(f'Failed to load extension {extension}.', file=sys.stderr)
traceback.print_exc()


bot.run('NDU1MzkxNDYxNjIyNTQ2NDQy.Df7T4g.mZM1KaMipx3EDJPbnFB85ugoWV4')



