import psutil

from pypresence import Presence
from tools.get_server_token import GetServerTokenForDiscord
from tools.constants import Urls
from tools.discord_oauth import CLIENT_ID

class DiscordToken:
    @staticmethod
    def decode_discord_token():
        discord_token = GetServerTokenForDiscord.get_token(GetServerTokenForDiscord.authenticate(Urls.API_URL.value))
        return discord_token

class CheckDiscord:

    @staticmethod
    def isdiscordrunning() -> bool:
        for process in psutil.process_iter(["name"]):
            if (process.info["name"].lower() == "discord.exe" or
                process.info["name"].lower() == "discordcanary.exe" or
                process.info["name"].lower() == "discord" or
                process.info["name"].lower() == "discord canary"):
                return True
        return False

    @staticmethod
    def isuserconnected() -> bool:
        rpc = Presence(CLIENT_ID)
        try:
            rpc.connect()
            return True
        except Exception as e:
            return False
        finally:
            try:
                rpc.close()
            except Exception as e:
                pass
