Skip to content

Latest commit

 

History

History
154 lines (115 loc) · 4.83 KB

README.md

File metadata and controls

154 lines (115 loc) · 4.83 KB

Discord AI Chat Bot

chatbot-logo

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).

Requirements

  • Python 3.8 - 3.11 (Python 3.12+ not fully supported yet due to package compatibility)
  • pip (Python package installer)

Features

  • 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

Creating your Discord Bot

  1. Go to the Discord Developer Portal (https://discord.com/developers/applications)
  2. Create your bot application
  3. In the "Bot" section, enable these Privileged Gateway Intents:
    • PRESENCE INTENT
    • SERVER MEMBERS INTENT
    • MESSAGE CONTENT INTENT
  4. Save changes
  5. 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
  6. Follow the URL you generate and install it on your discord server.

Installation (Docker)

  1. Install required dependencies: Ubuntu:
apt install docker.io docker-compose

Mac:

brew install docker
  1. 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
  1. Configure your environment variables in .env:

Installation (Python)

  1. 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
  1. Clone this repository:
git clone https://github.com/fredsmith/discord-ai-chat.git
cd discord-ai-chat
  1. (Optional but recommended) Create a virtual environment:
python -m venv venv
source venv/bin/activate  # On macOS/Linux
# or
.\venv\Scripts\activate  # On Windows
  1. 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
  1. Create a .env file from the template:
cp .env.example .env
  1. Configure your environment variables in .env:

Usage

  1. Start the bot:
docker-compose up -d

or

python bot.py
  1. 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
  2. Available commands:

    • !reset: Reset the conversation history for the current channel
    • !refresh_prompt: Refresh the system prompt from pinned messages

Channel-Specific Prompts

Each channel can have its own personality or purpose by setting a custom system prompt. To set a channel's prompt:

  1. Write your desired system prompt as a message in the channel
  2. Pin that message to the channel
  3. 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."

Troubleshooting

  1. 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

Notes

  • 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