fix: Discord token priority — settings before state file

load_token() was checking the state file before settings.discord_token,
so a stale fake token in discord_state.json would block the real token
from .env/DISCORD_TOKEN. Flipped the priority: env config first, state
file as fallback for tokens set via /discord/setup UI.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
Trip T
2026-03-11 19:03:24 -04:00
parent 0bc4f55e1a
commit ffdfa53259
2 changed files with 19 additions and 7 deletions

View File

@@ -297,7 +297,21 @@ class DiscordVendor(ChatPlatform):
logger.error("Failed to save Discord token: %s", exc)
def load_token(self) -> str | None:
"""Load token from state file or config."""
"""Load token from config or state file.
Priority: settings.discord_token (env/.env) > state file.
The state file is a fallback for tokens set via the /discord/setup UI.
"""
# 1. Config / env var takes priority
try:
from config import settings
if settings.discord_token:
return settings.discord_token
except Exception:
pass
# 2. Fall back to state file (set via /discord/setup endpoint)
try:
if _STATE_FILE.exists():
data = json.loads(_STATE_FILE.read_text())
@@ -307,12 +321,7 @@ class DiscordVendor(ChatPlatform):
except Exception as exc:
logger.debug("Could not read discord state file: %s", exc)
try:
from config import settings
return settings.discord_token or None
except Exception:
return None
return None
# ── OAuth2 URL generation ──────────────────────────────────────────────

View File

@@ -34,11 +34,14 @@ class TestDiscordVendor:
assert status.guild_count == 0
def test_save_and_load_token(self, tmp_path, monkeypatch):
from config import settings
from integrations.chat_bridge.vendors import discord as discord_mod
from integrations.chat_bridge.vendors.discord import DiscordVendor
state_file = tmp_path / "discord_state.json"
monkeypatch.setattr(discord_mod, "_STATE_FILE", state_file)
# Settings token empty — so state file token is used as fallback
monkeypatch.setattr(settings, "discord_token", "")
vendor = DiscordVendor()
vendor.save_token("test-token-abc")