A Discord bot that uses Anthropic's Claude AI to respond to messages in channels. Each channel maintains its own conversation history and custom system prompt (set via pinned messages).
- Python 3.8 - 3.11 (Python 3.12+ not fully supported yet due to package compatibility)
- pip (Python package installer)
- Responds to all messages in channels where it has access
- Maintains separate conversation histories per channel
- Uses pinned messages as custom system prompts for each channel
- Automatically splits long responses into multiple messages
- Includes commands to reset conversations and refresh system prompts
- Go to the Discord Developer Portal (https://discord.com/developers/applications)
- Create your bot application
- In the "Bot" section, enable these Privileged Gateway Intents:
- PRESENCE INTENT
- SERVER MEMBERS INTENT
- MESSAGE CONTENT INTENT
- Save changes
- Use the OAuth2 URL Generator to create an invite link with these permissions:
- Read Messages/View Channels
- Send Messages
- Read Message History
- View Server Members
- Follow the URL you generate and install it on your discord server.
- Install required dependencies: Ubuntu:
apt install docker.io docker-compose
Mac:
brew install docker
- Download the .env template and the docker-compose file
curl -o .env https://raw.githubusercontent.com/fredsmith/discord-ai-chat/refs/heads/main/.env.example
curl -o docker-compose.yaml https://raw.githubusercontent.com/fredsmith/discord-ai-chat/refs/heads/main/docker-compose.yaml
- Configure your environment variables in
.env
:DISCORD_TOKEN
: Your Discord bot token (get it from Discord Developer Portal)ANTHROPIC_API_KEY
: Your Anthropic API key (get it from Anthropic Console)
- Ensure you have a compatible Python version installed (3.8 - 3.11):
python --version
If needed, install a compatible Python version. We recommend Python 3.11:
- On macOS with Homebrew:
brew install [email protected]
- On macOS with asdf:
asdf install python 3.11.8
- Clone this repository:
git clone https://github.com/fredsmith/discord-ai-chat.git
cd discord-ai-chat
- (Optional but recommended) Create a virtual environment:
python -m venv venv
source venv/bin/activate # On macOS/Linux
# or
.\venv\Scripts\activate # On Windows
- Install required dependencies:
pip install -r requirements.txt
If you encounter any installation errors:
- Make sure you're using a compatible Python version (3.8 - 3.11)
- Try upgrading pip:
pip install --upgrade pip
- If using macOS, you might need to install additional build tools:
xcode-select --install
- Create a
.env
file from the template:
cp .env.example .env
- Configure your environment variables in
.env
:DISCORD_TOKEN
: Your Discord bot token (get it from Discord Developer Portal)ANTHROPIC_API_KEY
: Your Anthropic API key (get it from Anthropic Console)
- Start the bot:
docker-compose up -d
or
python bot.py
-
Set up channel-specific prompts:
- Pin a message in any channel to set its system prompt
- The first pinned message will be used as the system prompt
- If no message is pinned, a default helpful assistant prompt is used
-
Available commands:
!reset
: Reset the conversation history for the current channel!refresh_prompt
: Refresh the system prompt from pinned messages
Each channel can have its own personality or purpose by setting a custom system prompt. To set a channel's prompt:
- Write your desired system prompt as a message in the channel
- Pin that message to the channel
- Use
!refresh_prompt
to apply the new system prompt
Example system prompts:
- "You are a helpful programming assistant. Provide code examples and explanations."
- "You are a creative writing coach. Help users improve their writing skills."
- "You are a friendly chat companion. Engage in casual conversation."
- Runtime Issues:
- Verify your Discord bot token is correct
- Ensure the bot has proper permissions in your Discord server
- Check that your Anthropic API key is valid
- Make sure the bot has access to read/send messages in channels
- The bot maintains conversation history per channel until reset
- Long responses are automatically split into multiple messages
- Each channel's conversation is independent
- System prompts can be updated at any time by changing the pinned message