A Discord bot that integrates with Cloudflare Workers AI to provide conversational AI responses with persistent conversation memory.
- Conversational AI powered by Cloudflare Workers AI (Llama 3.1 8B model & llama-4-scout-17b-16e-instruct work, others depends on the response and I haven't tested)
- Auto RAG search functionality for knowledge base queries
- AI image generation using Cloudflare Workers AI image models
- Maintains conversation context (remembers previous messages)
- Smart memory management (keeps last 6 messages + system prompt)
- Secure environment variable configuration
- Simple
!ask,!d2, and!drawcommand interfaces
- Python 3.7 or higher
- A Discord account
- A Cloudflare account with Workers AI access
- Server to host bot with constant websocket connection (trying to see if Durable Objects is viable for this, will add later if so)
- Go to the Discord Developer Portal
- Click "New Application" and give it a name
- Navigate to the "Bot" section in the left sidebar
- Click "Add Bot" to create a bot user
- Under the "Token" section, click "Copy" to get your bot token (save this for later)
- In the "Bot" section, scroll down to "Privileged Gateway Intents"
- Enable "Message Content Intent" (required for the bot to read message content)
- Navigate to the "OAuth2" > "URL Generator" section
- Under "Scopes", select:
bot
- Under "Bot Permissions", select:
Send MessagesRead Message History
- Copy the generated URL from the OAuth2 URL Generator
- Open the URL in your browser
- Select the server you want to add the bot to
- Click "Authorize"
- Complete any CAPTCHA if prompted
- Log in to your Cloudflare Dashboard
- Go to "AI" > "Workers AI" in the sidebar
- Note your Account ID (found in the right sidebar)
- Create an API token:
- Go to "My Profile" > "API Tokens"
- Click "Create Token"
- Use the "Custom token" template
- Give it a name and set permissions for Workers AI
- Set up an AI Gateway (optional but recommended):
- Go to "AI" > "AI Gateway"
- Create a new gateway and note the gateway name
Auto RAG allows the bot to search through your knowledge base and provide contextual answers.
- In your Cloudflare Dashboard, go to "AI" > "Auto RAG"
- Create a new Auto RAG instance:
- Click "Create Auto RAG"
- Give it a name (save this as your
AUTORAG_NAME) - Upload your documents or connect data sources
- Create an Auto RAG API token:
- Select the AutoRAG
- Click Use AutoRAG
- Click API
- Click "Create an AutoRAG API Token"
- Give token AutoRAG run permissions (might also need edit & read idk)
- Save this token as your
AUTORAG_API_KEY
You can run this bot either directly with Python or using Docker. Docker is recommended for production deployments.
- Docker and Docker Compose installed on your system
- Git (to clone the repository)
- Clone the repository:
git clone https://github.com/jcamdev/clanker.git
cd clanker-
Configure environment variables: Create a
.envfile in the project root (see Environment Configuration section below or or .env.example for details) -
Build and run with Docker Compose:
docker-compose up -dThe bot will automatically start and connect to Discord. To view logs:
docker-compose logs -f discord-bot- Start the bot:
docker-compose up -d - Stop the bot:
docker-compose down - View logs:
docker-compose logs -f discord-bot - Restart the bot:
docker-compose restart - Rebuild after code changes:
docker-compose up --build -d
- Python 3.7 or higher (pref 3.11)
- pip
git clone https://github.com/jcamdev/clanker.git
cd clanker
pip install -r requirements.txtCreate a .env file in the project root:
# Discord Bot Token (from Discord Developer Portal)
DISCORD_TOKEN=your_discord_bot_token_here
# Cloudflare Credentials
API_KEY=your_cloudflare_api_key_here
ACCOUNT_ID=your_cloudflare_account_id_here
AI_GATEWAY=your_ai_gateway_name_here
MODEL=e.g. @cf/meta/llama-3.1-8b-instruct
# Auto RAG Configuration (Optional - only needed for !d2 command)
AUTORAG_NAME=your_autorag_instance_name
AUTORAG_API_KEY=your_autorag_api_key
# Image Generation Configuration (Optional - only needed for !draw command)
DRAW_MODEL=e.g. @cf/stabilityai/stable-diffusion-xl-base-1.0
# System Prompt (customize the bot's personality)
SYSTEM_PROMPT=You are a helpful AI assistant. Provide clear, concise, and informative responses.python discord_bot.pyYou should see:
[Bot Username] has connected to Discord!
Bot is ready and listening for !ask commands
!ask <your question>- Ask the AI a question with conversation memory!d2 <your query>- Search your Auto RAG knowledge base (no conversation memory)!draw <prompt>- Generate an AI image from your text prompt
!ask Command:
- Maintains conversation context across messages
- Remembers previous questions and answers in the same channel
- Uses the system prompt to define AI personality
- Best for ongoing conversations and follow-up questions
!d2 Command:
- Searches through your uploaded knowledge base
- No conversation memory (each query is independent)
- Returns information from your specific documents
- Best for factual lookups and document-based questions
Conversational AI (!ask):
!ask What is Python?
!ask How do I install it?
!ask What are some good Python libraries for web development?
Knowledge Base Search (!d2):
!d2 What is Dota 2?
!d2 How do I build items in Dota?
!d2 Best heroes for beginners
AI Image Generation (!draw):
!draw cyberpunk cat
!draw sunset over mountains
!draw abstract art with blue and gold
!draw a futuristic cityscape at night
Edit the SYSTEM_PROMPT in your .env file to change the bot's personality:
SYSTEM_PROMPT=You are a coding assistant specializing in Python. Always provide code examples when relevant.The bot automatically manages conversation memory:
- Keeps the last 6 messages per Discord channel
- Always preserves the system prompt
- Each Discord channel has its own conversation thread
Feel free to submit issues and enhancement requests!