diff --git a/chouette/utils/ranking.py b/chouette/utils/ranking.py index 3fadbd2..e5b048f 100644 --- a/chouette/utils/ranking.py +++ b/chouette/utils/ranking.py @@ -6,7 +6,13 @@ from chouette.utils.birthdays import month_to_str from chouette.utils.hypixel_data import experience_to_level -from chouette.utils.skyblock import get_profile, get_stats, load_skyblock, save_skyblock +from chouette.utils.skyblock import ( + get_hypixel_player, + get_profile, + get_stats, + load_skyblock, + save_skyblock, +) SPACES = " " * 38 @@ -35,7 +41,8 @@ async def update_stats(api_key: str) -> str: if not profile[0]: raise Exception("Error while updating stats") profile = profile[1] - new_data.get(uuid).update(await get_stats(session, api_key, pseudo, uuid, profile)) + player = await get_hypixel_player(session, api_key, uuid) + new_data.get(uuid).update(await get_stats(session, pseudo, uuid, player, profile)) msg += f"\n{SPACES}- {pseudo} sur le profil {profile_name}" await save_skyblock(new_data) # TODO: handle comparison using old and new data (see issue #56) diff --git a/chouette/utils/skyblock.py b/chouette/utils/skyblock.py index 75ed6b8..ed27c59 100644 --- a/chouette/utils/skyblock.py +++ b/chouette/utils/skyblock.py @@ -35,17 +35,11 @@ async def minecraft_uuid(session: ClientSession, pseudo: str) -> tuple[bool, str return True, json.get("id") -async def hypixel_discord(session: ClientSession, api_key: str, uuid: str) -> tuple[bool, str]: +async def hypixel_discord(player: dict) -> tuple[bool, str]: """Retourne le pseudo Discord lié à un compte Hypixel.""" - async with session.get( - f"{HYPIXEL_API}player", params={"key": api_key, "uuid": uuid} - ) as response: - json: dict = await response.json() - if response.status != 200: - return False, json.get("cause") - if not json.get("player").get("socialMedia", {}).get("links", {}).get("DISCORD", ""): - return False, "Vous n'avez pas associé votre compte Discord à Hypixel" - return True, json.get("player").get("socialMedia").get("links").get("DISCORD") + if not player.get("player").get("socialMedia", {}).get("links", {}).get("DISCORD", ""): + return False, "Vous n'avez pas associé votre compte Discord à Hypixel" + return True, player.get("player").get("socialMedia").get("links").get("DISCORD") async def selected_profile( @@ -109,12 +103,11 @@ async def get_networth(session: ClientSession, pseudo: str, profile_id: str) -> return json.get("profiles").get(profile_id).get("data").get("networth").get("networth", 0) -async def get_stats(session, api_key, pseudo, uuid, profile) -> dict[str, float]: +async def get_stats(session, pseudo, uuid, hypixel_player, profile) -> dict[str, float]: """Retourne les statistiques d'un joueur Skyblock avec l'API.""" info = profile.get("members").get(uuid) level: float = (info.get("leveling").get("experience")) / 100 networth = await get_networth(session, pseudo, profile.get("profile_id")) - hypixel_player = await get_hypixel_player(session, api_key, uuid) skill = info.get("player_data").get("experience") skills: tuple[float, float, float, float, float, float, float, float, float, float] = ( skill.get("SKILL_FISHING", 0), @@ -163,7 +156,8 @@ async def pseudo_to_profile( api_key = client.config.get("HYPIXEL_KEY") - discord = await hypixel_discord(session, api_key, uuid) + player = await get_hypixel_player(session, api_key, uuid) + discord = await hypixel_discord(player) if not discord[0]: # TODO: better handling return discord[1] @@ -183,7 +177,8 @@ async def pseudo_to_profile( client.bot_logger.debug(f"Le profil {profile.get('cute_name')} a été trouvé") info = {uuid: {"discord": discord, "pseudo": pseudo, "profile": profile.get("cute_name")}} - info.get(uuid).update(await get_stats(session, api_key, pseudo, uuid, profile)) + info.get(uuid).update(await get_stats(session, pseudo, uuid, player, profile)) + client.bot_logger.debug("Les stats ont bien été calculées") file_content = await load_skyblock() if file_content.get(uuid, {}).get("profile", "") != profile.get("profile_id"): file_content.update(info)