fix(discord_tool): key capability cache by token instead of single global

_capability_cache was a single module-level dict shared across all
tokens. If the bot token rotates or multiple tokens are used in one
process, capabilities detected for token A would be returned for
token B, causing wrong schema gating and incorrect runtime behavior.

Replace the single Optional cache with a Dict keyed by token so each
token gets its own isolated capability entry.
This commit is contained in:
sprmn24
2026-04-27 22:37:15 +03:00
committed by Teknium
parent 82b5786721
commit fa7b0b0a67

View File

@ -132,7 +132,7 @@ def _channel_type_name(type_id: int) -> str:
# ---------------------------------------------------------------------------
# Module-level cache so the app/me endpoint is hit at most once per process.
_capability_cache: Optional[Dict[str, Any]] = None
_capability_cache: Dict[str, Dict[str, Any]] = {}
def _detect_capabilities(token: str, *, force: bool = False) -> Dict[str, Any]:
@ -148,8 +148,8 @@ def _detect_capabilities(token: str, *, force: bool = False) -> Dict[str, Any]:
Cached in a module-global. Pass ``force=True`` to re-fetch.
"""
global _capability_cache
if _capability_cache is not None and not force:
return _capability_cache
if token in _capability_cache and not force:
return _capability_cache[token]
caps: Dict[str, Any] = {
"has_members_intent": True,
@ -172,14 +172,14 @@ def _detect_capabilities(token: str, *, force: bool = False) -> Dict[str, Any]:
"Discord capability detection failed (%s); exposing all actions.", exc,
)
_capability_cache = caps
_capability_cache[token] = caps
return caps
def _reset_capability_cache() -> None:
"""Test hook: clear the detection cache."""
global _capability_cache
_capability_cache = None
_capability_cache = {}
# ---------------------------------------------------------------------------