A Discord music bot built with Python that can search, download, and play music from YouTube. The bot features intelligent music search using vector embeddings and supports queue management for seamless music playback.
- π΅ Music Search: Search for music by name or YouTube URL
- π― Intelligent Search: Uses vector embeddings for semantic music search
- π₯ Auto Download: Automatically downloads and caches music from YouTube
- π§ Queue Management: Add multiple songs to a queue
- β―οΈ Playback Controls: Play, pause, resume, skip, and stop functionality
- ποΈ Database Storage: MongoDB integration for music metadata and embeddings
- π Vector Search: MongoDB Atlas Vector Search for intelligent music discovery
- Python 3.12+ (managed with pyenv)
- FFmpeg (required for audio processing)
- MongoDB (with Atlas Vector Search support)
- Poetry (for dependency management)
sudo apt update
sudo apt install ffmpegbrew install ffmpegDownload from FFmpeg official website and add to PATH.
git clone <repository-url>
cd MusycInstall and configure pyenv (if not already installed):
# Install pyenv (Linux/macOS)
curl https://pyenv.run | bash
# Add to your shell profile (~/.bashrc, ~/.zshrc, etc.)
echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bashrc
echo 'command -v pyenv >/dev/null || export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bashrc
echo 'eval "$(pyenv init -)"' >> ~/.bashrc
# Reload shell
source ~/.bashrc
# Install Python 3.12
pyenv install 3.12.0
pyenv local 3.12.0# Install Poetry (if not already installed)
curl -sSL https://install.python-poetry.org | python3 -
# Install project dependencies
poetry install# Start MongoDB using the provided docker-compose
docker-compose up -d- Create a MongoDB Atlas account
- Create a new cluster
- Get your connection string
Create the required database and collection with vector search index:
- Database:
Musyc - Collection:
Musics - Vector Search Index:
- Index Name:
default - Path:
embeddings - Dimensions:
384 - Similarity:
dotProduct
- Index Name:
{
"fields": [
{
"type": "vector",
"path": "embeddings",
"numDimensions": 384,
"similarity": "dotProduct"
}
]
}Create a .env file in the project root:
# Discord Bot Configuration
DISCORD_BOT_KEY=your_discord_bot_token_here
# MongoDB Configuration
MONGODB_URI=mongodb://localhost:27019 # For local Docker setup
# MONGODB_URI=mongodb+srv://username:password@cluster.mongodb.net/ # For Atlas
# YouTube API Configuration
YOUTUBE_API_KEY=your_youtube_api_key_hereDiscord Bot Token:
- Go to Discord Developer Portal
- Create a new application
- Go to "Bot" section
- Create a bot and copy the token
YouTube API Key:
- Go to Google Cloud Console
- Create a new project or select existing one
- Enable YouTube Data API v3
- Create credentials (API Key)
- Restrict the key to YouTube Data API v3
# Using Poetry
poetry run python -m musyc
# Or using the installed script
poetry run musyc$play <music_name_or_url>- Play music by name or YouTube URL$pause- Pause the current music$resume- Resume paused music$stop- Stop music and disconnect from voice channel$skip- Skip to the next song in queue$queue- Show current music and queue
$play Never Gonna Give You Up
$play https://www.youtube.com/watch?v=dQw4w9WgXcQ
$queue
$skip
$pause
$resume
$stop
Musyc/
βββ src/
β βββ musyc/
β βββ __main__.py # Application entry point
β βββ discord/
β β βββ bot.py # Discord bot implementation
β βββ file_manager/
β β βββ database_manager.py # MongoDB operations
β β βββ vectorizer.py # Text embedding generation
β βββ ytb/
β βββ api.py # YouTube API integration
β βββ handler.py # YouTube URL/name handling
β βββ pytube.py # YouTube video info extraction
β βββ video_downloader.py # Video download and processing
βββ musics/ # Downloaded music cache
βββ docker-compose.yaml # MongoDB Docker setup
βββ pyproject.toml # Poetry configuration
βββ README.md
The project uses the following key dependencies:
- discord.py: Discord API wrapper
- sentence-transformers: Text embedding generation (all-MiniLM-L6-v2 model)
- pymongo: MongoDB driver
- yt-dlp: YouTube video downloading
- google-api-python-client: YouTube Data API
- python-dotenv: Environment variable management
- numpy: Numerical operations for embeddings
The bot uses the all-MiniLM-L6-v2 model for generating text embeddings. This model:
- Generates 384-dimensional embeddings
- Runs on CPU by default
- Is automatically downloaded on first use (~90MB)
- Music is downloaded as MP3 files (192kbps quality)
- FFmpeg is used for audio format conversion
- Audio is played at 48kHz, stereo, 128kbps bitrate
Each music document in MongoDB contains:
{
"_id": "uuid4_string",
"name": "Song Title",
"path": "/path/to/audio/file.mp3",
"url": "https://youtube.com/watch?v=...",
"video_id": "youtube_video_id",
"embeddings": [0.1, 0.2, ...] // 384-dimensional vector
}- FFmpeg not found: Ensure FFmpeg is installed and in your PATH
- MongoDB connection failed: Check your MONGODB_URI and ensure MongoDB is running
- Discord bot not responding: Verify DISCORD_BOT_KEY is correct and bot has proper permissions
- YouTube API quota exceeded: Check your YouTube API quota in Google Cloud Console
Ensure your Discord bot has the following permissions:
- Send Messages
- Connect to Voice Channels
- Speak in Voice Channels
- Use Slash Commands (if applicable)
This project is licensed under the MIT License.
For issues and questions, please open an issue on the GitHub repository.