Skip to content

fluid-labs/discord-bot

Repository files navigation

FlowWeave Discord Monitor Bot

A Discord bot that monitors specific channels and sends notifications to subscribed users via Telegram using the existing FlowWeave Telegram bot infrastructure.

Features

  • 🔍 Channel Monitoring: Monitor specific Discord channels for new messages
  • 📢 Telegram Notifications: Send formatted notifications to Telegram users
  • 👥 User Subscriptions: Users can subscribe/unsubscribe to channel notifications
  • 🔐 Admin Controls: Admin-only commands for managing monitored channels
  • 📊 Database Integration: Persistent storage with PostgreSQL and Prisma
  • 🎯 Smart Filtering: Ignores bot messages, focuses on user content

Architecture

The bot integrates with the existing workflows system:

Discord Channel → Discord Bot → Database → Telegram Bot → User Notifications
  • Discord Service: Monitors channels and processes messages
  • Telegram Service: Formats and sends notifications via existing bot
  • Database: Stores channel monitors, subscriptions, and message history
  • Commands: Slash commands for user interaction

Setup Instructions

1. Prerequisites

  • Node.js 18+ installed
  • PostgreSQL database (reuses existing FlowWeave database)
  • Discord Application with Bot Token
  • Telegram Bot Token (from existing FlowWeave setup)

2. Discord Bot Setup

  1. Go to Discord Developer Portal
  2. Create a new application or use existing one
  3. Go to "Bot" section and create a bot
  4. Copy the bot token
  5. Enable these bot permissions:
    • Read Messages/View Channels
    • Send Messages
    • Use Slash Commands
    • Read Message History
  6. Copy the Application ID (Client ID)

3. Installation

# Navigate to the discord-bot-monitor directory
cd discord-bot-monitor

# Install dependencies
npm install

# Setup environment variables
cp .env.example .env
# Edit .env with your tokens

4. Environment Configuration

Update .env file with your credentials:

# Discord Bot Configuration
DISCORD_BOT_TOKEN=your_discord_bot_token_here
DISCORD_CLIENT_ID=your_discord_client_id_here

# Telegram Bot Configuration (from existing workflows)
TELEGRAM_BOT_TOKEN=your_telegram_bot_token

# Database Configuration (from existing workflows)
DATABASE_URL="your_postgresql_connection_string"

5. Database Setup

# Generate Prisma client
npm run prisma:generate

# Push database schema
npm run prisma:push

# Or run migrations
npm run prisma:migrate

6. Bot Invitation

  1. Go to Discord Developer Portal → OAuth2 → URL Generator
  2. Select scopes: bot and applications.commands
  3. Select bot permissions:
    • Read Messages/View Channels
    • Send Messages
    • Use Slash Commands
    • Read Message History
  4. Use the generated URL to invite the bot to your server

7. Running the Bot

# Development mode
npm run dev

# Production mode
npm run build
npm start

Usage

Admin Commands

/monitor <channel> - Start monitoring a channel for notifications

  • Only administrators can use this command
  • Adds the specified channel to the monitoring list

/list-monitors - List all monitored channels

  • Shows channel statistics and subscriber counts
  • Admin-only command

User Commands

/subscribe <channel> <telegram_username> - Subscribe to channel notifications

  • Channel must be monitored by an admin first
  • Provide your Telegram username without the @ symbol
  • You must have started a conversation with the FlowWeave Telegram bot

/unsubscribe <channel> - Unsubscribe from channel notifications

  • Removes your subscription from the specified channel

/list-subscriptions - List your current subscriptions

  • Shows all channels you're subscribed to

Notification Format

When a message is posted in a monitored channel, subscribers receive a formatted Telegram notification:

🔔 New Discord Message

🏰 Server: Your Server Name
📢 Channel: #announcements
👤 Author: Username
🕐 Time: 2024-01-15 10:30:00

Message:
Hello everyone! This is an important announcement.

📎 Attachments: 2 file(s)

🔗 View Message

Database Schema

The bot extends the existing database with Discord-specific tables:

  • DiscordChannelMonitor: Tracks monitored channels
  • DiscordSubscription: Manages user subscriptions
  • DiscordMessageRecord: Stores message history for analytics

Development

Available Scripts

npm run dev          # Start in development mode with hot reload
npm run build        # Build TypeScript to JavaScript
npm start           # Start production build
npm run watch       # Watch mode for development
npm run prisma:generate  # Generate Prisma client
npm run prisma:push     # Push schema to database
npm run prisma:migrate  # Run database migrations

Project Structure

discord-bot-monitor/
├── src/
│   ├── services/
│   │   ├── discord.service.ts     # Discord monitoring logic
│   │   └── telegram.service.ts    # Telegram notification service
│   ├── commands/
│   │   └── subscription.commands.ts  # Slash command handlers
│   ├── types/
│   │   └── discord.ts             # TypeScript interfaces
│   └── index.ts                   # Main bot entry point
├── prisma/
│   └── schema.prisma              # Database schema
├── package.json
├── tsconfig.json
├── .env                           # Environment variables
└── README.md

Troubleshooting

Common Issues

  1. "Channel is not being monitored"

    • Ask a server administrator to run /monitor on the channel first
  2. "No chat ID found for Telegram user"

    • Make sure you've started a conversation with the FlowWeave Telegram bot
    • Use the exact same username in both Discord and Telegram subscriptions
  3. Bot not responding to commands

    • Check bot permissions in Discord server
    • Verify the bot token is correct
    • Ensure slash commands are registered
  4. Database connection errors

    • Verify DATABASE_URL is correct
    • Check if the database is accessible
    • Run npm run prisma:generate and npm run prisma:push

Logs

The bot provides detailed logging for debugging:

# View logs in development
npm run dev

# For production, consider using PM2 or similar process manager
pm2 start dist/index.js --name "discord-monitor"
pm2 logs discord-monitor

Integration with FlowWeave

This Discord bot seamlessly integrates with the existing FlowWeave ecosystem:

  • Shared Database: Uses the same PostgreSQL database as the workflows system
  • Telegram Integration: Leverages the existing Telegram bot for notifications
  • Consistent Architecture: Follows the same patterns as other FlowWeave services

Security Considerations

  • Bot tokens are stored in environment variables
  • User permissions are checked for admin commands
  • Database queries use Prisma for SQL injection protection
  • Error handling prevents sensitive information leakage

Future Enhancements

  • Message Filtering: Filter by keywords, user roles, or message types
  • Scheduled Summaries: Daily/weekly digest of channel activity
  • Rich Embeds: Support for Discord embed forwarding
  • User Management: Bulk subscription management for admins
  • Analytics: Message statistics and engagement metrics

Support

For issues or questions:

  1. Check the troubleshooting section above
  2. Review the logs for error messages
  3. Ensure all environment variables are correctly set
  4. Verify database connectivity and schema is up to date

License

This project is part of the FlowWeave ecosystem and follows the same licensing terms.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published