AI Starter Package
Guides/Discord Setup

Discord Bot Setup for AI Agents

Bring your AI brain into Discord. Create an application, configure a bot, set permissions, and connect it to Claude Code — all without leaving your team's existing workspace.

Discord APISlash CommandsTeam-Friendly

Why Connect AI Agents to Discord?

Team collaboration

Run your AI agent inside your team Discord server so everyone can ask questions and get answers in shared channels.

Slash commands

Register /ask, /summarize, /generate, or any custom command that routes directly to your Claude agent.

Role-based access

Use Discord roles to control who can trigger expensive AI operations — admin only, specific channels, etc.

Rich embeds

Format AI responses as Discord embeds with headers, fields, colors, and links — much richer than plain text.

Step 1 — Create a Discord Application

1

Open the Developer Portal

Go to discord.com/developers/applications and click New Application. Give it a name (e.g., "My AI Brain") and accept the terms.

2

Create the Bot User

In the left sidebar, click Bot. Click Add Bot and confirm. Your application now has a bot user that can join servers and receive messages.

3

Get the Bot Token

On the Bot page, click Reset Token and copy it immediately. Store it in your environment:

# .env.local
DISCORD_TOKEN=MTExxx.YYYyyy.ZZZzzz
DISCORD_CLIENT_ID=123456789012345678
DISCORD_PUBLIC_KEY=abc123def456...
This token is shown only once
Save it immediately. If you lose it, you must reset the token — which invalidates all existing connections.
4

Invite the Bot to Your Server

Go to OAuth2 → URL Generator. Select bot and applications.commands scopes. Then select the permissions below and use the generated URL to invite:

Send MessagesRequired to reply in channels
Use Slash CommandsRequired to register and respond to / commands
Read Message HistoryAllows reading context from previous messages
Embed LinksEnables rich embed formatting in responses
Attach FilesAllows sending file-based responses (e.g., code files, reports)
Add ReactionsUseful for acknowledging messages while processing

Slash Commands vs Message-Based Interaction

Slash Commands

/ask What is the weather in Paris?
Pros
  • +Discoverable — Discord shows available commands
  • +No prefix needed
  • +Works in all channels where bot has permission
Cons
  • Must register commands via API before use
  • Requires Interactions endpoint URL
Best for: Structured, user-facing commands with clear intent

Message Events

@MyBot summarize this thread
Pros
  • +More conversational, natural feel
  • +Easy to implement with discord.js
  • +Works with message content
Cons
  • Requires Message Content Intent (privileged)
  • Less discoverable for new users
Best for: Power users, internal team bots, conversational workflows

Connecting to Your AI Brain

The standard pattern: a Next.js API route handles Discord interactions, routes the message to Claude via the Anthropic SDK, and replies to the user within Discord's 3-second acknowledgement window.

// src/app/api/discord/route.ts
export async function POST(req: Request) {
const body = await req.json();
// 1. Verify Discord signature (required)
verifyDiscordSignature(req, body);
// 2. Handle ping from Discord
if (body.type === 1) return Response.json({ type: 1 });
// 3. Defer reply (gives 15 min to respond)
deferReply(body.token);
// 4. Route to Claude, then followUp()
// ... anthropic.messages.create(...)
}

Always defer the reply immediately. Discord requires a response within 3 seconds — Claude takes longer. Use the interaction token to send a follow-up when Claude finishes.

Security Considerations

Use environment variables for all tokens
Store DISCORD_TOKEN and DISCORD_CLIENT_SECRET in .env.local — never in source code.
Verify interaction signatures
For slash commands via webhook, Discord signs requests with your app's public key. Verify every request.
Restrict bot to specific guilds
Register slash commands as guild commands during development. Only promote to global when production-ready.
Implement per-user rate limiting
Track requests per user per minute. Block users who exceed a threshold to prevent runaway API costs.
Log all interactions
Record user ID, channel, command, and response for every interaction. Essential for abuse detection.

Get Your Agent Into Discord Today

Our managed agent packages include Discord bot setup — slash command registration, Claude routing, signature verification, and rate limiting — all pre-configured for your server.