@1outres/dsh-bot-discord
v0.4.1
Published
Connects each bot of DeepSeek Harness to its own Discord bot: direct messages from allowed users reach the bot's Main session, and its replies come back.
Maintainers
Readme
@1outres/dsh-bot-discord
Talk with each bot of DeepSeek Harness in Discord direct messages. Every bot gets its own Discord bot (its own token); the plugin (row bot-discord, inject = ["bot", "storageDomain"]) registers the discord channel with @1outres/dsh-bot-core, which keeps the settings and connects each configured bot (see bot-core's README, "Channels").
Behavior
Its owners field is Owners (key approvers, kept from when it named connector approvers only), and its formatHint tells the bot that Markdown renders, tables do not, and <> keeps a link from expanding. The channel declares (DISCORD_CAPABILITIES): up to 10 attachments of up to 500 MiB per message (bot-core's own limits are lower), files up to 10 MiB (a bot's upload limit outside a boosted server), turn states, the stop and status commands (and its own /pending), connector actions and credential incidents (both to the owners), and every interaction kind (question, approval, plan-review).
- DMs only. Messages in servers (anything with a
guild_id) are ignored, as are system messages (pins, calls; onlyDEFAULTandREPLYmessages count), messages from bots, from the bot itself and from users not in Allowed users. - To the Main session. An allowed user's DM goes to the bot's Main session the way DSH's Web composer sends a plain Enter (bot-core's
deliver: queued while idle; while a turn runs, queued or steered by the composer's busy-Enter preference). It passes the place (direct) and the sender: the Discord user id, the display name (the account's global name, else its username, as Discord shows it) and the username as handle, and whether the account is a bot; bot-core writes the header ([Discord · DM · Alice (@alice, owner)]). Messages are delivered one at a time, in arrival order, each at most once (a DM's messages are taken in id order). When the Gateway has to start a new session instead of resuming, the DMs heard from so far are read for the messages posted meanwhile, which are delivered before newer ones. Once the Main session accepted it, the message gets a 👀 reaction; when it was not delivered it gets ⚠️ and a reply to it saying why (too large, too many attachments, the model takes no images, the bot is not ready…). - Attachments are passed with their declared name, size and type; bot-core reads them from Discord's CDN only once it accepted them (
src/host/download.ts: HTTPScdn.discordapp.com/media.discordapp.netonly, no redirects, stopping past the limit): images become image parts as in DSH's composer, other files are saved in the bot's<workspace>/.inbox/and named in the text (bot-core's README, "Channels"). Stickers and embeds are not passed. - Replies. Every assistant message the Main session commits with text (whoever started the turn: Discord, the Web UI, a schedule) goes to the DM of every allowed user, made Discord-safe (
src/host/format.ts: Markdown tables become aligned code blocks, wide characters counted twice; headings below###become bold lines; horizontal rules a line Discord shows; code blocks are left alone) and split into messages of at most 2000 characters (at paragraph, line or word boundaries; a cut code block is closed and opened again). Reasoning and tool calls are not sent; mentions in replies never ping (allowed_mentions: { parse: [] }). Users' own messages, and other sessions of the bot, are not mirrored. - Files. A file the bot's Main session presents with
presentis uploaded to every allowed user's DM after the turn's last reply, with its description as the caption (a caption longer than one message goes first as messages of its own); when Discord refused it for everyone (too large, …), bot-core tells the Main session. - Turn notices. A failed turn ("⚠️ The reply failed: <the model's error>") and a stopped or interrupted one ("⏹️ Stopped.") are told to every allowed user's DM, in the Notice language (English or Japanese).
- Typing. While the Main session runs a turn (from its start to its end, and at connection when one is running), every allowed user's DM shows "typing", refreshed every 8 s (Discord's indicator lasts about 10 s) and again 1.5 s after a reply (so a turn ending right after its last reply shows none); it stops when the turn ends. Typing goes out in order with the replies, so none is shown after the turn's last reply.
- Questions, approvals and plan reviews (
src/host/interactions/). Each interaction of the Main session gets a card (an embed and components) in every allowed user's DM, in the Notice language, in order with the replies; a plan (and a question's detail longer than 1500 characters) is posted first as messages of its own, formatted and split like replies. Every user's card keeps that user's own draft.- A question batch shows one question per page (
n/N) with its choices listed: up to 5 single-select choices as buttons (choosing moves to the next unanswered question; a lone single-select question is sent at once), otherwise select menus of 25 (max_valuesfor multi-select), up to 4 menus (100 choices; the card says so beyond). Other… / Answer… opens a form (a modal, up to 4000 characters; on a single-select question it replaces the choice), Skip, Back, Next, Submit (asks for the first question still unanswered). A timed batch shows when the bot goes on (a Discord timestamp), Take time, and later "waiting until you answer" or "the bot went on; you can still answer"; any choice, form or Take time holds the countdown. - An approval offers Reject, Allow once (or the asker's wording) and Always allow only when the asker offers it; a subagent's request says so. A plan review offers Approve and Request changes… (a form with optional feedback).
- A custom id is
dshb:<interaction id>:<action>(positions, never labels: at most 100 characters). Presses by users not in Allowed users, or outside a DM, get a private refusal in the presser's language. A pressed card shows "Sending…" at once (Discord's three seconds), then the closure; a refused answer shows why and stays answerable. Once the interaction closes, every card is edited: who answered ("Discord", "the Web UI" or the channel's id) and what, or withdrawn, with every component disabled. A card posted before a reconnection still answers while its interaction is open; one whose interaction closed turns into "no longer waiting".
- A question batch shows one question per page (
- Connector actions. A connector action that asks first (e.g. sending an email) is posted to the DM of every user in Owners as a card: the preview's title, the connector and tool, the fields and the body as typed (fields escaped, the body in a code block a fence inside cannot close; a body longer than the embed's 4096 characters is cut and says the Web UI shows all of it), when it was requested and when it expires, with Approve and Reject (custom id
dshc:<action id>:a|r). A press goes to bot-core (link.decidewith the presser's Discord user id), which takes it only from an approver: anyone else, or a press outside a DM, gets a private refusal and the card stays. Every card then follows the action, wherever it was decided (who decided where, running, done with an excerpt of the result, failed with the reason, rejected, expired, cancelled), without buttons once it is no longer pending. Where each card sits (DM channel, message, action, approver) is stored (below), so a new connection (a restart, a reconnection, a settings change) edits the cards it posted instead of posting them again: the open actions bot-core sends again are edited in place, and the cards of actions that ended or were dropped meanwhile are brought up to date (link.connectorAction) and forgotten. A card deleted in Discord is posted again while its action is pending (the list links to it), and forgotten otherwise. A card with no record (from an earlier version) still decides and, once pressed, shows the action as it stands (or that the ledger no longer keeps it), and is followed from then on if its approver had none. - Pending approvals list. Every approver's DM holds one message listing the bot's pending actions, oldest first, one line each: the title linking to that approver's card, the connector and tool, and when it expires (
<t:…:R>), under "📋 Pending approvals of (N)" in the Notice language ("📋 の承認待ち(N 件)"); with none, "No pending approvals." ("承認待ちはありません。"). It is read from bot-core (link.pendingActions) and edited whenever it changes, after every card. Lines that do not fit the embed's 4096 characters become "…and N more: the bot's page in the Web UI shows them all." (the newest ones). It is pinned once, right after it is posted (Discord announces a pin once); when Discord refuses the pin (the DM holds the most pins allowed, no permission, any other failure) a warning is logged, the pin is not tried again for that message, and the list is still kept up to date. Unpinning it is respected. A list deleted in Discord is posted (and pinned) again at its next update or the next connection; one that could not be posted (the DM is closed) is tried again at the next update. - Approvers change. A settings change reconnects: a new approver gets the list and the cards of actions still pending; someone no longer an approver has their list deleted and their cards replaced by "This card was withdrawn…" without the preview. Records of a DM this bot can no longer reach (another token, so another Discord bot) are forgotten and posted anew in the new bot's DM.
- Credential incidents (
src/host/incidents/, docs/specs/bot-vaults.md §7.3). When the leak guard finds a secret of one of the bot's logins where the bot reads or writes, bot-core sends the incident (credential-incident), and every user in Owners gets a card in their DM, in the Notice language: a headline naming the bot (🔒 withheld, 🚨 exposed), then the login's name and kind (password, one-time password, passkey), where (the result or the arguments of a tool, by name, a reply the bot wrote, a message sent to the bot, or what bot-core handed the bot), when (<t:…:f>and<t:…:R>; for an incident that repeated before it was acknowledged, how many times in all and the last time, as of when the card is sent), and the outcome: withheld (the value was replaced before the model read it) or exposed (the model already had it), which always adds what to change now (the password; for a passkey or one-time passwords, those and the password). It ends by pointing to the Web UI (the bot's settings > Logins) to acknowledge it; there is no button in Discord (bot-core takes acknowledgements from the Web UI only). The card names only those fields of the incident (no session, grant id or anything else), names escaped. bot-core sends every unacknowledged incident again to each new connection, so each owner is shown an incident once: a record per incident and owner is stored after the card is posted. An owner whose DM refused it is tried again at the next connection, a new owner gets the open ones, and the output fails (bot-core logs it) only when no owner that still had to see it got it. The card is not edited when the incident is acknowledged (bot-core sends nothing then), and the records stay until the bot is deleted (an unacknowledged incident comes again at every connection, however old). - Stored. The storage domain
dsh_bot_discord(src/host/domain.ts, version 2 reading version 1,backup-and-skip: a record that no longer parses is set aside and its message posted anew):cards(<bot>:<action>:<user>),digests(<bot>:<user>, with the pin statedue/pinned/abandoned) andincidents(<bot>:<incident>:<user>, the card shown to an owner). Onlysrc/host/actions/records.tsandsrc/host/incidents/records.tswrite them (design rule 72). Records of deleted bots are dropped when the plugin starts; a card's record goes once its action ended and the card shows it.test/domain-schema.test.tssnapshots the schema: a change that breaks stored records needs a version bump. - Slash commands.
/stop(stop the current turn, as the Web UI's stop button; queued messages stay),/status(busy or idle, the model, the queued messages) and/pendingare registered as global commands usable in the bot's DMs only (contexts: [BotDM]), on every new Gateway session, with bot-core's descriptions (English, Japanese localization). Users not in Allowed users, and any use outside a DM, get a private (ephemeral) refusal and nothing runs./pendingis this channel's own command (src/host/actions/command.ts), for the Owners instead of the allowed users: it answers privately with the same list as the pinned message, linking to the invoker's cards; bot-core checks the invoker is an approver (link.pendingActions), and anyone else gets a refusal that shows nothing of the actions (a preview can hold personal data). Answers are in the invoker's Discord language (Japanese or English). - Connection. One discord.js client per bot (intent
DirectMessagesonly, partialChannelso DMs arrive from channels not cached yet; a DM's content needs no privileged intent; interactions need no intent). discord.js keeps the Gateway session (heartbeats and zombie detection, resume, identify again, reconnect) and REST (rate limits, retries of 5xx and network errors). A rejected token (at login, or close 4004 later) or another unrecoverable close stops the bot for good and shows "Failed" in the dialog; a login that cannot reach Discord is retried with a new client after 1 s, doubling to 60 s. The dialog shows "Connected as ", also after a resume. A client destroyed while its login is still underway opens no Gateway connection afterwards. The token never appears in logs.
Settings (bot menu > Channels > Discord)
| Field | What |
| --------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| Bot token | Secret, required. Stored in DSH's credential store; the browser only learns whether it is set. |
| Allowed users | Required, 1–25 Discord user IDs (17–20 digits), one per line. Only their DMs reach the bot; replies go to each of them. |
| Owners | Optional, up to 10 Discord user IDs (key approvers): the bot's owners, who can give it instructions and approve its connector actions. Their messages are marked owner in the bot's header (they also need to be Allowed users to talk with it); connector actions and credential incidents are posted to their DMs, and only they can decide connector actions in Discord. Empty: nobody on Discord is treated as the owner (decide in the Web UI). |
| Notice language | Optional, picked from Default (English), English or 日本語 (stored "", en, ja): the bot's own notices in DMs. Command answers follow each user's Discord language. |
Discord setup
- Discord Developer Portal > New Application (one per DSH bot).
- Bot > Reset Token, copy it into the bot's Discord settings. No privileged Gateway intent is needed (Message Content is not needed for DMs).
- Installation (or OAuth2 > URL Generator): scope
bot, invite it to a server you share with each allowed user. Discord only lets a user and a bot exchange DMs when they share a server (or the app is user-installed). - In Discord, Settings > Advanced > Developer Mode, then right-click each user > Copy User ID into Allowed users.
- In DSH, the bot's menu > Channels > Discord: set the token, paste the user IDs, and turn Discord on (each setting is saved as you change it). The state turns to "Connected" with the bot's name under Discord. DM the bot.
Structure
| Module | What |
| ---------------------------------------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| src/host/discord.ts | The contract the logic uses: plain message data, client events (ready for a new Gateway session, resumed, reconnecting, message, command, failed) and actions (openDm, send with files and a reply target, react, typing, messagesAfter, registerCommands). |
| src/host/discordjs/ | The discord.js adapter, the only code importing discord.js (design rule 33): client options and sharding (options.ts, sharding.ts), mapping to the contract (messages.ts, commands.ts), failures (failures.ts), events and actions (client.ts). |
| src/host/connection.ts | Keeps a bot logged in: reports the state, replaces a client that failed retryably. |
| src/host/bridge.ts | DMs to the Main session and bot-core's outputs back: ordering, catch-up after a new session, reactions and failure replies, files, turn notices, typing, slash commands. |
| src/host/interactions/ | Interaction cards: custom ids (custom-id.ts), each user's draft (draft.ts), the cards, pure (render.ts, texts.ts), and presses and forms answered through bot-core (presenter.ts). |
| src/host/actions/ | Connector action cards and the pending approvals list: custom ids, the card and the list, pure (render.ts, digest.ts, texts.ts), the stored records (domain.ts, records.ts), each approver's list (digests.ts), cards, presses and /pending (presenter.ts, command.ts). |
| src/host/incidents/ | Credential incident cards: the card, pure (render.ts, texts.ts), which owner was shown which incident (domain.ts, records.ts), posting each once per owner (presenter.ts). |
| src/host/domain.ts | The storage domain dsh_bot_discord and its tables. |
| inbound.ts, download.ts, format.ts, texts.ts, cursor.ts, directory.ts, split.ts, typing.ts, queue.ts | Who may talk and what a DM brings, reading attachments, Discord-safe Markdown, the bot's own texts (en/ja), the newest message taken per DM, each user's DM channel, splitting at 2000, the typing generation, serial queues. |
Everything outside src/host/discordjs/ is library-independent and tested with fakes of the contract; the adapter is tested against a real discord.js Client with REST and login stubbed. A new Discord feature adds its events and actions to discord.ts (buttons, select menus, modals and embeds are there: component, modal, send returning the message id, edit and remove telling a message that is gone, pin telling why Discord refused) and implements them in the adapter (discordjs/components.ts). Neither half touches the Host's files or processes (design rule 35); the composition root (src/index.ts) wires the timers, fetch and the storage domain.
Not supported: tool calls or reasoning as they happen, progress, edits and deletions in Discord, sessions other than Main, servers, threads and group DMs, sign-in flows, changing settings from Discord, stickers, polls and voice. A card's draft lives in the connection: after a reconnection of the plugin (a settings change, a restart) open interactions are posted again.
discord.js is a dependency of this package (^14): installing the plugin or the @1outres/dsh-bots bundle installs it into the profile next to the plugin, while builds keep it external (test/packages-bundle.test.ts checks that lib/ holds only this package's code and imports only declared packages). discord.js and its dependencies run no install scripts; its optional native accelerators (zlib-sync, bufferutil, utf-8-validate) are not needed and not installed.
Requirements
@1outres/dsh-bot-core with DSH's credential store (ctx.credentials) and storage domains (ctx.storageDomain), both part of the default composition, and Node.js 22 or newer. Install a packed tarball together with bot-core, as for @1outres/dsh-bot-vm; pnpm installs discord.js with it.
