import aiohttp
import asyncio
import os
from colorama import init, Fore, Style

init(autoreset=True)

LOGO = Fore.GREEN + r"""
   ____ _               _               
  / ___| |__   ___  ___| | _____ _ __  
 | |   | '_ \ / _ \/ __| |/ / _ \ '__| 
 | |___| | | |  __/ (__|   <  __/ |    
  \____|_| |_|\___|\___|_|\_\___|_|    
""" + Style.RESET_ALL

async def validate_token(token):
    url = "https://discord.com/api/v9/users/@me"

    headers = {
        "Authorization": token
    }

    async with aiohttp.ClientSession() as session:
        async with session.get(url, headers=headers) as response:
            if response.status == 200:
                return True
            elif response.status == 401 or response.status == 403:
                return False
            else:
                print(f"Unexpected status {response.status} for token: {token}")
                return False

async def check_tokens(file_path):
    valid_tokens = []
    invalid_tokens = []

    try:
        with open(file_path, 'r') as file:
            tokens = [line.strip() for line in file]

        for token in tokens:
            is_valid = await validate_token(token)
            if is_valid:
                valid_tokens.append(token)
                print(f'{Fore.GREEN}Token: {token} is valid{Style.RESET_ALL}')
            else:
                invalid_tokens.append(token)
                print(f'{Fore.RED}Token: {token} is invalid{Style.RESET_ALL}')

        with open('valid.txt', 'w') as valid_file:
            valid_file.write('\n'.join(valid_tokens))

        with open('invalid.txt', 'w') as invalid_file:
            invalid_file.write('\n'.join(invalid_tokens))

        print("Validation complete. Results have been saved.")

    except FileNotFoundError:
        print(f"File not found at path: {file_path} or path is incorrect.")
    except IsADirectoryError:
        print(f"Path points to a directory, not a file: {file_path}")
    except Exception as e:
        print(f"An unexpected error occurred: {e}")

def find_tokens_file(directory):
    for root, dirs, files in os.walk(directory):
        if 'tokens.txt' in files:
            return os.path.join(root, 'tokens.txt')
    return None

def main_menu():
    print(LOGO)
    while True:
        print("Select the desired function:")
        print("1) Token validator")

        choice = input("Enter the function number: ")

        if choice == '1':
            directory = input("Enter the directory path to search for the tokens file: ")
            file_path = find_tokens_file(directory)
            if file_path:
                asyncio.run(check_tokens(file_path))
            else:
                print(f"File 'tokens.txt' not found in directory {directory} and its subdirectories.")
        else:
            print("Invalid choice, please try again.")

if __name__ == '__main__':
    main_menu()