Self-taught Projects

Discord Music Bot

TypeScriptBunDiscord.jsFly.ioFFmpegDockerGitHub ActionsDependabot

A lightweight, personal-use Discord music bot designed for cost-efficient deployment and optimized performance on minimal server resources.

  • Hosting: Fly.io (Legacy Hobby plan, Singapore)
  • Resources: Shared CPU (x1) + 256MB RAM
  • Cost: ~$1.40/month (effectively free since Fly.io waives costs under $5/month for legacy users)

The bot's architecture is highly optimized, ensuring smooth operation within strict resource limits.

Optimization Highlights:

  • OpenSSL: Provides secure TLS/SSL connections required for encrypted audio streaming.
  • Opus: High-performance audio codec for low-latency, high-quality voice transmission in Discord.
  • Sodium: Cryptographic library used for authentication and secure voice communication.
  • FFmpeg: Custom-built from source, stripped to essential configs for efficient streaming.
  • Alpine: Ultra-lightweight base image utilizing multi-stage builds for optimized production deployment.
    • Base image: alpine:x.x ~12.79 MB
    • Compiled executable (bytecode): ~121.1 MB (via bun build --compile --bytecode ...)
      • Bun runtime: ~104.82 MB
      • N-API modules:
        • DAVE (Discord Audio & Video E2E Encryption): ~1.7 MB
        • Opus: ~0.58 MB
      • App size: 1 MB
      • Precompiled bytecode: ~8 MB
    • FFmpeg size: ~17.5 MB (via ./configure --disable-everything ... && make install)
  • Custom patches: Implement static-analysis–friendly modifications for the Bun compiler.
    • Substitute dynamic NAPI imports from prism-media with a static import of opus.node
    • Replace the dynamic detection of FFmpeg sources from prism-media with a static ffmpeg command
    • Eliminate the ws dependency from @discordjs/voice and @discordjs/ws by using Bun's native WebSocket

Development History:

  1. ~2018: Cloned MusicBot for quick launch locally
  2. ~2021: Write own bot in Discord.js and Distube
  3. ~2022: Migrated to TypeScript, Dockerized, added Dependabot, and deployed to Fly.io
  4. ~2023: Gradual migration to Bun; tweaking custom FFmpeg builds to fix OOM issues caused by npm-installed FFmpeg binary
  5. Late 2024: Fully stable on Bun. The final build is significantly reduced from ~300 MB to 1 MB
  6. Mid 2025: Deployed to the home server due to some music providers banned the Fly.io IPs
  7. Late 2025: Further optimization through custom patches, compiling with Bun, and using a highly stripped-down FFmpeg build (--disable-everything)