跳到主要内容
非官方简体中文翻译,内容版权归 Mastra 所有,遵循 Apache-2.0 许可。

Discord

Discord channels let a Mastra agent receive Discord interactions and bot messages. Mastra handles the agent wiring and webhook route; the Chat SDK Discord adapter docs cover Discord application setup, bot permissions, and Gateway behavior.

Install the adapter
Install the adapter的直接链接

Install the Discord adapter from the Chat SDK:

npm install @chat-adapter/discord

Agent configuration
Agent configuration的直接链接

Add createDiscordAdapter() to the agent's channels.adapters object:

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(),
},
},
})

Register the agent on the Mastra instance:

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

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

Adapter setup
Adapter setup的直接链接

Follow the Chat SDK Discord adapter docs for Discord-specific setup, including application creation, bot tokens, interaction endpoints, permissions, and Gateway behavior.

The adapter reads Discord credentials from environment variables such as:

.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 generates the Discord webhook route from the agent ID and adapter key:

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

Use your public Mastra server URL as the base URL:

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

Use this URL anywhere the Discord adapter docs ask for the interactions endpoint or webhook URL. Discord regular message events may require Gateway setup outside the generated webhook route; follow the Chat SDK adapter docs for that path.