Content is user-generated and unverified.

How to Set Up a Discord Bot Powered by Any AI Model (via OpenRouter)

This is a beginner-friendly overview of how I set up a Discord bot that acts as a front-end for an AI model. The bot lives in my Discord server, and when I message it, it responds using an AI of my choice (I used DeepSeek R1 0528, but you can use Claude, GPT, Gemini, Llama, etc. — anything available on OpenRouter).

What you'll need:

  • A Discord account
  • An OpenRouter account + API key (pay-as-you-go, most models are very cheap)
  • Python installed on your computer
  • (Optional) A Supabase account if you want your bot to remember past conversations

The Architecture (Big Picture)

You type in Discord → Bot picks up your message → Sends it to OpenRouter API → 
OpenRouter routes it to your chosen AI model → AI responds → Bot posts the reply in Discord

That's it. The bot is just a messenger between you and the AI.


Phase 1: Create Your Discord Bot

  1. Go to discord.com/developers/applications
  2. Click New Application, name it whatever you want
  3. Go to Bot in the sidebar → turn on Message Content Intent (this lets your bot read messages)
  4. Click Reset Token and save your bot token somewhere safe — treat it like a password
  5. Go to OAuth2URL Generator → check bot under Scopes
  6. Under Bot Permissions, check: Send Messages, Read Message History, View Channels
  7. Copy the generated URL, paste it in your browser, and invite the bot to your server

Don't have a server? Just hit the + button in Discord's sidebar and make one.

Your bot should now appear in your server's member list (offline — it has no brain yet).


Phase 2: Install Dependencies

Open your terminal (Command Prompt on Windows, Terminal on Mac) and run:

python -m pip install discord.py requests

If you're adding memory with Supabase, also run:

python -m pip install supabase

Phase 3: The Bot Script

Create a Python file (e.g., bot.py) with the following structure:

  • Your credentials — Discord bot token, OpenRouter API key, (optionally) Supabase URL and key
  • Your AI config — model name, system prompt (this is the AI's personality/instructions), and sampling parameters (temperature, top_p, max_tokens, etc.)
  • Message handler — when someone sends a message in your server, the bot grabs it, packages it with the system prompt and conversation history, sends it to OpenRouter's /api/v1/chat/completions endpoint, and posts the AI's response back into Discord

The OpenRouter API call looks something like this:

python
response = requests.post(
    "https://openrouter.ai/api/v1/chat/completions",
    headers={"Authorization": f"Bearer {YOUR_OPENROUTER_KEY}"},
    json={
        "model": "deepseek/deepseek-r1-0528",  # swap for any model
        "messages": [
            {"role": "system", "content": "Your system prompt here"},
            {"role": "user", "content": "The user's message"}
        ],
        "temperature": 0.7,
        "max_tokens": 2048
    }
)

You have full control over the system prompt and all sampling parameters — same as using any AI API directly.


Phase 4 (Optional): Persistent Memory with Supabase

Without a database, your bot forgets everything when you restart it. With Supabase:

  1. Create a free Supabase project
  2. Create a table called messages with columns: id, channel_id, role, content, created_at
  3. In your bot script, save every message (user + AI) to the database
  4. Before each API call, load the last N messages from the database so the AI has context

This gives your bot real conversation memory that survives restarts.


Phase 5: Run It

python bot.py

Keep the terminal open — the bot is alive as long as the script is running. You'll see a message like "Bot is ready!" and your bot will show as online in Discord.


Tips

  • Model swapping is easy — just change the model string in your config. Browse available models at openrouter.ai/models
  • DeepSeek R1 has <think> tags in its responses (chain-of-thought reasoning). You'll want to strip those before posting to Discord with a simple regex
  • Discord has a 2000-character limit per message. If the AI's response is longer, chunk it into multiple messages
  • For the full code and detailed walkthrough, I highly recommend pasting this guide into Claude, ChatGPT, or any AI assistant and asking them to write the complete bot script for you. They can walk you through every line and troubleshoot issues in real time — that's actually how I built mine!

Built with help from Claude. If you get stuck on any step, genuinely just ask an AI to walk you through it — they're great at this kind of thing.

Content is user-generated and unverified.
    How to Build a Discord AI Bot with OpenRouter | Claude