Zum Inhalt springen
Plugins

platform_delivery Plugin

Discord

The `discord` plugin (`plugins/discord/`) adds Discord as a chat-delivery platform: it links a Discord account to a Syrus user, listens for direct messages over Discord's Gateway, routes them into the same chat pipeline as the web UI, and delivers assistant replies back as Discord DMs. It is a self-contained Rails engine plugin, installed but disabled by default (`default_enabled: false`, `disableable: true`, category `platform_delivery`). It behaves as another chat surface, not a separate automation engine — there is no Discord-specific Job/Workflow/Epic model.

discordQuellcode

Discord

The discord plugin (plugins/discord/) adds Discord as a chat-delivery platform: it links a Discord account to a Syrus user, listens for direct messages over Discord's Gateway, routes them into the same chat pipeline as the web UI, and delivers assistant replies back as Discord DMs. It is a self-contained Rails engine plugin, installed but disabled by default (default_enabled: false, disableable: true, category platform_delivery). It behaves as another chat surface, not a separate automation engine — there is no Discord-specific Job/Workflow/Epic model.

Configuration

No config_schema. The bot token is an instance-owned connected account (provider "Discord bot"), managed by administrators on Admin → Connected accounts; Admin → Settings → Discord links there. To set or rotate it, paste the token from the Discord developer portal: Syrus checks it with a harmless authenticated call (GET /users/@me) before storing it, encrypted, and never shows it again. The account's label defaults to the bot's username.

The plugin reaches the token only through the Connected accounts broker, as a named platform service with the instance-owned account in service mode, and every use is audited with its purpose. REST calls take the token for the duration of one call. The Gateway connection is long-lived: it holds the token in memory for the lifetime of the connection only (it is sent in the Gateway's IDENTIFY/RESUME frames and dropped when the connection ends) and never writes it to logs, the environment, command lines or files. Each Gateway heartbeat (about every 40 seconds) checks that the account is still connected with the same credential. On rotation the session is closed and the connector reconnects immediately with the new token; on disconnect the session is closed and the connector stops. Discord has no token revocation call, so disconnecting does not invalidate the token at Discord: reset it in the developer portal when needed.

Upgrading: an existing token from the old application setting is copied into a connected account on first use, verified with a live call, and only then erased from the old setting. If the copy or verification fails, the old value stays in place and keeps working, and the copy is retried every ten minutes. Once a connected account exists the old setting is never read again.

Discord::PlatformConfig#configured? gates the account-linking instructions shown under Settings → Connected Platforms on the account being connected.

Account linking

Linking uses the same signed-token flow as other platform-delivery plugins: the web UI issues a linking token (Rails.application.message_verifier (:platform_linking)), and Discord::PlatformConfig#instructions tells the user to DM /link <token> to the Syrus bot. Discord::GatewayConnectionJob #handle_linking verifies the token, resolves the User, and creates/updates a PlatformIdentity (platform: "discord", keyed on the Discord user's numeric external_id) with linked_at stamped — then confirms over DM. An invalid or expired token gets a DM telling the user to generate a new one.

Inbound: Gateway connection

Discord::GatewayClient is a from-scratch Gateway v10 WebSocket client (raw TCPSocket/OpenSSL::SSL::SSLSocket plus the websocket-driver gem for framing — no Discord SDK dependency): it IDENTIFYs with the DIRECT_MESSAGES | MESSAGE_CONTENT intents (the minimum needed to read DM text), answers Gateway heartbeats on the server-provided interval, and RESUMEs a dropped session in place up to 5 attempts with linear backoff before giving up and letting the caller reconnect from scratch. This is a bot-initiated *outbound* connection — Syrus never opens an inbound HTTPS endpoint for Discord, consistent with the rest of Syrus's no-inbound-webhook design.

Discord::GatewayConnectionJob (a PlatformPollingJob subclass, registered as the plugin's platform_delivery connector_job_class) owns exactly one Gateway session per #poll_once call; when GatewayClient#run returns (session exhausted or connection lost), the base class re-enqueues a fresh job after the connector's reconnect backoff, which opens a new connection -- the same self-healing pattern used for reconnection across worker restarts/deploys. PlatformDelivery::Registry.start_connectors! starts/stops this job in step with the plugin's enabled state (like the Telegram plugin's poller).

For each MESSAGE_CREATE dispatch, the job:

  • ignores anything that isn't a DM (guild_id present means a guild channel)

and ignores bot-authored messages (including its own sends);

  • treats a /link <token> message as account linking (see above);
  • otherwise routes the text through InboundMessageRouter (`platform:

"discord"`, keyed by the author's Discord user id/username) into the linked user's chat session; an unlinked sender gets a DM pointing them at Settings → Connected Platforms.

Outbound: reply delivery

Discord::PlatformAdapter (registered as the plugin's platform_delivery provider, platform_key: "discord") implements the outbound half: given a chat message and the recipient's PlatformIdentity, it extracts the text content and calls Discord::Client#send_dm, which opens/reuses a DM channel via POST /users/@me/channels and posts through POST /channels/:id/messages using bot-token (Authorization: Bot <token>) REST calls. Long replies are split on the last newline before Discord's 2000 character message cap (DISCORD_MAX_CHARS) rather than truncated, mirroring the same chunking Telegram::PlatformAdapter does for Telegram's 4096-character cap. Delivery errors are logged and swallowed — a failed Discord DM never fails the chat turn that produced the reply.

Admin/sidebar pages

No dedicated Discord admin page. Two existing surfaces cover it instead: account linking uses the generic Settings → Connected Platforms page (PlatformIdentity::PlatformConfig::Base.for dispatches to Discord::PlatformConfig once the plugin registers itself), and bot-token setup uses a Discord-specific section on the core Admin → Settings page (AdminSettings.tsx's DiscordSection, next to Telegram's) with a token field and a Start polling button that re-primes Discord::GatewayConnectionJob via the shared POST /api/v1/app/admin/platform_polling/start endpoint (see config/syrus_docs/external_platforms.md) -- it reads only the discord entry out of that call's connectors array, so it can't be confused with Telegram's own button and status line right above it.

MCP tools

None.

Operational notes

The Discord listener is in-process Ruby running inside a Solid Queue worker pod, not an external daemon: Discord::GatewayConnectionJob holds the live Gateway WebSocket for as long as one #poll_once call lasts, and there is no separate bot process to restart. This plugin depends on external Discord connectivity, a live Gateway session, and a valid bot token — monitor it like any other long-lived platform integration. A disconnected bot account shows up as Discord::PlatformConfig#configured? returning false (linking instructions disappear and the connector stops). Startup connection failures and exhausted reconnect runs are logged on each reconnect attempt and re-enqueued with a growing backoff. Discord Gateway fatal closes such as bad authentication or invalid/disallowed intents are logged once and stop that job's self-reenqueue loop; a manual restart or the connector watchdog may re-prime it later, but the fatal condition will stop each attempt until an operator fixes the configuration.

Because the job re-enqueues itself in an ensure block after each Gateway session ends, it normally survives worker restarts and reconnects on its own. That self-healing breaks if the Solid Queue process running it is pruned or killed before it reaches ensure (SolidQueue::Processes:: ProcessPrunedError, an OOMKilled worker, a deploy's SIGKILL) — the job simply vanishes, and the listener goes silently deaf with no error anywhere and no PlatformIdentity/chat activity from /link DMs. Two things recover from that without a deploy:

  • PlatformConnectorWatchdogJob (config/recurring.yml's

watch_platform_connectors, every 5 minutes) re-primes any configured connector, including this one, that is missing from the queue, and logs a [PlatformConnectorWatchdogJob] warning when it does.

  • An operator can re-prime immediately via `POST

/api/v1/app/admin/platform_polling/start (admin-only), which starts both core pollers and plugin connectors like this one and reports each connector's started/already_running/not_configured` status.

Both rely on Discord::GatewayConnectionJob's inherited dedup check (an unfinished SolidQueue::Job row for the class), so re-priming an already healthy listener is a no-op rather than a second Gateway session. See config/syrus_docs/external_platforms.md for the shared connector-recovery design across all platform-delivery connectors.