Skip to content

The agent that grows with you

fix(discord): reject a numeric application ID pasted as the bot token during setup

Tekniume151d0b3458e
diff --git a/plugins/platforms/discord/adapter.py b/plugins/platforms/discord/adapter.py
index 0d5611fd8e6..b2b309b8f26 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 203cfe99c06..31ace40c90c 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 == []