Overview
The!play command is the core functionality of MusicBot. It downloads audio from YouTube and plays it in your Discord voice channel.
Basic Usage
Syntax
- Direct YouTube URLs - Play a specific video
- Search queries - Search YouTube and play the first result
YouTube URLs
Supported URL Formats
The bot detects URLs by checking if the input starts withhttp:// or https:// (bot.py:264):
Example Usage
Playlist Handling
Playlist URLs are not supported - the bot will only extract the first video:Search Queries
How Search Works
When you provide a non-URL input, the bot:- Sends a “Searching for…” message (bot.py:269)
- Calls the
search_youtube()function (bot.py:114-159) - Uses YouTube Data API v3 to find videos
- Returns the first matching video URL
- Proceeds to download that video
Search Implementation
From bot.py:129-135:Example Searches
Search Fallback
yt-dlp also has a built-in search capability as a fallback:Audio Download Process
Download Configuration
Audio is downloaded using yt-dlp with these settings (bot.py:67-78):Audio Quality
- Format: MP3
- Bitrate: 192 kbps
- Source: Best available audio stream from YouTube
Download Steps
The download process (bot.py:288-377):-
Extract video info without downloading
-
Check cache for existing file
-
Download if not cached
- Add to queue and start playback
Caching System
Per-Guild Cache Directories
Each Discord server has its own cache directory (bot.py:278-282):Cache Benefits
- Faster playback - No re-download for repeated songs
- Reduced bandwidth - Same video cached per server
- Reduced API calls - No repeated YouTube API requests
Cache Hit vs Cache Miss
Cache Hit (bot.py:315-319):Cache Naming
Files are named using YouTube video IDs:- Unique filenames per video
- No conflicts between different videos
- Easy identification of cached videos
Cache Persistence
Cached files are retained after playback (bot.py:560):Cache Cleanup
Cache is automatically cleared when:- Using
!leavecommand (deletes entire guild cache) - Using
!clearcachecommand (manual cleanup)
Voice Channel Connection
Prerequisites
Before playing music:-
User must be in a voice channel (bot.py:232-234):
-
Bot must not be in a different channel (bot.py:238-240):
Auto-Join
The bot automatically joins your channel (bot.py:243-259):Error Handling
Common Errors
No search query provided:Error Logging
All errors are logged with full details (bot.py:403-410):Playback Flow
- User invokes
!play <query> - Bot validates user is in voice channel
- Bot connects to voice channel if needed
- Query is processed (URL or search)
- Video info is extracted
- Cache is checked
- Audio is downloaded (if not cached)
- Song is added to queue
- Playback starts if nothing is playing
- Otherwise, song waits in queue
Performance Considerations
- First play: Requires download (may take 5-30 seconds depending on video length)
- Subsequent plays: Instant (from cache)
- Search queries: Add ~1-2 seconds for API lookup
- Direct URLs: Skip search, go straight to download
Tips
- Use direct URLs for faster, more accurate results
- Search queries return the first result - be specific
- Songs are cached automatically - no manual action needed
- Cache persists across bot restarts
- Each server has isolated cache
