#It works!
import asyncio
import discord
import json
import oauth2
import os
import pafy
import pickle
import simplejson
import sys
import time
from datetime import datetime
from random import randint
import urllib
import urllib.request
import urllib.response
import urllib.error
import youtube_dl

import points_stuff
import messages
import general_commands
import mod_commands
import chat_logging
import music

client = discord.Client()

modlist = open("modlist.txt","r")
mod = modlist.read()#List of mods on 1 line, edit at your pleasure
mod = mod.split(";")
modlist.close()

#Main Body
@client.async_event
def on_message(message):
    split_message = message.content.split()
    server = str(message.server)
    message.author.name = message.author.name.lower()
    message_author = message.author.name

    chat_logging.logging_consent(message)
    if "!chatlog" in message.content.lower():
        logging_chat = chat_logging.logging_config(message)
        if logging_chat != None:
            yield from client.send_message(message.author, logging_chat)

    adding_points = points_stuff.add_points(message)
    if adding_points != True:
        print("An error has occurred.")

    #General Commands
    result = general_commands.command_check(message)
    if result != None:
        yield from client.send_message(message.channel, result)

    #Adding commands
    if message.content.startswith("!addcom".casefold()):
        if message_author in mod:
            added_command = mod_commands.add_command(message)
            yield from client.send_message(message.channel, added_command)
        else:
            yield from client.send_message(message.author, "You do not have permission to perform that command.")

    #Replacing commands
    if message.content.startswith("!repcom".casefold()) or message.content.startswith("!editcom".casefold()):
        if message_author in mod:
            edited_command = mod_commands.edit_command(message)
            yield from client.send_message(message.channel, edited_command)
        else:
            yield from client.send_message(message.author, "You do not have permission to perform that command.")

    #Deleting commands
    if message.content.startswith("!delcom".casefold()):
        if message_author in mod:
            if len(split_message) < 2:
                yield from client.send_message(message.author, "Invalid amount of parameters")
            else:
                mod_commands.delete_command(message,split_message[1])
                yield from client.send_message(message.channel, "Successfully deleted.")
        else:
            yield from client.send_message(message.author, "You do not have permission to perform that command.")

    #Banning
    if message.content.startswith("!ban".casefold()):
        if message_author in mod:
            if len(split_message) < 2:
                yield from client.send_message(message.author, "Invalid user")
            else:
                try:
                    for member in message.server.members:
                        split_message[1] = split_message[1].replace("<", "")
                        split_message[1] = split_message[1].replace(">", "")
                        split_message[1] = split_message[1].replace("@", "")#strips the string down to the id of the member
                        if split_message[1] in member.id:#is this the member that you want to ban?
                            yield from client.ban(member)
                            yield from client.send_message(message.channel, "{} Has been banned from the server".format(split_message[1]))
                            break
                except:
                    yield from client.send_message(message.author, "That user does not exist")


    #Command info
    if message.content.startswith("!commandinfo".casefold()):
        command_info = general_commands.command_info(message)
        yield from client.send_message(message.channel, command_info)


    #!commands
    if message.content.startswith("!commands".casefold()):
        list_of_commands = general_commands.list_commands(message)
        yield from client.send_message(message.channel, list_of_commands)

    #is it?
    if message.content.startswith("!itis".casefold()):
        number = randint(1,10)
        if number % 4 == 0:
            yield from client.send_message(message.channel, "Is it?")
        else:
            yield from client.send_message(message.channel, "It isn't")

    #Checking if a stream is live using urllib and json
    if message.content.startswith("!live".casefold()):
        try:
            bot_message = general_commands.stream_live_check(message.content.split()[1])
        except IndexError:
            bot_message = general_commands.stream_live_check("r00kieoftheyear")
        yield from client.send_message(message.channel, bot_message)

    if message.content.startswith("!info".casefold()):
        roles = ""
        for i in range(len(message.author.roles)):
            message.author.roles[i] = str(message.author.roles[i]).replace("@", "")
            roles += message.author.roles[i] + " "

        bot_message = "```"
        bot_message += "ID: {}\n".format(message.author.id)
        bot_message += "Username: {0} and {1}\n".format(message.author, message.author.name)
        bot_message += "Account created: {}\n".format(message.author.created_at)
        bot_message += "Date joined: {}\n".format(message.author.joined_at)
        bot_message += "Roles: {}\n".format(roles)
        bot_message += "Your avatar is {}\n```".format(message.author.avatar_url)
        yield from client.send_message(message.channel, bot_message)

    if message.content.startswith("!serverinfo".casefold()):
        bot_message = "Name: {}\n".format(message.server.name)
        bot_message += "Server Owner: {}\n".format(message.server.owner.name)
        bot_message += "ID: {}\n".format(message.server.id)
        bot_message += "Region: {}\n".format(message.server.region)