How stryp0/mio works
Project Type and Purpose
• This is a standalone Discord music bot (“Mio”) written in TypeScript.
• It listens for chat commands in Discord text channels, fetches or downloads audio from YouTube, maintains an in-memory song queue, and streams audio into Discord voice channels.
File Structure Analysis
• src/index.ts
• Entry point: initializes Discord client, loads .env config via ConfigHandler, and registers the MessageHandler.
• src/commands/*.ts
• One file per user command (play, pause, skip, queue, shuffle, etc.).
• Implements a Command Pattern: each command exports metadata (name, args) + an execute() function.
• src/handlers/*.ts
• CommandHandler: maps incoming messages to the correct command module.
• MessageHandler: listens to Discord “message” events, pre-processes them and delegates to CommandHandler.
• ConfigHandler: reads environment variables (.env) and exposes typed config.
• DownloadHandler: interfaces with YouTube Data API + ytdl-core to fetch audio streams / manage caching.
• PlaybackHandler: opens FFmpeg processes, connects to Discord voice, and pushes audio data.
• QueueHandler: in-memory queue management (add, remove, shuffle, list).
• MetaHandler: generates metadata for embeds (duration, title, thumbnail).
• UIHandler: builds and sends Discord embed messages (song info, queue previews).
• Configuration files:
• .env.example / .gitignore (secure tokens)
• tsconfig.json, package.json (build scripts, dependencies)
Architecture from README
• Uses Discord Bot Token & YouTube Data API Key.
• Core features: queue system, playlist support, embedded UI.
a. Main Components
• Discord Gateway (discord.js client)
• Bot Application (Node.js process)
• MessageHandler
• CommandHandler
• ConfigHandler
• DownloadHandler ↔ YouTube Data API
• QueueHandler
• PlaybackHandler ↔ FFmpeg
• UIHandler / MetaHandler
• External Services
• YouTube Data API (search, metadata)
• YouTube audio streams (via ytdl-core)
• Discord Voice Server (for audio streaming)
b. Relationships & Data Flow
1. User types !play <url> → Discord Gateway emits “messageCreate”
2. MessageHandler parses prefix & passes command name + args to CommandHandler
3. CommandHandler invokes the corresponding command module
4. Command module requests DownloadHandler (for metadata or stream URL)
5. DownloadHandler calls YouTube Data API and/or ytdl-core, returns a readable audio stream
6. Command module enqueues item via QueueHandler
7. PlaybackHandler pulls next stream from QueueHandler, spawns FFmpeg to transcode if needed
8. PlaybackHandler pipes PCM data into Discord Voice Connection
9. UIHandler/MetaHandler send embed updates to the text channel
c. Architectural Patterns & Principles
• Event-Driven: Discord events drive the whole flow
• Command Pattern: decouple command definition from invocation logic
• Single Responsibility: each Handler has a well-defined role
• In-Memory State: queue lives in process memory (could be externalized later)
d. Key Technologies
• Node.js + TypeScript
• discord.js library (Discord Gateway & Voice)
• ytdl-core (audio stream fetching)
• FFmpeg (audio decoding/transcoding)
• dotenv (ConfigHandler)
• YouTube Data API v3