From e151d0b3458e136729fe498b566deb795ffb6a42 Mon Sep 17 00:00:00 2001 From: Teknium <127238744+teknium1@users.noreply.github.com> Date: Wed, 9 Sep 2026 02:41:33 -0700 Subject: [PATCH] fix(discord): reject a numeric application ID pasted as the bot token during setup MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Port from openclaw/openclaw#140531: users paste the application ID from the Developer Portal's General Information page instead of the bot token (Bot page); the gateway then fails at runtime with an opaque 401. A real bot token is dot-separated base64 and never purely numeric, so the setup wizard now rejects an all-digit answer with pointed guidance and re-prompts once. A second consecutive numeric answer is kept (user override), and non-numeric tokens are saved exactly as before. Adapted to hermes: the guard lives in the Discord plugin's interactive_setup (the active setup path per the #9983 review — legacy _setup_standard_platform is not the dispatch target), mirroring the existing Telegram token-shape validation in hermes_cli/setup_platforms.py. --- plugins/platforms/discord/adapter.py | 34 +++++++++++++++++++++- tests/gateway/test_discord_plugin_setup.py | 30 +++++++++++++++++++ 2 files changed, 63 insertions(+), 1 deletion(-) diff --git a/plugins/platforms/discord/adapter.py b/plugins/platforms/discord/adapter.py index 0d5611fd8e..b2b309b8f2 100644 --- a/plugins/platforms/discord/adapter.py +++ b/plugins/platforms/discord/adapter.py @@ -6861,6 +6861,38 @@ def _clean_discord_user_ids(raw: str) -> list: return cleaned +def _discord_token_shape_error(token: str) -> Optional[str]: + """Reject a Discord bot token that is really the numeric application ID. + + Users routinely paste the application ID from the Developer Portal's General + Information page instead of the bot token (Bot page); the gateway then fails + at runtime with an opaque 401. A real bot token is dot-separated base64 and + never purely numeric, so this is a safe, narrow shape check (port of + openclaw/openclaw#140531). + """ + if token and token.strip().isdigit(): + return ("That looks like a numeric application ID, not a bot token. " + "Paste the bot token from the Discord Developer Portal (Bot page), " + "not the application ID (General Information page).") + return None + + +def _prompt_discord_bot_token(prompt) -> str: + """Prompt for the bot token, re-prompting once when the answer is a numeric app ID.""" + from hermes_cli.cli_output import print_error + token = "" + for _attempt in range(2): + token = prompt("Discord bot token", password=True) + if not token: + return "" + error = _discord_token_shape_error(token) + if error is None: + return token + print_error(error) + # Second consecutive numeric answer: trust the user, keep the value. + return token + + def interactive_setup() -> None: """Guide the user through Discord bot setup: token, allowlist, home channel (lazy CLI imports).""" from hermes_cli.config import get_env_value, remove_env_value, save_env_value @@ -6900,7 +6932,7 @@ def interactive_setup() -> None: "Save Changes in the Developer Portal before starting the gateway.", "Docs: https://hermes-agent.nousresearch.com/docs/user-guide/messaging/discord", ) - token = prompt("Discord bot token", password=True) + token = _prompt_discord_bot_token(prompt) if not token: return save_env_value("DISCORD_BOT_TOKEN", token) diff --git a/tests/gateway/test_discord_plugin_setup.py b/tests/gateway/test_discord_plugin_setup.py index 203cfe99c0..31ace40c90 100644 --- a/tests/gateway/test_discord_plugin_setup.py +++ b/tests/gateway/test_discord_plugin_setup.py @@ -78,3 +78,33 @@ class TestDiscordSetupPrivilegedIntentsGuidance: assert "discord.com/developers/applications" in joined + + +class TestDiscordTokenShapeGuard: + """A numeric application ID pasted as the bot token is rejected with guidance + (port of openclaw/openclaw#140531).""" + + def test_numeric_app_id_reprompts_then_accepts_real_token(self, monkeypatch, tmp_path): + monkeypatch.setenv("HERMES_HOME", str(tmp_path)) + saved, removed, errors = {}, [], [] + real_token = "«redacted»." + "part2.part3" + _patch_setup_io( + monkeypatch, + ["1234567890123456789", real_token, "", ""], + saved, + removed, + existing={}, + ) + monkeypatch.setattr(cli_output_mod, "print_error", lambda *a, **_kw: errors.append(" ".join(map(str, a)))) + interactive_setup() + assert saved.get("DISCORD_BOT_TOKEN") == real_token + assert any("application ID" in e for e in errors) + + def test_non_numeric_token_saves_without_error(self, monkeypatch, tmp_path): + monkeypatch.setenv("HERMES_HOME", str(tmp_path)) + saved, removed, errors = {}, [], [] + _patch_setup_io(monkeypatch, _PROMPTS_BLANK, saved, removed, existing={}) + monkeypatch.setattr(cli_output_mod, "print_error", lambda *a, **_kw: errors.append(" ".join(map(str, a)))) + interactive_setup() + assert saved.get("DISCORD_BOT_TOKEN") == _PROMPTS_BLANK[0] + assert errors == []