How to Build a Discord AI Bot in 2026: discord.py (16k Stars) + LLM - Step by Step
Discord has no built-in AI assistant, but its free bot API + discord.py (16,128 stars) lets you add an LLM-powered bot to any server in about 30 minutes - with slash commands, private DMs, and zero hosting surprises.
💡 What You Will Learn
Discord has no built-in AI assistant, but its free bot API + discord.py (16,128 stars) lets you add an LLM-powered bot to any server in about 30 minutes - with slash commands, private DMs, and zero ho
💡 Key Takeaways
- Discord has no built-in AI assistant, but its free bot API plus discord.py (16,128 stars, MIT) lets you add an LLM-powered bot to any server.
- A working bot takes about 30 minutes: create an app on the Discord developer portal, invite it with the right scopes, and run one Python script.
- Slash commands and private DMs use the same code pattern — discord.py handles permissions and rate limits for you.
- Free to run: pair the bot with a local model (Ollama) for $0, or call a hosted LLM API per token.
- The two most common failures are 401 (wrong token) and rate limits (missing intents) — both fixable in under five minutes.
📜 Table of Contents
The short answer
discord.py (16,128 stars, MIT) is the most popular Python wrapper for Discord's official bot API. A bot is just a Python process that logs in with a token and reacts to messages. Add any LLM API and you have an AI assistant that works in every channel of your server.
Step 1 - Create the bot on Discord's developer portal
- Open https://discord.com/developers/applications and click New Application.
- Go to Bot -> Reset Token -> copy the token (keep it secret, treat it like a password).
- Under Bot Settings, enable Message Content Intent (required to read messages).
- In OAuth2 -> URL Generator, select the
botscope and theSend Messages+Use Slash Commandspermissions, then open the generated URL in a browser to invite the bot to your server.
Step 2 - Install and run the minimal bot
pip install discord.py
import discord
from discord.ext import commands
bot = commands.Bot(command_prefix="!", intents=discord.Intents.default())
@bot.event
async def on_ready():
print(f"Logged in as {bot.user}")
@bot.command()
async def ping(ctx):
await ctx.send("pong")
bot.run("YOUR_BOT_TOKEN")
Run it and type !ping in any channel - you should get "pong".
Step 3 - Add the LLM
import os, requests
@bot.command()
async def ask(ctx, *, prompt):
r = requests.post("https://api.deepseek.com/chat/completions",
headers={"Authorization": f"Bearer {os.environ['LLM_KEY']}"},
json={"model": "deepseek-chat", "messages": [{"role": "user", "content": prompt}],
"max_tokens": 500})
reply = r.json()["choices"][0]["message"]["content"]
# Discord messages max 2000 chars - split long replies
for i in range(0, len(reply), 1900):
await ctx.send(reply[i:i+1900])
Now !ask 帮我写一首关于猫的诗 works in your server. For slash commands, use @app_commands.command() from discord.py's built-in support.
Why long-tail "how to" content wins for AI blogs
Searching "how to build a discord bot" captures users who are one step away from building - they convert far better than broad queries like "AI bot". Step-by-step guides also get picked up by AI search engines as direct answers, because each step is self-contained and quotable.
3 common pitfalls
- Token leaks - never commit the token to GitHub; use environment variables.
- Message Content Intent disabled - the bot runs but never sees messages.
- 2000-char limit - long LLM replies crash the send; always split.
FAQ
Q: Is discord.py free? A: Yes, MIT licensed and the Discord API itself has no per-message cost.
Q: discord.py vs discord.js? A: Both are official-API wrappers. Python devs prefer discord.py; Node.js devs use discord.js. Feature parity is close.
Q: Can the bot read all my server's messages? A: Only if you give it Message Content Intent and the right permissions - by default it only sees what it is mentioned in.
❓ FAQ
Does building a Discord AI bot cost money?
No. discord.py is free (MIT open source), creating a bot in the Discord developer portal is free, and free tiers of LLM APIs (e.g. DeepSeek) are enough to run it. The only real cost is hosting if you deploy the bot to a cloud server instead of running it on your own machine.
discord.py vs discord.js — which one should I use?
discord.py is the Python wrapper (16,128 stars, MIT) and discord.js is the official Node.js library. Pick by your stack: Python → discord.py, JavaScript → discord.js. Both support the same features; discord.py's async model pairs naturally with async LLM API calls.
Can my bot read all messages in my server?
Only if you explicitly enable Message Content Intent and grant the bot the right permissions. Since 2022, Discord requires verified servers (100+ members) to read message content; smaller servers must apply for an exemption. By default the bot only receives events where it is mentioned.
Written by our editorial team; tools listed here are tested or verified against public sources. Links point to official sites or GitHub repos for reference only — no paid placements.
