Skip to content

jcamdev/clanker

Repository files navigation

Discord LLM Bot

A Discord bot that integrates with Cloudflare Workers AI to provide conversational AI responses with persistent conversation memory.

Features

  • 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 !draw command interfaces
image

Prerequisites

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

Discord Bot Setup

1. Create a Discord Application

  1. Go to the Discord Developer Portal
  2. Click "New Application" and give it a name
  3. Navigate to the "Bot" section in the left sidebar
  4. Click "Add Bot" to create a bot user
  5. Under the "Token" section, click "Copy" to get your bot token (save this for later)

2. Configure Bot Permissions

  1. In the "Bot" section, scroll down to "Privileged Gateway Intents"
  2. Enable "Message Content Intent" (required for the bot to read message content)
  3. Navigate to the "OAuth2" > "URL Generator" section
  4. Under "Scopes", select:
    • bot
  5. Under "Bot Permissions", select:
    • Send Messages
    • Read Message History

3. Invite Bot to Your Server

  1. Copy the generated URL from the OAuth2 URL Generator
  2. Open the URL in your browser
  3. Select the server you want to add the bot to
  4. Click "Authorize"
  5. Complete any CAPTCHA if prompted

Cloudflare Setup

1. Get Your Cloudflare Credentials

  1. Log in to your Cloudflare Dashboard
  2. Go to "AI" > "Workers AI" in the sidebar
  3. Note your Account ID (found in the right sidebar)
  4. 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
  5. Set up an AI Gateway (optional but recommended):
    • Go to "AI" > "AI Gateway"
    • Create a new gateway and note the gateway name

2. Set Up Auto RAG (Optional)

Auto RAG allows the bot to search through your knowledge base and provide contextual answers.

  1. In your Cloudflare Dashboard, go to "AI" > "Auto RAG"
  2. 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
  3. 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

Installation & Configuration

You can run this bot either directly with Python or using Docker. Docker is recommended for production deployments.

Option 1: Docker (Recommended)

Prerequisites

  • Docker and Docker Compose installed on your system
  • Git (to clone the repository)

Quick Start with Docker

  1. Clone the repository:
git clone https://github.com/jcamdev/clanker.git
cd clanker
  1. Configure environment variables: Create a .env file in the project root (see Environment Configuration section below or or .env.example for details)

  2. Build and run with Docker Compose:

docker-compose up -d

The bot will automatically start and connect to Discord. To view logs:

docker-compose logs -f discord-bot

Docker Commands

  • 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

Option 2: Direct Python Installation

Prerequisites

  • Python 3.7 or higher (pref 3.11)
  • pip

Installation Steps

git clone https://github.com/jcamdev/clanker.git
cd clanker
pip install -r requirements.txt

Environment Configuration

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

3. Run the Bot

python discord_bot.py

You should see:

[Bot Username] has connected to Discord!
Bot is ready and listening for !ask commands

Usage

Basic 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

Command Differences

!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

Examples

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

Configuration Options

Customizing the System Prompt

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.

Memory Management

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

Contributing

Feel free to submit issues and enhancement requests!

About

LLM integrated discord bot

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors