This is a Discord bot that integrates with the Ollama API to generate responses based on a custom set of instructions. The bot can be activated to respond to all messages in a specific channel, and it can be deactivated when necessary. The bot uses the aiohttp library for asynchronous HTTP requests and supports custom instructions for more dynamic responses. It also logs commands with a timezone specified in .env
. If the response is longer than 2000 characters the response will be in a .md file.
⚠️ If you use Windows the bot might not be able to log commands or read the instructions.txt
file. I suggest using Windows Subsystem for Linux to run the bot if you're using Windows.
This project requires the following python dependencies:
discord.py
: For building the bot.aiohttp
: For making asynchronous HTTP requests.python-dotenv
: To manage environment variables.pytz
: For time logging in the command log.
You can install these dependencies via requirements.txt
.
You also need git to clone the repository.
# 1. Clone the repository to your local machine:
git clone https://github.com/mininukefromfallout2/discord-ollama-chatbot
cd discord-ollama-chatbot
# 2. Create a virtual environment (recommended but not required):
python3 -m venv venv
# 3. Activate the virtual environment:
source venv/bin/activate # On macOS/Linux
venv\Scripts\activate # On Windows
# 4. Install the required dependencies:
pip3 install -r requirements.txt
Create a .env
file in the root of your project directory to store sensitive environment variables.
BOT_TOKEN=YOUR_TOKEN_HERE
TIMEZONE=America/New_York
OLLAMA_MODEL=phi3:mini
OLLAMA_ERROR_MESSAGE=I couldn't process that.
- BOT_TOKEN: Your Discord bot token (can be obtained from the Discord Developer Portal).
- TIMEZONE: Timezone to log commands in. List of timezones you can use are in
timezones.txt
(these were generated by pytz.all_timezones). - OLLAMA_MODEL: The model used by Ollama (e.g.,
phi3:mini
). - OLLAMA_ERROR_MESSAGE: Customizable error message shown if Ollama fails to generate a response.
-
Install Ollama:
-
Download and install Ollama from ollama.com.
-
After installation, open a terminal and run:
ollama run phi3:mini
-
-
Test API:
- Open your browser and visit
http://localhost:11434/api/generate
to ensure the API is running.
- Open your browser and visit
-
Stopping ollama:
- In the same terminal run
ollama stop phi3:mini
- In the same terminal run
-
Install Ollama:
-
Download and install Ollama from ollama.com.
-
Run Ollama with the following command:
ollama run phi3:mini
-
-
Test API:
- Open your browser and visit
http://localhost:11434/api/generate
to ensure the API is running.
- Open your browser and visit
-
Stopping ollama:
- Run
ollama stop phi3:mini
- Run
Once everything is set up, you can run the bot using Python.
# 1. Ensure the virtual environment is activated:
source venv/bin/activate # On macOS/Linux
venv\Scripts\activate # On Windows
# 2. Run Ollama:
ollama run phi3:mini
# 3. Run the bot:
python3 chatbot.py
The bot will log into Discord using the token provided in the .env
file. You should see a message in your terminal indicating that the bot has successfully logged in.
Activates the bot to respond to all messages in the channel where the command is used.
/activate
Deactivates the bot from responding to all messages in the channel where the command is used.
/deactivate
Reloads the instructions from instructions.txt
. If the file doesn't exist, the default instructions will be used.
/reload_instructions
When the bot is active in a channel, it will respond to all messages in that channel. The bot uses the instructions from the instructions.txt
file (or the default instructions) to formulate a response using the Ollama API.
# Install dependencies for building aiohttp
sudo apt update
sudo apt install -y python3-dev build-essential
# Install aiohttp
pip3 install aiohttp
# Install dependencies for building aiohttp
brew install python3
# Install aiohttp
pip3 install aiohttp
-
Install Visual C++ Build Tools:
- Download and install from: Visual Studio Downloads.
-
Install aiohttp using pip:
pip install aiohttp
-
On Ubuntu/Debian-based systems, you can install Python with the following command:
sudo apt update sudo apt install python3 python3-pip
-
On other Linux distributions, use your package manager to install Python (e.g.,
yum
,dnf
, etc.).
-
Install Python 3 using Homebrew:
brew install python3
-
Verify the installation:
python3 --version
-
Download Python from python.org.
-
Run the installer and ensure the "Add Python to PATH" checkbox is checked during installation.
-
Verify the installation:
python3 --version
To obtain a bot token, follow these steps:
- Go to the Discord Developer Portal.
- Create a new application.
- Under the "Bot" tab, click "Reset Token" (while youre in the bot tab scroll down and enable Message Content Intent and Server Members Intent).
- Copy the bot token and paste it into the
.env
file underBOT_TOKEN
.
To invite the bot to your server:
- Go to the "OAuth2" tab in the Discord Developer Portal.
- Under "OAuth2 URL Generator", select "bot" as a scope and choose the necessary permissions which will be said in the next section. Also make sure to enable the message content intent in the Privileged Gateway Intents section
- Copy the generated URL and paste it into your browser to invite the bot to your server.
- Read Message History
- Send Messages
- View Channels
- Attach Files
If you have any problems please create an issue and I'll get to it as soon as I can