Overview
Rosy Music Bot is built on a modular architecture using Discord.js and DisTube for music playback. The system follows an event-driven design pattern with clear separation between command handling, event processing, and utility functions.Core Architecture
Bot Client Setup
The Discord client is initialized inindex.js with required gateway intents:
index.js:10-17
Guilds- Access to guild informationGuildVoiceStates- Monitor voice channel connectionsGuildMessages- Receive message eventsMessageContent- Read message content for prefix commands
DisTube Integration
DisTube handles all music playback functionality with plugin support:index.js:24-36
- SpotifyPlugin - Enables Spotify URL support and playlist imports
- YtDlpPlugin - YouTube download and streaming with auto-updates
Event System
The bot uses a dual event system for Discord client events and DisTube music events.Client Events
Located in/events/client/, these handle Discord interactions:
- interactionCreate.js - Processes button clicks for music controls
- messageCreate.js - Handles prefix commands (r!play, r!skip, etc.)
handlers/events.js:6-17
DisTube Events
Located in/events/distube/, these handle music playback:
- playSong.js - Triggered when a song starts playing
- error.js - Handles playback errors
- debug.js - Logs debug information
- setup.js - Initializes DisTube configuration and connection handling
events/distube/playSong.js:7-26
Command Handling Flow
Command Registration
Commands are automatically loaded from/commands/music/ by the command handler:
handlers/commands.js:4-23
Command Execution
Prefix-based commands are processed inindex.js:
index.js:52-68
Command Structure
All commands follow this structure:Voice Connection Handling
Connection Validation
Commands validate voice channel permissions before execution:commands/music/play.js:10-29
Voice State Monitoring
The bot monitors voice connection states to handle reconnections:events/distube/setup.js:19-28
Architecture Diagram
Key Design Patterns
Modular Event System
Events are separated by source (client vs. DisTube) and loaded dynamically, allowing easy addition of new event handlers.Command Pattern
Each command is a self-contained module with name, description, and execute function.Factory Pattern
Utility modules likeembeds.js use factory functions to create consistent Discord embeds.
Observer Pattern
Progress updater observes queue state changes and updates embeds every 10 seconds.Configuration
Bot configuration is centralized inconfig.js:
config.js
.env) store sensitive data:
TOKEN- Discord bot tokenSPOTIFY_CLIENT_ID- Spotify API client IDSPOTIFY_CLIENT_SECRET- Spotify API client secret
Performance Considerations
Event Listener Management
The bot increases the max listener limit to prevent memory warnings:index.js:8
Progress Update Throttling
Embed updates are throttled to 10-second intervals to avoid API rate limits:utils/progressUpdater.js:12
Memory Management
Active progress updaters are tracked and cleaned up when queues stop to prevent memory leaks:utils/progressUpdater.js:43-50