diff --git a/modules/billing_manager.py b/modules/billing_manager.py index b9f63ca..6af66a0 100644 --- a/modules/billing_manager.py +++ b/modules/billing_manager.py @@ -211,26 +211,35 @@ def obtenir_solde(utilisateur_nom: str) -> float: # --- SYNCHRONISATION DIRECTE POUR L'ADMINISTRATEUR PRINCIPAL --- if utilisateur_nom.lower() in ["xavier"]: api_key = os.getenv("OPENROUTER_API_KEY") - if api_key: + + if not api_key: + log_event("CRITICAL", "BILLING", "Clé OPENROUTER_API_KEY introuvable dans l'environnement ! La synchronisation est impossible.") + else: try: - resp = requests.get("https://openrouter.ai/api/v1/auth/key", headers={"Authorization": f"Bearer {api_key}"}, timeout=3) + resp = requests.get("https://openrouter.ai/api/v1/auth/key", headers={"Authorization": f"Bearer {api_key}"}, timeout=5) if resp.status_code == 200: data = resp.json().get("data", {}) - - credit_total = data.get('usage', 0.0) - limite_restante = data.get('limit_remaining') - balance = data.get('balance', 0.0) - vrai_solde = float(limite_restante) if limite_restante is not None else float(balance) + usage = float(data.get('usage', 0.0)) + limit = data.get('limit') + balance = data.get('balance') - if vrai_solde > 0 or limite_restante is not None: + vrai_solde = 0.0 + if limit is not None: + vrai_solde = float(limit) - usage + elif balance is not None: + vrai_solde = float(balance) + + if vrai_solde > 0 or (limit is not None) or (balance is not None): soldes = charger_soldes() soldes["cagnotte_commune_vip"] = vrai_solde soldes["xavier"] = vrai_solde sauvegarder_soldes(soldes) return vrai_solde + else: + log_event("ERROR", "BILLING", f"Refus de l'API OpenRouter (Code {resp.status_code}) : {resp.text}") except Exception as e: - log_event("WARNING", "BILLING", f"Échec de synchronisation OpenRouter : {e}") + log_event("WARNING", "BILLING", f"Échec réseau lors de la connexion à OpenRouter : {e}") # --- COMPORTEMENT STANDARD POUR LES AUTRES UTILISATEURS --- soldes = charger_soldes()