import requests
import time
import json
import os
import random

def read_statuses(file_name):
    with open(file_name, "r", encoding="utf-8") as file:
        return [line.strip() for line in file.readlines()]

def get_user_info(token):
    header ={
        'authorization': token
    }
    r = requests.get("https://discord.com/api/v10/users/@me", headers=header)
    if r.status_code == 200:
        user_info = r.json()
        return user_info["username"] + "#" + user_info["discriminator"], True
    else:
        return "Token invalid", False

def change_status(token, message):
    header = {
        'authorization': token
    }

    current_status = requests.get("https://discord.com/api/v8/users/@me/settings", headers=header).json()

    custom_status = current_status.get("custom_status", {})
    if custom_status is None:
        custom_status = {}
    custom_status["text"] = message

    jsonData = {
        "custom_status": custom_status,
        "activities": current_status.get("activities", [])
    }

    r = requests.patch("https://discord.com/api/v8/users/@me/settings", headers=header, json=jsonData)
    return r.status_code

def clear_console():
    os.system('cls' if os.name=='nt' else 'clear')

def load_config():
    with open("config.json", "r") as file:
        return json.load(file)

config = load_config()
token = config["token"]
clear_enabled = config["clear_enabled"]
clear_interval = config["clear_interval"]
sleep_interval = config["sleep_interval"]

status_count = 0  

while True:
    user_info, is_valid_token = get_user_info(token)
    statuses = read_statuses("statuses.txt")
    
    status = random.choice(statuses)
    
    time_formatted = time.strftime("%I:%M %p:")
    if is_valid_token:
        token_masked = token[:10] + "*****"
        token_info = f"{token_masked} | {user_info}"
    else:
        token_info = "Token invalid"
    
    status_colored = status
    print(f"{time_formatted} Status changed. New status: {status_colored}")
    change_status(token, status)
    
    status_count += 1
    time.sleep(sleep_interval)
    
    if clear_enabled and status_count % clear_interval == 0:
        clear_console()