diff --git a/engine/discord.py b/engine/discord.py index 99f1918..fd39822 100644 --- a/engine/discord.py +++ b/engine/discord.py @@ -15,7 +15,16 @@ ENV = ROOT / ".env" # The operator-managed bot credential is shared by TRON worktrees. Keep the # engine-local file first for standalone installs and tests. -MODES_ENV = ROOT.parents[2] / "tron-modes" / ".env" +def _modes_env() -> Path: + """Find the shared modes env from either a checkout or an added worktree.""" + for parent in ROOT.parents: + candidate = parent / "tron-modes" / ".env" + if candidate.exists(): + return candidate + return ROOT.parents[2] / "tron-modes" / ".env" + + +MODES_ENV = _modes_env() CHANNEL_ID = "1547918251651112971" MESSAGE_URL = f"https://discord.com/api/v10/channels/{CHANNEL_ID}/messages" _state = {"loaded": False, "token": None, "after": None} @@ -38,10 +47,14 @@ def _load(): def _request(url, data=None, timeout=10): + headers = { + "Authorization": f"Bot {_state['token']}", + "User-Agent": "42labs-tron/1.0", + } + if data is not None: + headers["Content-Type"] = "application/json" request = urllib.request.Request( - url, data=data, - headers={"Authorization": f"Bot {_state['token']}", - "Content-Type": "application/json"}) + url, data=data, headers=headers) with urllib.request.urlopen(request, timeout=timeout) as response: return json.loads(response.read().decode())