How to Build a Discord AI Bot in 2026: discord.py (16k Stars) + LLM - Step by Step

๐Ÿ“˜ Tutorials 2026-08-05 2 min read

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

## 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 1. Open https://discord.com/developers/applications and click **New Application**. 2. Go to **Bot** -> **Reset Token** -> copy the token (keep it secret, treat it like a password). 3. Under **Bot Settings**, enable **Message Content Intent** (required to read messages). 4. In **OAuth2 -> URL Generator**, select the `bot` scope and the `Send Messages` + `Use Slash Commands` permissions, then open the generated URL in a browser to invite the bot to your server. ## Step 2 - Install and run the minimal bot ```bash pip install discord.py ``` ```python 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 ```python 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 1. **Token leaks** - never commit the token to GitHub; use environment variables. 2. **Message Content Intent disabled** - the bot runs but never sees messages. 3. **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.
Related Articles
2026-06-29
The Mainline Dragon Strategy โ€” Chasing the Leader Without Paying for Data
2026-06-29
The AI Hiding in Your Laptop
2026-07-14
Free AI Coding Assistant Setup 2026: 5-Min VS Code Guide (Continue, Copilot, Windsurf)

๐Ÿ’ฌ Comments (0)

No comments yet. Be the first!

Login to comment