Skip to content

fix(discord-bridge): check DISCORD_BOT_TOKEN env var before .env file - #1050

Merged
qingyun-wu merged 2 commits into
sonichi:mainfrom
amkunte:fix/discord-bridge-token-env-var-first
May 23, 2026
Merged

qingyun-wu merged 2 commits into
sonichi:mainfrom
amkunte:fix/discord-bridge-token-env-var-first

Conversation

@amkunte

@amkunte amkunte commented May 23, 2026

Copy link
Copy Markdown
Contributor

Problem

discord-bridge.py reads DISCORD_BOT_TOKEN exclusively from ~/.claude/channels/discord/.env at module-load time. There is no fallback to os.environ. This causes test suites that inject a stub token via os.environ.setdefault("DISCORD_BOT_TOKEN", "test-token-not-real") to fail with:

DISCORD_BOT_TOKEN not set in ~/.claude/channels/discord/.env

Affects VasiliyRad PRs #1046 (bridges-sending-orphan-recovery.test.py) and #1048 (discord-bridge-delivery-sentinel.test.py) — both set the env var before import but the bridge never checked it.

Fix

Check os.environ.get("DISCORD_BOT_TOKEN") first; fall back to the .env file. This is the same pattern already used by telegram-bridge.py (lines 102–104).

Impact

🤖 Generated with Claude Code

Matches telegram-bridge.py pattern. Allows test suites to inject a
stub token via os.environ without needing a real ~/.claude/channels/
discord/.env file — unblocks CI for VasiliyRad PRs sonichi#1046 and sonichi#1048
whose tests already set the env var before importing the bridge.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

@qingyun-wu qingyun-wu left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM — this is the right, minimal fix and correctly diagnosed. Env-var-takes-precedence with .env fallback is the standard "env overrides config file" precedence, the exit(1) when neither is set is preserved, and it's appropriately surgical (8/-7, one file). CI is green.

This is effectively the keystone for the restart-safety series: it unblocks #1046's CI directly. One nuance worth flagging — it's necessary but not sufficient for #1048: that test imports the bridge without stubbing the discord module (whereas #1046 does), so after this lands #1048 will likely still fail at import discord until it adds the stub. (Raised that on #1048.)

Two minor, non-blocking nits:

  1. Strip consistency. The .env path does .split("=", 1)[1].strip(), but the new env-var path doesn't strip — os.environ.get("DISCORD_BOT_TOKEN", ""). A stray trailing newline in the env var would slip through. Consider .strip() for parity: os.environ.get("DISCORD_BOT_TOKEN", "").strip().
  2. Error message. Now that the env var is also consulted, the failure message ("DISCORD_BOT_TOKEN not set in ~/.claude/channels/discord/.env") is slightly misleading — it doesn't mention the env var that was also checked. e.g. "DISCORD_BOT_TOKEN not set (checked env var and ~/.claude/channels/discord/.env)" would aid debugging.

Neither blocks. Nice clean fix.

@qingyun-wu
qingyun-wu merged commit 0ade60d into sonichi:main May 23, 2026
2 checks passed
liususan091219 pushed a commit to liususan091219/sutando-fork that referenced this pull request May 28, 2026
…sonichi#1050)

Matches telegram-bridge.py pattern. Allows test suites to inject a
stub token via os.environ without needing a real ~/.claude/channels/
discord/.env file — unblocks CI for VasiliyRad PRs sonichi#1046 and sonichi#1048
whose tests already set the env var before importing the bridge.

Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
Co-authored-by: Qingyun Wu <qingyun0327@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants