Repository navigation
Expand file tree
/
Copy pathservices.py
More file actions
82 lines (61 loc) · 2.97 KB
/
Copy pathservices.py
File metadata and controls
82 lines (61 loc) · 2.97 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
70
71
72
73
74
75
76
77
78
79
80
81
82
"""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 i18n import DEFAULT_LANGUAGE, t
from models import PlayerRef
from riot_api import RiotAPI, RiotAPIError
logger = logging.getLogger("botdiff.services")
PROFILE_MATCH_COUNT = 10
# Âge maximal des données Riot réutilisées par les commandes (la boucle de suivi les rafraîchit toutes les 2 min)
COMMAND_CACHE_S = 150
HISTORY_MATCH_COUNT = 5
Message = tuple[list[discord.Embed], list[discord.File], discord.ui.View]
class UserFacingError(Exception):
"""Erreur montrée à l'utilisateur : clé de message (voir i18n) et ses paramètres."""
def __init__(self, key: str, **params: object) -> None:
super().__init__(key)
self.key = key
self.params = params
def message(self, lang: str) -> str:
return t(lang, self.key, **self.params)
def user_error_message(exc: BaseException, lang: str = DEFAULT_LANGUAGE) -> str:
"""Message d'erreur lisible pour Discord (sans détail technique)."""
if isinstance(exc, UserFacingError):
return f"❌ {exc.message(lang)}"
if isinstance(exc, RiotAPIError):
if exc.status == 404:
return t(lang, "error_not_found")
if exc.status in (401, 403):
return t(lang, "error_api_key")
if exc.status == 429:
return t(lang, "error_rate_limit")
if exc.status == 0:
return t(lang, "error_no_response")
return t(lang, "error_unavailable")
return t(lang, "error_internal")
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, lang: str = DEFAULT_LANGUAGE
) -> Message:
summoner, league_entries, matches = await asyncio.gather(
riot.summoner(platform, player.puuid, COMMAND_CACHE_S),
riot.league_entries(platform, player.puuid, COMMAND_CACHE_S),
riot.get_recent_matches(player.puuid, PROFILE_MATCH_COUNT, COMMAND_CACHE_S),
)
embed, files, view = await build_profile_embed(player, summoner, league_entries, matches, lang)
return [embed], files, view
async def load_history(riot: RiotAPI, player: PlayerRef, lang: str = DEFAULT_LANGUAGE) -> Message:
matches = await riot.get_recent_matches(player.puuid, HISTORY_MATCH_COUNT, COMMAND_CACHE_S)
if not matches:
raise UserFacingError("no_recent_games", player=player.display)
return await build_history_embed(player, matches, lang)