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

💡 Key Takeaways

📜 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

  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

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

  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.

❓ 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.

Related Articles
2026-07-26
AI Code Review Prompt Engineering
2026-08-13
Perplexity API Guide 2026: Base URL, URL Scheme and Which Model to Call
2026-08-01
ChatGPT Search SEO: 9 Ways to Get Cited by AI Search in 2026

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.

💬 Comments (0)

No comments yet. Be the first!

Login to comment