Repository navigation
Expand file tree
/
Copy pathservices.py
More file actions
69 lines (50 loc) · 2.6 KB
/
Copy pathservices.py
File metadata and controls
69 lines (50 loc) · 2.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
"""Opérations partagées entre les commandes slash et les boutons."""
from __future__ import annotations
import asyncio
import logging
import discord
from embeds.history import build_history_embed
from embeds.profile import build_profile_embed
from models import PlayerRef
from riot_api import RiotAPI, RiotAPIError
logger = logging.getLogger("botdiff.services")
PROFILE_MATCH_COUNT = 10
HISTORY_MATCH_COUNT = 5
Message = tuple[list[discord.Embed], list[discord.File], discord.ui.View]
class UserFacingError(Exception):
"""Erreur dont le message peut être montré tel quel à l'utilisateur."""
def user_error_message(exc: BaseException) -> str:
"""Message d'erreur lisible pour Discord (sans détail technique)."""
if isinstance(exc, UserFacingError):
return f"❌ {exc}"
if isinstance(exc, RiotAPIError):
if exc.status == 404:
return "❌ Joueur ou partie introuvable. Vérifie le pseudo et le tag."
if exc.status in (401, 403):
return "❌ La clé API Riot est invalide ou a expiré."
if exc.status == 429:
return "❌ Trop de requêtes vers l'API Riot, réessaie dans une minute."
if exc.status == 0:
return "❌ L'API Riot ne répond pas, réessaie plus tard."
return "❌ L'API Riot est indisponible pour le moment, réessaie plus tard."
return "❌ Une erreur interne est survenue."
async def resolve_player(riot: RiotAPI, riot_id: str, tag: str) -> PlayerRef:
puuid = await riot.get_puuid(riot_id, tag)
return PlayerRef(riot_id, tag, puuid)
async def player_from_puuid(riot: RiotAPI, puuid: str) -> PlayerRef:
"""Riot ID actuel d'un PUUID (suit les changements de pseudo)."""
account = await riot.get_account_by_puuid(puuid)
return PlayerRef(account.get("gameName", "?"), account.get("tagLine", "?"), puuid)
async def load_profile(riot: RiotAPI, platform: str, player: PlayerRef) -> Message:
summoner, league_entries, matches = await asyncio.gather(
riot.get_summoner_by_puuid(platform, player.puuid),
riot.get_league_entries_by_puuid(platform, player.puuid),
riot.get_recent_matches(player.puuid, PROFILE_MATCH_COUNT),
)
embed, files, view = await build_profile_embed(player, summoner, league_entries, matches)
return [embed], files, view
async def load_history(riot: RiotAPI, player: PlayerRef) -> Message:
matches = await riot.get_recent_matches(player.puuid, HISTORY_MATCH_COUNT)
if not matches:
raise UserFacingError(f"Aucune partie récente trouvée pour **{player.display}**.")
return await build_history_embed(player, matches)