Files
nanobot/nanobot/channels/discord/validation.py
T
chengyongru e8a29a0087 feat(webui): improve setup flows across chat channels (NAN-112) (#5356)
* feat(webui): improve channel setup flows

* fix(webui): refine channel setup interactions (NAN-112)

* fix(webui): keep email setup sections channel-owned

* fix(webui): remove concrete channel details from shared components

* refactor(webui): remove unused channel setup resources

* fix(webui): finish channel-owned setup boundaries

* fix(webui): update channel switches optimistically

* test(channels): remove obsolete absence assertions

* fix(webui): flatten channel instance settings disclosure

* fix(webui): complete channel setup and connection flows

* fix(whatsapp): wait for session database release

* fix(webui): preserve scrolling channel controls

* fix(webui): stage channel connection checks

* fix(channels): align verification actions

* fix(email): expose connection checks

* fix(webui): align channel field actions

* fix(email): verify account credentials

* fix(wecom): bypass system proxy for websocket

* fix(channels): stabilize async connection cleanup

* fix(webui): autosave channel credentials

* fix(email): make provider presets non-destructive

* test(weixin): isolate persisted QR credentials
2026-09-11 15:22:14 +08:00

72 lines
2.3 KiB
Python

"""Discord setup validation owned by the channel package."""
from typing import Any
import httpx
from nanobot.channels.contracts import ChannelValidationContext
from nanobot.channels.validation import (
check,
http_get,
payload,
required_checks,
status_from_checks,
string_value,
)
def validate(values: dict[str, Any], _context: ChannelValidationContext) -> dict[str, Any]:
checks, missing = required_checks("discord", values)
token = string_value(values.get("token"))
proxy = string_value(values.get("proxy"))
if token:
try:
data = http_get(
"https://discord.com/api/v10/users/@me",
headers={"Authorization": f"Bot {token}"},
proxy=proxy or None,
)
bot_id = str(data.get("id") or "")
checks.append(check("bot_token", "Bot token", "pass", "Discord accepted the bot token."))
identity = {
"name": data.get("global_name") or data.get("username"),
"account": bot_id,
}
if bot_id:
checks.append(
check(
"invite",
"Server invite",
"pass",
"Use this generated OAuth URL to invite the bot.",
action_url=(
"https://discord.com/oauth2/authorize"
f"?client_id={bot_id}&scope=bot%20applications.commands"
),
)
)
return payload(
"discord",
"connected",
checks,
identity=identity,
missing_fields=missing,
)
except httpx.HTTPStatusError as exc:
checks.append(
check(
"bot_token",
"Bot token",
"fail",
f"Discord rejected the token: HTTP {exc.response.status_code}",
)
)
except Exception as exc:
checks.append(
check("bot_token", "Bot token", "warn", f"Could not reach Discord now: {exc}")
)
return status_from_checks("discord", checks, missing)
__all__ = ["validate"]