Skip to content
BotServBotServ
DiscordBotAI BotOllamaNode.jsPythonSelf-Hosting

Discord Bot with Local AI

Build a Discord bot with local AI: Connect to Discord, integrate Ollama, and ensure data privacy.

S

schutzgeist

2 min read
Discord Bot with Local AI

Discord Bot with Local AI

What this article covers

  • How a Discord bot with local AI works
  • Which libraries work well for Python and Node.js
  • How to integrate Ollama into your bot
  • Data privacy and Discord permissions to keep in mind

Introduction: Discord bot with local AI

Adding your own AI to a Discord server is a popular entry point into local language models. You can answer questions, execute commands, or summarize content from your documents without sending messages to cloud services.

Local AI is ideal for this because server conversations and attachments stay within your infrastructure. This is especially valuable for communities, businesses, and private groups that care about data privacy.

What do you need?

To run a Discord bot with local AI, you need:

  • A Discord account and your own Discord application
  • A server to run the bot on, such as a home server or VPS
  • A running Ollama instance with at least one model like Llama 3.2
  • Python or Node.js, depending on which library you prefer

For Python, discord.py is the most popular choice. For Node.js, discord.js is widely used. Both offer similar capabilities.

Creating a Discord application

In the Discord Developer Portal, create a new application. There you specify which permissions the bot needs. At minimum, it needs to read and send messages. If the bot should react to messages, it also needs Message Content Intents permissions.

Copy the bot token and store it in an environment variable or configuration file. Never hardcode it into your source code.

Integrating Ollama

The bot sends user input to the Ollama API and waits for a response. This typically happens over HTTP:

import requests

def frage_ollama(prompt, modell='llama3.2'):
    url = 'http://localhost:11434/api/generate'
    daten = {
        'model': modell,
        'prompt': prompt,
        'stream': False
    }
    antwort = requests.post(url, json=daten)
    return antwort.json()['response']

In Discord, the bot listens for a message, calls this function, and posts the response back to the channel.

Context and memory

A bot feels much better when it can remember earlier messages. To achieve this, store the last few messages from a channel or thread in a list and include them in the prompt. This creates a simple conversation history.

For production bots, limit the context length. Otherwise, token usage and response time grow quickly. A limit of five to ten messages is a good starting point.

Data privacy and moderation

The bot should not perform sensitive commands or actions on its own. An AI bot in Discord is a public target for prompt injection. Keep these points in mind:

  • Allow only specific roles to use certain commands
  • Validate user input before sending it to Ollama
  • Don’t let the bot delete files, send emails, or call external APIs without human approval
  • Log actions so you can track what happened

Summary: Discord bot with local AI

A Discord bot with local AI connects Discord to Ollama. You create an application in the Discord Developer Portal, build the connection in Python or Node.js, and integrate the Ollama API. Through context limits, role-based access control, and input validation, you keep operations secure and compliant with data privacy requirements.

Back to Blog
Share:

Nächster Artikel in Chatbots

Weiterlesen
IRC Bot: Advanced Setup

Related Posts