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

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.
This commit is contained in:
Teknium
2026-09-09 02:41:33 -07:00
parent 0f3199bd65
commit e151d0b345
2 changed files with 63 additions and 1 deletions
+33 -1
View File
@@ -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)
@@ -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 == []