Discord Bot Development with LLMs
You'll learn how to build a Discord bot that integrates LLM capabilities using discord.py, managing intents, slash commands, and streaming AI responses.
What Makes Discord Bot Development Different
Unlike Slack's event-driven architecture, Discord bots connect via a persistent WebSocket gateway and use a more granular permissions system called intents. Think of intents as declaring upfront which types of events your bot wants to receive—like subscribing to specific radio channels rather than listening to everything.
Core Setup with discord.py
First, you need to enable the right intents in Discord's Developer Portal and your code. For an LLM bot, you typically need MESSAGE_CONTENT intent to read messages and GUILDS to know which servers your bot is in.
import discord
from discord.ext import commands
intents = discord.Intents.default()
intents.message_content = True # Required to read message text
bot = commands.Bot(command_prefix='!', intents=intents)
Slash Commands vs Text Commands
Modern Discord bots prefer slash commands (the / menu interface). These provide autocomplete and validation built into Discord's UI:
@bot.tree.command(name="ask", description="Ask the AI a question")
async def ask(interaction: discord.Interaction, question: str):
await interaction.response.defer() # Acknowledge within 3 seconds
# Call your LLM here (OpenAI, Anthropic, etc.)
response = await get_llm_response(question)
await interaction.followup.send(response)
Key Considerations
Discord requires responding to interactions within 3 seconds, so always defer() first when calling LLMs. Handle message length limits (2000 characters) by chunking responses. Store conversation context using Discord's thread IDs or user IDs as keys.
Key Takeaway: Discord bots require explicit intent declarations, favor slash commands for modern UX, and need careful handling of Discord's 3-second response deadline when integrating LLM calls.