跳至主要內容

Discord

Discord 頻道讓 Mastra Agent 接收 Discord 互動及機械人訊息。Mastra 會處理 Agent 的接線及 webhook 路由;Chat SDK Discord 適配器文件則涵蓋 Discord 應用程式設定、機械人權限及 Gateway 行為。

安裝適配器
安裝適配器 的直接連結

從 Chat SDK 安裝 Discord 適配器:

npm install @chat-adapter/discord

Agent 設定
Agent 設定 的直接連結

createDiscordAdapter() 加至 Agent 的 channels.adapters 物件:

src/mastra/agents/discord-agent.ts
import { Agent } from '@mastra/core/agent'
import { createDiscordAdapter } from '@chat-adapter/discord'

export const discordAgent = new Agent({
id: 'discord-agent',
name: 'Discord Agent',
instructions: 'Answer questions and help with tasks in Discord.',
model: 'openai/gpt-5.6-sol',
channels: {
adapters: {
discord: createDiscordAdapter(),
},
},
})

在 Mastra instance 上註冊 Agent:

src/mastra/index.ts
import { Mastra } from '@mastra/core'
import { discordAgent } from './agents/discord-agent'

export const mastra = new Mastra({
agents: { discordAgent },
})

適配器設定
適配器設定 的直接連結

按照 Chat SDK Discord 適配器文件,完成 Discord 專用設定,包括建立應用程式、機械人 token、互動端點、權限及 Gateway 行為。

適配器會從環境變數讀取 Discord 認證資料,例如:

.env
DISCORD_BOT_TOKEN=your-discord-bot-token
DISCORD_PUBLIC_KEY=your-discord-application-public-key
DISCORD_APPLICATION_ID=your-discord-application-id

Webhook URL
Webhook URL 的直接連結

Mastra 會根據 Agent ID 及適配器 key 產生 Discord webhook 路由:

/api/agents/discord-agent/channels/discord/webhook

使用公開的 Mastra 伺服器 URL 作為基礎 URL:

https://your-app.example.com/api/agents/discord-agent/channels/discord/webhook

當 Discord 適配器文件要求提供互動端點或 webhook URL 時,請使用此 URL。Discord 的一般訊息事件可能需要在所產生的 webhook 路由以外設定 Gateway;請按照 Chat SDK 適配器文件完成相關設定。