DEV Community

Cover image for Creating an AI Discord Bot with Ollama
Ethan
Ethan

Posted on

Creating an AI Discord Bot with Ollama

Hello! 😎
In this tutorial I will teach you how to create your own AI-powered Discord bot from scratch. By the end, you'll have a bot that can:

  • Respond to messages using AI
  • Have conversations with users

No prior experience is needed but it does help to have some basic knowledge about Python.

What you will learn

  1. How to create a Discord bot application
  2. How to install and use Ollama (Fully local)
  3. How to write Python code to connect them
  4. How to make your bot talk like a real person
  5. How to run the bot 24/7

What you will need

  • Python 3.8+ - Programming language
  • Discord Account - To create the bot
  • Ollama - Free local AI
  • Text Editor - And IDE you like

Part 1: Setting up your Discord bot

First we need to create the Discord bot at the Discord developers page:

https://discord.com/developers/applications

Login and click on New Application, give it a name you like and create the new application.

Next navigate to the Bot tab and scroll down to the privileged gateway intents and enable the following, this step is important.

  • Presence Intent
  • Server Members Intent
  • Message Content Intent

When enabled save all the changes, this will allow your bot to see messages and user info.

Next we need the token scroll back up and click on Reset Token and copy it to your clipboard and save it somewhere where only you can see.

Next we need to invite the bot to the server, this can be done via the sidebar click on OAuth2.

We need to allow scopes and permissions for scopes check the following two:

  • bot
  • applications.commands

For permissions select the following:

  • Send Messages
  • Send Messages in Threads
  • Read Message History
  • View Channels
  • Use Slash Commands
  • Add Reactions

Once done copy and access the generated url, select the server you want the bot to run in and accept. With this the bot should now be in the Discord server as offline.

Next lets install Ollama!


Part 2: Installing Ollama

Ollama is free software that runs AI models on your own computer. The benefits to this are:

  • Free - No API costs/tokens
  • Private - Runs locally, no data send to servers
  • Fast - Responses in seconds
  • Offline - Works without internet (after downloading models)

Download Ollama based on your machine.

Mac:

brew install ollama
Enter fullscreen mode Exit fullscreen mode

Linux:

curl -fsSL https://ollama.ai/install.sh | sh
Enter fullscreen mode Exit fullscreen mode

Windows:
Download the exe from https://ollama.ai/download

Next start Ollama:

ollama serve
Enter fullscreen mode Exit fullscreen mode

The terminal should show that ollama is running. Keep this terminal open, the bot will need this to work.

Next we need to download an AI Model, for this example im using a simple model but feel free to use anything you want.

ollama pull llama3.1
Enter fullscreen mode Exit fullscreen mode

This downloads the LLaMA 3.1 model it's:

  • Free and open source
  • Great for conversations
  • Fast responses
  • Good at following instructions

Wait for the download to complete, which takes around a couple of minutes.

Lastly lets see if the model is installed correctly.

ollama run llama3.1
Enter fullscreen mode Exit fullscreen mode

You should see output, feel free to try conversing with it, next we will finally be getting into the coding part.


Part 3: Installing Python Dependencies

First create a virtual environment and activate it.

python3 -m venv env
source env/bin/activate
Enter fullscreen mode Exit fullscreen mode

Next install the following dependencies:

pip3 install py-cord
pip3 install requests
pip3 install python-dotenv
Enter fullscreen mode Exit fullscreen mode
  • py-cord - Discord bot library
  • requests - Makes HTTP requests to talk to Ollama
  • python-dotenv - Loads environment variables, ie the Discord token

Part 4: Writing Your Bot Code

Setup the directories for the project and create a new .env file to store the token:

vim .env
Enter fullscreen mode Exit fullscreen mode

Add this line replace the token with your actual token.

DISCORD_TOKEN="token"
Enter fullscreen mode Exit fullscreen mode

Done now we can finally start coding the bot.


Part 5: The Complete Bot Code

Create a new file called bot.py and fill it with the following content, it will be explained later.

# ============================================
# IMPORTS - Load the libraries we need
# ============================================
import discord
from discord.ext import commands
import requests
import json
import os
from dotenv import load_dotenv

# Load environment variables from .env file
load_dotenv()

# ============================================
# CONFIGURATION
# ============================================

# Get bot token from .env file
TOKEN = os.getenv("DISCORD_TOKEN")

# Ollama API endpoint (where AI is running)
OLLAMA_URL = "http://localhost:11434/api/generate"

# Which AI model to use
OLLAMA_MODEL = "llama3.1"

# Bot command prefix (e.g., !chat hello)
PREFIX = "!"

# ============================================
# BOT SETUP
# ============================================

# Create bot with all permissions
intents = discord.Intents.all()
bot = commands.Bot(command_prefix=PREFIX, intents=intents)

# ============================================
# HELPER FUNCTION: Talk to Ollama AI
# ============================================

def ask_ai(prompt, temperature=0.9):
    """
    Send a prompt to Ollama and get AI response.

    Args:
        prompt (str): What to ask the AI
        temperature (float): Creativity level (0.0-1.0)
            - 0.0 = Very predictable, factual
            - 0.5 = Balanced
            - 1.0 = Very creative, random

    Returns:
        str: AI's response
    """
    try:
        # Prepare the request
        payload = {
            "model": OLLAMA_MODEL,
            "prompt": prompt,
            "stream": False,  # Get complete response at once
            "options": {
                "temperature": temperature,
                "num_predict": 200  # Max response length in tokens
            }
        }

        # Send request to Ollama
        response = requests.post(OLLAMA_URL, json=payload, timeout=30)

        # Check if successful
        if response.status_code == 200:
            data = response.json()
            return data.get("response", "").strip()
        else:
            return "Sorry, I had trouble thinking of a response!"

    except requests.exceptions.ConnectionError:
        return "Error: Ollama is not running! Start it with 'ollama serve'"
    except Exception as e:
        return f"Error: {str(e)}"

# ============================================
# EVENT: Bot is Ready
# ============================================

@bot.event
async def on_ready():
    """Called when bot successfully connects to Discord"""
    print(f"βœ… Bot is online as {bot.user.name}")
    print(f"βœ… Connected to {len(bot.guilds)} server(s)")
    print(f"βœ… Prefix: {PREFIX}")
    print("Ready to chat!")

# ============================================
# EVENT: Message Received
# ============================================

@bot.event
async def on_message(message):
    """Called whenever a message is sent in a channel the bot can see"""

    # Don't respond to yourself (prevents infinite loops!)
    if message.author == bot.user:
        return

    # Don't respond to other bots
    if message.author.bot:
        return

    # Check if bot was mentioned
    if bot.user.mentioned_in(message):
        # Remove the mention from the message
        user_message = message.content.replace(f'<@{bot.user.id}>', '').strip()

        # If they just mentioned with no text
        if not user_message:
            user_message = "Hello!"

        # Show typing indicator (looks more natural)
        async with message.channel.typing():
            # Ask AI for response
            ai_response = ask_ai(user_message)

            # Send response
            await message.channel.send(ai_response)

    # Process commands (important for !commands to work)
    await bot.process_commands(message)

# ============================================
# COMMAND: !chat
# ============================================

@bot.command(name="chat")
async def chat_command(ctx, *, message: str = None):
    """
    Chat with the AI bot.
    Usage: !chat How are you today?
    """
    if not message:
        await ctx.send("Please provide a message! Example: `!chat Hello!`")
        return

    # Show typing indicator
    async with ctx.typing():
        # Get AI response
        response = ask_ai(message)

        # Send it
        await ctx.send(response)

# ============================================
# COMMAND: !help
# ============================================

@bot.command(name="help")
async def help_command(ctx):
    """Show available commands"""

    help_text = """
**πŸ€– AI Bot Commands**

`!chat <message>` - Chat with the AI
Example: `!chat What's your favorite color?`

`@Bot <message>` - Mention the bot to talk
Example: `@MyBot Tell me a joke!`

`!help` - Show this help message

**How it works:**
This bot uses Ollama (local AI) to generate responses. The AI runs on the computer hosting the bot, so all conversations are private!
    """

    await ctx.send(help_text)

# ============================================
# START THE BOT
# ============================================

if __name__ == "__main__":
    print("Starting bot...")
    print(f"Ollama URL: {OLLAMA_URL}")
    print(f"Model: {OLLAMA_MODEL}")

    # Run the bot (this blocks until bot stops)
    bot.run(TOKEN)
Enter fullscreen mode Exit fullscreen mode

Lets break down what each part does.

import discord
from discord.ext import commands
import requests
Enter fullscreen mode Exit fullscreen mode

The imports import the libraries that we need.

TOKEN = os.getenv("DISCORD_TOKEN")
OLLAMA_URL = "http://localhost:11434/api/generate"
Enter fullscreen mode Exit fullscreen mode

Configs the Discord token and the Ollama endpoint url.

def ask_ai(prompt, temperature=0.9):
    payload = {
        "model": OLLAMA_MODEL,
        "prompt": prompt,
        "stream": False,
        "options": {
            "temperature": temperature,
            "num_predict": 200
        }
    }

    response = requests.post(OLLAMA_URL, json=payload, timeout=30)

    if response.status_code == 200:
        data = response.json()
        return data.get("response", "").strip()
    else:
        return "Sorry, I had trouble thinking of a response!"
Enter fullscreen mode Exit fullscreen mode

This ask_ai function takes your message (prompt), sends it to Ollama, waits for AI to generate a response and then returns the response as text.

Now for the parameters:

  • temperature - How creative the AI is (0.0 = boring, 1.0 = wild)
  • num_predict - Maximum length of response (in tokens, roughly words)
  • stream - Set to false to wait for the complete response (not word-by-word)

Next i will explain what each event does.

@bot.event
async def on_ready():
    print(f"βœ… Bot is online as {bot.user.name}")
Enter fullscreen mode Exit fullscreen mode

Nothing too hard, this gets fired when the bot successfully connects to Discord and prints a confirmation message.

@bot.event
async def on_message(message):
    if message.author == bot.user:
        return  # Don't respond to yourself!

    if bot.user.mentioned_in(message):
        user_message = message.content
        ai_response = ask_ai(user_message)
        await message.channel.send(ai_response)
Enter fullscreen mode Exit fullscreen mode

What this does is:

  1. Triggers every time someone sends a message
  2. Checks if bot was mentioned
  3. If yes, extracts the message
  4. Asks AI for response
  5. Sends AI's response back

The bot should not reply to itself unless you want an infinite loop of spam.

Next I will explain the command.

@bot.command(name="chat")
async def chat_command(ctx, *, message: str = None):
    async with ctx.typing():  # Show "Bot is typing..."
        response = ask_ai(message)
        await ctx.send(response)
Enter fullscreen mode Exit fullscreen mode

What this does it:

  • Creates a command: !chat your message here
  • Shows typing indicator (makes it feel more human)
  • Gets AI response
  • Sends it back
bot.run(TOKEN)
Enter fullscreen mode Exit fullscreen mode

Finally the above just starts the bot, connects to Discord using the token and keeps the bot running until you turn it off.


Part 7: Running Your Bot

First make sure Ollama is running with the previous command and then start the bot.

python3 bot.py
Enter fullscreen mode Exit fullscreen mode

You should see the following console:

Starting bot...
Ollama URL: http://localhost:11434/api/generate
Model: llama3.1
βœ… Bot is online as My AI Bot
βœ… Connected to 1 server(s)
βœ… Prefix: !
Ready to chat!
Enter fullscreen mode Exit fullscreen mode

Feel free to test the bot in Discord. Try mentioning it, it should respond. Feel free to try the !chat command also.

Done you have now created a new Discord bot!


Part 8: Customizing Your Bot

You can customize various things with your bot such as its personality.
You can add a personality to the bot via a system prompt to make more personal.

If you want to increase the response length just change num_predict to a higher number.

You can also change the temperature to increase the creativity of the bot.

Other feature you can also add are things like:

  • Respond to DMs
  • Remember Context
  • Random Reactions

If you would like I can create tutorials for all the above.


Part 10: Running Your Bot 24/7

You could keep your local computer running which is free and gives you full control. This will however need your machine to stay on 24/7 and may strain your electric bill.

If you aren't able to run your machine 24/7 I recommend you run it on a cloud server using Digital Ocean, AWS, or Oracle Cloud which has a free tier.

Feel free to pick the best option that works for you.


Part 11: Next Steps

Next you can make the bot even more advanced, via the following implementations. I am also thinking of doing more tutorials expanding on the following topics.

Beginner Projects

  1. Add more commands
  2. Make it respond to specific words
  3. Add personality

Intermediate Projects

  1. Database integration - Remember user preferences
  2. Multiple AI Models - Switch between models for different tasks
  3. Image Generation - Use Stable Diffusion with Ollama
  4. Voice Channel Support - Make the bot speak with people in voice channels

Advanced Projects

  1. Multi-server bot - Deploy to many servers
  2. Web Dashboard - See stats and control the bot from the browser
  3. Custom AI Model - Train your own model with Ollama
  4. Bot Network - Multiple bots that interact with each other

Have fun coding.


Conclusion

Well done you've built your very own AI powered Discord Bot.

You now know how to:

  • Create Discord bot applications
  • Use Ollama for local AI
  • Connect Python code to both services
  • Handle commands and messages
  • Deploy your bot

I am planning to expand this series with more features such as mixing in Text to Speech to allow the bot to converse with users in the voice channel.

Side note: for production usage it's best to limit the bots intents to only the intents it needs.

Happy Coding! 😎


Like my work? I post about a variety of topics, if you would like to see more please like and follow me.
Also I love coffee.

β€œBuy Me A Coffee”

Top comments (0)