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:
@ -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 = {}
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
Reference in New Issue
Block a user