Skip to content

Latest commit

ย 

History

History
130 lines (106 loc) ยท 4.29 KB

File metadata and controls

130 lines (106 loc) ยท 4.29 KB

๐ŸŽต Discord Musicbot

A self-hosted Discord music bot powered by yt-dlp with slash commands and Docker support.

Features

  • Multi-Platform โ€” YouTube, SoundCloud, Bandcamp, Twitch, Vimeo and all yt-dlp sites
  • Search & Select โ€” /play <query> shows 10 results, /select <nr> picks one
  • Playlist Support โ€” YouTube playlists with background downloading and progress display
  • Now Playing UI โ€” Interactive embed with โฎ๏ธโฏ๏ธโญ๏ธ๐Ÿ”‰๐Ÿ”Š buttons
  • Audio Cache โ€” LRU file cache with configurable size, persisted to disk
  • Local Mapping Playback โ€” /playchrist queues local audio files from /mapping/christ
  • Repeat & Shuffle โ€” Song loop, queue loop, shuffle mode
  • Rate Limiting โ€” 10 downloads/user/minute
  • Graceful Shutdown โ€” Clean voice disconnects on container stop

Setup

1. Create a Bot Token

  1. Go to Discord Developer Portal โ†’ New Application
  2. Create a Bot โ†’ copy the token
  3. OAuth2 โ†’ URL Generator: Scopes bot + applications.commands, Permissions: Connect, Speak, Send Messages
  4. Invite the bot to your server

2. Configure

cp .env.example .env

Edit .env โ€” at minimum set TOKEN:

TOKEN=your_bot_token
MAX_CACHE=200
DOWNLOAD_TIMEOUT_SEC=120
LOG_LEVEL=info
DOWNLOAD_HOST_PATH=./downloads
MAPPING_HOST_PATH=./mapping/christ

3. Run (Docker)

docker compose up -d --build

View logs:

docker compose logs -f musicbot

yt-dlp is automatically updated on every container start and checked every 6 hours.

Commands

Command Description
/play <query/url> Play a song, playlist or start a search
/select <1-10> Pick a search result
/pause Pause playback
/resume Resume playback
/skip Skip current song
/stop Stop playback and clear queue
/queue Show current queue
/volume <0-100> Set volume
/shuffle Toggle shuffle mode
/repeatsingle Repeat current song
/repeat Repeat entire queue
/playcache Play all cached songs
/playchrist Play all audio files from /mapping/christ
/leave Disconnect bot from voice
/debug Show debug info
/clearcache Clear audio cache (Admin)
/refresh Re-register slash commands (Admin)

Architecture

src/
โ”œโ”€โ”€ index.js                 # Entry point, slash commands, events
โ”œโ”€โ”€ commands/
โ”‚   โ””โ”€โ”€ commandHandlers.js   # All command handlers
โ”œโ”€โ”€ queue/
โ”‚   โ””โ”€โ”€ QueueManager.js      # Queue & playback (ffmpeg PCM buffering)
โ”œโ”€โ”€ download/
โ”‚   โ”œโ”€โ”€ ytdlp.js             # yt-dlp wrapper
โ”‚   โ”œโ”€โ”€ BackgroundDownloader.js
โ”‚   โ”œโ”€โ”€ ProgressManager.js
โ”‚   โ””โ”€โ”€ RateLimiter.js
โ”œโ”€โ”€ cache/
โ”‚   โ”œโ”€โ”€ AudioCache.js        # LRU file cache
โ”‚   โ””โ”€โ”€ SearchCache.js
โ”œโ”€โ”€ voice/
โ”‚   โ””โ”€โ”€ VoiceManager.js      # Voice join with retry
โ”œโ”€โ”€ utils/
โ”‚   โ”œโ”€โ”€ validation.js        # URL/input security
โ”‚   โ”œโ”€โ”€ urlCleaner.js         # YouTube URL parsing
โ”‚   โ”œโ”€โ”€ formatting.js
โ”‚   โ””โ”€โ”€ logger.js             # Winston logger
โ””โ”€โ”€ config/
    โ””โ”€โ”€ constants.js          # All configuration values

Security

  • URL validation against SSRF (localhost, private IPs, file://)
  • Shell injection prevented (shell: false on all spawns)
  • Input sanitization for all user inputs
  • Per-user rate limiting
  • yt-dlp runs with nice -n 19 (low CPU priority)

Environment Variables

Variable Default Description
TOKEN โ€” Discord bot token (required)
MAX_CACHE 200 Max number of cached audio files
DOWNLOAD_TIMEOUT_SEC 120 Download timeout in seconds
SEARCH_TIMEOUT_SEC 30 Search timeout in seconds
LOG_LEVEL info Log level (debug, info, warn, error)
DOWNLOAD_HOST_PATH ./downloads Host path for audio cache
MAPPING_HOST_PATH ./mapping/christ Host path for local audio files mounted for /playchrist
MAPPING_DIR /mapping/christ Container/local path read by /playchrist
LOGS_HOST_PATH ./logs Host path for log files

License

MIT