跳到主要内容

Discord

Discord Channels 让 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 实例上注册该 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 和适配器键生成 Discord webhook 路由:

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

使用公开的 Mastra Server URL 作为基础 URL:

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

在 Discord 适配器文档要求填写交互端点或 webhook URL 的任何位置使用此 URL。Discord 的常规消息事件可能需要在生成的 webhook 路由之外设置 Gateway;请按照 Chat SDK 适配器文档完成该流程。