# Discord

URL: https://swb.one/zh-Hant/docs/setup/discord

SwB 以機器人身分登入 Discord，並經由 Discord Gateway 接收新訊息。Gateway 是一條由 SwB 主動向 Discord 建立的連線。

## 開始之前 [#開始之前]

準備一個 Discord 帳號，它在要連接的伺服器上要有 **Manage Server** 權限。以擁有者身分登入 SwB。

## 複製設定提示詞 [#複製設定提示詞]

https://swb.one/prompts/discord.md

```
Read the setup guide at https://swb.one/docs/setup/discord.

Help me connect Discord to SwB. SwB carries messages between my agents and channels.

If the SwB tools are missing, ask me to add SwB as a custom MCP server. Follow https://swb.one/docs/connect to add the client.

For Claude, give me the path `Customize > Connectors > Add > Custom > Web`. For ChatGPT, ask me to open `https://chatgpt.com/plugins`. Give me the path `+ > Add custom MCP server`. For Grok, give me the path `grok.com > Connectors > New Connector > Custom`. For other apps, ask me to open the MCP server settings. Wait for me to confirm.

Use a SwB client with owner access.

Call `connect(what="discord")`. Do each step marked "you" yourself. Give me each step marked "person" one at a time with the exact click path and links. Wait for me to say done after each step.

Ask me to create an app at https://discord.com/developers/applications. Under **Bot**, ask me to generate its token. Ask me to turn on **Message Content Intent**. Ask me to enter the token only through the claim link from `connect`.

Add a space under `blocks.discord.spaces` in `config/switchboard.yaml`. Set `guild_id` to my Discord server ID and `owner_ids` to my user IDs. Grant the test agent access to the space in `blocks.discord.agents`. If the file needs a manual edit, give me the YAML. Restart SwB after an edit.

Read the invite link from `setup.invite_url` in `discord_status`. If the link is missing, set `blocks.discord.application_id` or wait for the bot to connect. Ask me to open the link and add the bot to my server. Call `connect` again to check the connection.

If Discord reports code `4014`, check **Message Content Intent**. If it reports code `4004`, replace the rejected token. When `connect` reports `connected`, ask me to choose a test channel. Call `discord_post` with the space label, channel ID and test text. Show me the result.

Subscribe to switchboard.message.created if this app supports MCP events. Otherwise, call wait or inbox to collect new messages.
```

把提示詞貼到你的 SwB 擁有者用戶端。

## 設定 Discord 機器人 [#設定-discord-機器人]

### 建立應用程式 [#建立應用程式]

開啟 https://discord.com/developers/applications 並按一下 **New Application**。
### 為應用程式命名 [#為應用程式命名]

輸入名稱，再按一下 **Create**。
### 重設 Bot 權杖 [#重設-bot-權杖]

按一下 **Bot** → **Reset Token**，並確認重設。

### 複製 Bot 權杖 [#複製-bot-權杖]

趁 Discord 還顯示權杖時按一下 **Copy**。Discord 只顯示這一次。
### 啟用 Message Content Intent [#啟用-message-content-intent]

在 **Bot** 頁面的 **Privileged Gateway Intents** 下啟用 **Message Content Intent**，再按一下 **Save Changes**。

### 儲存權杖 [#儲存權杖]

開啟代理提供的 SwB 認領連結，把權杖貼到欄位中。
### 邀請機器人 [#邀請機器人]

開啟代理提供的邀請連結。如果 Discord 詢問，選取你的伺服器，再按一下 **Authorize**。

## 代理會做什麼 [#代理會做什麼]

代理呼叫 `connect(what="discord")`，必要時會啟用 `discord` 模組。模組啟用後，`connect` 或 `discord_provision` 會回傳 Bot 權杖的認領連結。

你儲存權杖後，代理呼叫 `discord_status`，檢查機器人、Gateway 連線和伺服器，再把 `setup.invite_url` 中的邀請連結交給你。

代理在 `config/switchboard.yaml` 的 `blocks.discord.spaces` 下設定伺服器。空間（space）把一個伺服器對應到一個 SwB 專案。代理會設定 `guild_id` 和 `owner_ids`。如果要讓某個代理呼叫 `discord_post`，在 `blocks.discord.agents` 下授予它存取權。編輯檔案後，重新啟動 SwB。

## 憑證 [#憑證]

| 權杖 | Discord 顯示的位置 | 格式 | 去處 |
| --- | --- | --- | --- |
| Bot 權杖 | **Bot** → **Reset Token**，只顯示一次 | 三段字串，中間以句點分隔 | SwB 認領連結 |
| 應用程式 ID | **General Information** → **Application ID** | 一串數字 | `blocks.discord.application_id`，或由 SwB 自行找出 |

## 檢查連線 [#檢查連線]

請代理再次呼叫 `connect`。設定完成時，它會回報 `connected`。`discord_status` 會顯示 Gateway 已就緒，而且機器人已在你的伺服器中。

使用列在 `owner_ids` 中的 Discord 帳號，在頻道中提及機器人，並加上 `targets` 中的目標名稱和一則測試訊息。確認同一個頻道中出現回覆。

## 疑難排解 [#疑難排解]

如果出現 Gateway 代碼 `4014`，啟用 **Message Content Intent**。如果要以提及作為觸發方式，設定 `message_content: false`，並在每則訊息中提及機器人。

如果出現 Gateway 代碼 `4004`，重設 Discord Bot 權杖。如果權杖來自伺服器環境，在那裡更換權杖，再重新啟動 SwB。其他情況下，使用新的認領連結。

如果機器人已在伺服器中，卻在某個頻道保持沉默，檢查這個頻道的權限。
