Skip to content

Firstbober/ai-discord-bot

Repository files navigation

Discord Bot with AI Integration

A Discord bot powered by OpenRouter AI that can respond to mentions, follow up on conversations, and occasionally chime in randomly. The bot stores message history in SQLite and uses LLM models for intelligent responses.

Features

  • AI-powered responses using OpenRouter API
  • Message history tracking in SQLite database
  • Multiple response triggers:
    • Direct mentions
    • Follow-up conversations (30% chance)
    • Random interventions (1/17 chance)
  • Image recognition support
  • Custom emoji replacement
  • Message context from chat history

Prerequisites

  • Bun runtime
  • Discord Bot Token
  • OpenRouter API Key

Setup

1. Clone and Install

bun install

2. Configure Environment Variables

Copy the example environment file:

cp .env.example .env

Fill in the required values in .env:

DISCORD_TOKEN=your_discord_bot_token_here
OPENROUTER_TOKEN=your_openrouter_api_key_here
ACTIVITY=your_bot_status_message
MODEL=your_preferred_model (e.g., anthropic/claude-3.5-sonnet)
IMAGE_RECOGNITION_MODEL=your_image_model (e.g., anthropic/claude-3.5-sonnet)

Getting tokens:

3. Configure Custom Emojis

Copy and customize the emoji mappings:

cp emojis.example.ts emojis.ts

Edit emojis.ts to map text emoji codes to Discord emoji IDs:

export const Emojis = {
    ":NOOO:": "<:NOOO:1351971548436627528>",
    ":HAPPY:": "<:HAPPY:your_emoji_id_here>",
    // Add more custom emojis
}

4. Configure System Prompt

Copy and customize the AI system prompt:

cp system_prompt.example.ts system_prompt.ts

Edit system_prompt.ts to define your bot's personality and behavior:

export const SystemPrompt = `Your bot's personality and instructions here`;
export const ImageDescriptionPrompt = `Instructions for image analysis`;

Running the Bot

Development

bun run index.ts

Production

Consider using a process manager like PM2:

pm2 start "bun run index.ts" --name discord-bot

Database

The bot automatically creates a SQLite database (messages.sqlite3) on first run with the following schema:

  • message_id - Primary key
  • channel_id - Discord channel ID
  • content - Message content
  • attachment - Attachment data (currently unused)
  • author_id - User ID
  • created_timestamp - Message timestamp
  • content_length - Length of message

Bot Permissions Required

When inviting the bot, ensure these permissions are enabled:

  • Read Messages/View Channels
  • Send Messages
  • Send Messages in Threads
  • Read Message History
  • Use External Emojis
  • Add Reactions

License

This project is MIT Licensed

About

An AI discord bot using OpenRouter

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published