A Discord bot that integrates with Ollama to generate AI-powered replies.
This bot allows you to interact with Ollama's models directly from Discord, making it easy to generate text responses, summaries, and more.
Both the Discord bot and Ollama are designed to be run locally in Docker containers, providing a self-hosted solution for AI interactions, ensuring privacy and control over your data.
| Command | Arguments | Description |
|---|---|---|
/settings model set <model> |
model (string) |
Set the Ollama model for this guild. |
/settings model get |
none | Get the current Ollama model. |
/settings model list |
none | List all available models from the Ollama server. |
/settings model pull <model> |
model (string) |
Download (pull) a model into Ollama. |
/settings prompt set <prompt> |
prompt (string) |
Set the system prompt sent at the start of each chat. |
/settings prompt get |
none | Get the current system prompt. |
/settings role set [role] |
role (@Role) or leave blank |
Restrict commands to users who have this role (or clear). |
/settings role get |
none | Show the currently required role (if any). |
/settings history set [reply_history] [message_history] |
reply_history (int), message_history (int) |
Set how many messages to fetch as context. |
/settings history get |
none | Show current reply & message history limits. |
To chat with the bot you can do one of the following:
- Mention the bot: Mention the bot (example:
@Ollama Bot) followed by your message. - Reply to the bot: Click on the bot's message and type your reply.
Doing either will send your message to the bot, which will then generate a response using the configured Ollama model.
To install, run, and add the bot to your Discord server, follow these steps:
- Create a Discord Application:
- Go to the Discord Developer Portal.
- In the
Botsection:- Note down the
Token, as you will need it to run the bot. - Under
Privileged Gateway Intents, enable theMessage Content Intentto allow the bot to read messages.
- Note down the
- Invite the Bot to Your Server:
- In the
Installationsection:- Under
Installation Contexts, SelectGuild Installonly. - Under
Default Install Settings, add theapplications.commandsandbotscopes, then theSend MessagesandSend Messages in Threadspermissions. - Finally, copy the
Discord Provided Linkand paste it into your browser to invite the bot to your server.
- Under
- In the
Tip
For more information on creating a Discord bot, refer to the Discord Developer Portal documentation.
- Run Docker Compose
- Ensure you have Docker installed.
- Copy the
docker-compose.ymlfile from this repository to your local machine. - Customize the
docker-compose.ymlfile if necessary, such as changing mounted volumes or ports. - Create a
.envfile in the root directory with your Discord bot token and Ollama API URL:DISCORD_APP_TOKEN=your_discord_app_token OLLAMA_API_URL=your_ollama_api_url
- Run the following command to start the bot and Ollama (detached):
docker compose up -d
Tip
For more information about the Ollama Docker image, refer to the Ollama documentation (GPU support is available here).
.
├── .env The environment variables for the bot
├── .gitignore Files and directories to ignore in git
├── README.md Documentation for the bot
├── Dockerfile Dockerfile to build the bot image
├── docker-compose.yml Example Docker Compose file
├── src/ Source code for the bot
│ ├── main.py Main entry point for the bot
│ ├── ollama_bot.py Discord client and message handler
│ ├── settings_manager.py Manages settings for the bot
│ ├── history_group.py /settings history subcommands
│ ├── model_group.py /settings model subcommands
│ ├── prompt_group.py /settings prompt subcommands
│ ├── role_group.py /settings role subcommands
│ ├── settings_group.py /settings group command
│ ├── utils.py Utility functions for the bot
│ └── requirements.txt Python dependencies
Important
Ollama must be running for the bot to function properly. You can run Ollama using Docker with the following command:
docker run -d -v ollama:/root/.ollama -p 11434:11434 --name ollama ollama/ollamaTo run the bot locally, follow these steps:
- Clone the repository:
git clone https://github.com/TechnoBro03/OllamaBot cd ollamabot - Create & activate a virtual environment (optional but recommended):
python -m venv venv source venv/bin/activate # On Windows use `venv\Scripts\activate`
- Install dependencies:
pip install -r src/requirements.txt
- Create a
.envfile in the root directory with the following content:DISCORD_APP_TOKEN=your_discord_app_token OLLAMA_API_URL=your_ollama_api_url
- Run the bot:
python src/main.py
To run the bot using Docker, follow these steps:
- Ensure you have Docker installed on your machine.
- Create a
.envfile in the root directory with your Discord bot token and Ollama API URL:DISCORD_APP_TOKEN=your_discord_app_token OLLAMA_API_URL=your_ollama_api_url
- Build and run the Docker container:
docker build -t ollama-bot . docker run -d --env-file .env --name ollama-bot ollama-bot