Files
kingston 26a0728e14 docs: move cog documentation into the wiki and stub the README
The root README carried the full ten cog manual while the wiki held the
authoritative pages. Point the repository at the wiki, keep only install and
catalog basics in the README, and remove the per cog documents that are now
folded into wiki source under wiki/.
2026-11-03 12:54:42 -06:00

7.7 KiB

YTAudio

Plays YouTube audio in voice channels through a Lavalink node. The cog resolves a query with yt-dlp, streams the audio into a growing file, and asks Lavalink's local source to play that file while yt-dlp keeps filling it.

YTAudio also owns the process-wide Lavalink client for the whole bot. TtsToy depends on that connection.

Requirements

  • A Lavalink node on localhost:2333 with password youshallnotpass, defined in the source as LL_HOST, LL_PORT, LL_PASSWORD.
  • The local source enabled on the node.
  • A shared filesystem between the bot and the node, because Lavalink opens a path the bot creates.
  • yt-dlp installed in the bot's Python environment, plus ffmpeg on the PATH for the fallback conversion.
  • Python 3.9 or newer for asyncio usage, though the bot baseline is 3.11.

Commands

All commands are guild only and none of them have a permission check. Any member can control playback, change the volume, or disconnect the bot.

Command Aliases Arguments Behavior
[p]play none <query> Joins your voice channel and queues a URL or search
[p]skip none none Skips the current track
[p]stop none none Stops playback and clears the queue
[p]pause none none Pauses the current track
[p]resume none none Resumes playback
[p]queue q none Shows the current track and what is next
[p]nowplaying np none Shows the current track
[p]volume none <0-150> Sets and saves the guild volume
[p]summon ytjoin none Joins your voice channel
[p]ytleave ytdc none Stops, disconnects and clears state

play accepts a full URL, a www. address, or a search phrase. Searches are resolved as ytsearch1:<query>, so the first result is used. Playlists are disabled by yt-dlp options.

When something is already playing, play replies with the queue position. When the queue was empty, the track starts immediately and progress is reported through status messages in the invoking channel.

volume clamps to 0 through 150 and stores the value per guild. It applies to the active player and to the next track.

skip aborts any in-flight resolution for the current track by setting its skip event, then stops the Lavalink player. The track end event advances the queue, so the cog does not signal the queue directly, which would skip two tracks.

stop and ytleave also clear pending tracks and delete their temporary files. ytleave additionally disconnects the Lavalink player.

queue truncates its output at 1900 characters.

Playback pipeline

  1. play ensures the bot has a Lavalink player and is connected to the author's voice channel. If the bot is in a different channel, the player is moved rather than reconnected.
  2. A QueuedTrack is appended to the guild's pending list. The player loop wakes.
  3. The loop takes the first pending track, sets it as current, and calls _play_track.
  4. _play_track posts "Loading: " and sets the guild volume.
  5. The streaming path resolves metadata with yt-dlp using download=False, which returns the direct media URL and title without downloading, subject to a 30 second resolver timeout.
  6. A regular file is created under the cog data directory, and python -m yt_dlp -f bestaudio/best -o - <target> is started with its stdout writing directly into that file. The file must be a regular file because this Lavalink build's local loader rejects FIFOs.
  7. The cog waits until the file reaches 64 KiB or the process exits, up to 15 seconds. A process that exits early counts as a stream failure.
  8. Lavalink loads the path through its local source. On success the track plays while yt-dlp continues to write.
  9. When the track ends, is skipped, or fails, the yt-dlp process is killed and the file is deleted.

The stream path avoids Lavalink's own YouTube source and avoids a full download before playback. Tracks generally start within a few seconds, and a skip is immediate because it only requires killing a process.

Fallback path

If streaming fails, the cog edits the status to "Stream unavailable, downloading: " and downloads the audio through yt-dlp with an FFmpegExtractAudio postprocessor at 192 kbps, writing <video id>.mp3 into mp3cache. Progress hooks edit the status message at most once per second and only when the whole percent changes. The mp3 is then played through the same local source.

If the fallback also fails, the status becomes "Failed to load: " and the queue advances.

Queue model

Each guild has a GuildPlayer with:

Field Meaning
pending Tracks waiting, index 0 plays next
current The track playing now, or None
volume Cached guild volume
task The player loop task
next_event Set when the current track should end
wakeup Set when a track is added

Commands read pending and current directly, so queue and nowplaying reflect real state rather than a separate model.

Lavalink track end, exception and stuck events set next_event for the guild. That is the only signal that moves the loop to the next track.

On setup, start_up_task creates a background task that waits for Red to be ready, then:

  1. Checks lavalink.get_all_nodes(). If a node is already connected, it marks itself ready and shares it.
  2. Otherwise calls lavalink.initialize with a 30 second timeout, up to five attempts, waiting two seconds between timeouts.
  3. Registers a track event listener on success.

On unload it cancels the connect task, cancels every player loop, unregisters the listener, closes the Lavalink client, kills spawned yt-dlp processes, and deletes and recreates mp3cache and streams.

Because the client is a process singleton, only one cog should call initialize and close. If the built-in Audio cog is loaded and already connected, YTAudio shares that node. If YTAudio is unloaded while TtsToy is still loaded, TtsToy loses its player connection.

Interaction with TtsToy

TtsToy plays generated audio through the same Lavalink player. Its queue processor pauses music, saves the current track, position and queue, clears the queue, plays the TTS clip, then restores the queue and seeks back to the saved position.

That restoration relies on YTAudio's queue objects being present in the Lavalink player when TTS finishes. A track added during a TTS clip is appended to the player, not to YTAudio's pending list, so it can be lost when the saved state is restored.

Limits and failure modes

  • No permission checks, as noted above.
  • The queue is in memory. A reload or restart loses pending tracks.
  • Age restricted, members only, or region blocked videos fail in both paths and report "Failed to load".
  • yt-dlp must be current. Sites change their player frequently and an old binary fails to resolve.
  • The Lavalink node must be able to read the path written by the bot. Containers and separate hosts break the streaming path.
  • Downloads and streams count against disk space until the track ends or the cog unloads.
  • The resolver timeout is 30 seconds; a slow search is treated as a skip.
  • A play invoked while the bot is not connected still requires the author to be in a voice channel.
  • Volume 0 does not pause playback; the player keeps decoding silently.

Configuration and stored data

Guild config under identifier 0x5954415544494F holds one key, volume, defaulting to 100.

Generated files:

  • <cog data>/mp3cache for fallback downloads, named by video id.
  • <cog data>/streams for in-progress streams, named by UUID with the source extension.

Both are wiped on unload. No user data is stored. The Lavalink password is in the source and should be treated as public within the deployment.