chore: prefer BOT_TOKEN env var over secrets file

Backward-compatible reorder: env var wins, then file. This lets
multiple instances on the same host (prod + shipyard staging)
each load a distinct token via systemd EnvironmentFile, instead
of fighting over the single ~/.secrets/bigbiggerbiggestbot file.

Also documents the new two-environment workflow in README.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
Danny 2026-05-10 12:43:42 +02:00
parent c0caf6cdf4
commit 967c7880fc
3 changed files with 43 additions and 22 deletions

16
bot.py
View file

@ -27,22 +27,24 @@ logging.basicConfig(
)
logger = logging.getLogger(__name__)
# Token resolution: secrets file → .env / environment variable
# Token resolution: BOT_TOKEN env var → secrets file
# Env var wins so multiple instances on the same host (e.g. prod + shipyard
# staging) can each point to a different token without sharing a secrets file.
SECRETS_FILE = os.path.expanduser("~/.secrets/bigbiggerbiggestbot")
def _load_token() -> str:
# 1. Try the secrets file
# 1. Env var (set by systemd EnvironmentFile in multi-instance setups)
token = os.environ.get("BOT_TOKEN", "").strip()
if token:
return token
# 2. Default secrets file
if os.path.isfile(SECRETS_FILE):
token = open(SECRETS_FILE).read().strip()
if token:
return token
# 2. Fall back to env var (set via .env or shell)
token = os.environ.get("BOT_TOKEN", "").strip()
if token:
return token
raise RuntimeError(
f"No bot token found. Put it in {SECRETS_FILE} or set BOT_TOKEN env var."
f"No bot token found. Set BOT_TOKEN env var or put it in {SECRETS_FILE}."
)