Jukebox (bot side)
This page is generated from the project corpus and rewritten on every docs build. Edit the source in corpus/, not here.
The bot plays atrium's music in voice channels. Atrium owns every Player (its
D57); the bot is a speaker that atrium steers. Terms are in
glossary.md; the call is in decisions.md ("Music
comes from atrium"). Code: bots/discord/src/jukebox/.
Pieces
atrium/: logs in to atrium as the Bot account withPOST /auth/loginand calls it with theatrium_sessioncookie, kept in memory only. A 401 logs in again once (single-flight) and retries; a renewedSet-Cookiereplaces the kept one; a refused password is tried once only (atrium's five-failure lockout).session.tsholds the session,request.tsthe client,client.tsthe singleton. Errors are typed:NotConfigured,SignInFailed,Forbiddenstop;Unavailableretries;RequestErrorcarries atrium's{error}code.link.ts: takes the command cursor once, reports each guild idle, then holdsGET /jukebox/bot/commands?wait=20open (30 s timeout). Backs off 1 s doubling to 30 s; stops for good on a permanent error. Reports status on every change and every 10 s while playing.player.ts: one per guild. Commands run in order, so aplayafter ajoinwaits for the connection. Applies aplayonly if itsplayIdis newer (the first after boot always). When a Track ends it asksadvance; if atrium is down it retries with backoff, outside the command chain, and drops the retry once a newer play, a stop or a leave arrives. It asks once per play, for the play the speaker was sounding when the event fired (2026-11-02): a broken file gives the voice library'serrorand then Idle, and two advances skipped the next song and, the second sayingended, reset atrium's failure count.buffer.ts: per guild in$TMPDIR/jukebox/<guildId>/. ffmpeg converts each MP3 to Ogg Opus once (-c:a libopus -b:a 96k -ar 48000 -ac 2 -f ogg), fed through stdin so the session cookie never reaches its command line. Keeps the current Track and the next two, never more than three files. A Track not yet buffered plays from a live pipe. Wiped at boot, cleared at shutdown. A song that fails to buffer is not fetched again while it stays next, for up to 5 minutes (2026-11-02): every status answer replans the buffer, every 10 s while playing, and one bad file used to be refetched and logged that often.alone.ts: nobody but bots in the channel →control: pause, thenleaveafter 10 minutes; someone returning cancels the leave and stays paused. Both go through atrium, so the Player keeps one writer.speaker.ts: the voice side behind aSpeakerinterface.DiscordSpeakerwraps@discordjs/voice(self-deafened,StreamType.OggOpus, no Opus encoder needed);discordHost(client)routes voice-state and channel events. The link and player see only the interface, which is how they were run against atrium without Discord. A join not ready within 20 s is destroyed and the player reports no voice channel (2026-11-02); before that, the half-open connection still read as connected and a play went to a player nobody heard.
Atrium's rules
Atrium's status rules, read from atrium's jukebox.service.ts on 2026-11-02
(built 2026-10-09, changed 2026-10-27; atrium's jukebox.md has the why):
- A poll without
aftertells atrium the bot restarted: every playing or paused Player goes idle, its Queue kept. So the cursor is taken once, at ready, and a reconnect keeps the one it has. - A status report carries state and position only for the current
playId, and only while atrium has not stopped that play. A report about an older play changes just voice and liveness. - An idle report for the current play waits for the
advance. It only stamps the play; with noadvancewithin 5 s the Player goes idle the next time anything reads it. - Lost voice takes two reports. No voice channel on the current play in
two reports running idles the Player; one is taken for a voice reconnect.
A
playthat arrives with no voice connection is reported idle with thatplayId, and the play ends by one of these two rules. - A control that starts a song needs a voice channel:
play,resume,next,previousandplayTrackanswer 409NOT_IN_VOICEwhile the Player has none.joinsets the channel at once, so/jukebox playjoins and then plays without waiting for the bot. - Five failed songs in a row stop the Player. Each
advancewithreason: errorcounts, anendedone resets the count, and the fifth goes idle with the Player'sfailuretext, which the next play clears. advancewith an oldplayIdanswersstale(a newerplayis coming); on a Player already idle it answers{play: null}.- The long-poll's maximum
waitis 20 s, held through atrium's dev proxy and container on 2026-10-08.
types.ts and format.ts's PlayerView restate atrium's shapes by hand.
Atrium's packages/shared/src/jukebox.ts is the source; check it, not this
page, when they might have drifted.
Triage
A healthy image's generateDependencyReport() (2026-10-09) shows
@discordjs/voice 0.19.2, prism-media 1.3.5, native aes-256-gcm,
@snazzah/davey 0.1.12 and FFmpeg 8.1.2 with libopus. "Opus Libraries: not
found" is expected. Log lines, all scope [jukebox]:
- Not configured:
Jukebox not configured (no JUKEBOX_* variables); the link is off. - Old variables:
JUKEBOX_WARD_… no longer read (brief 29): not renamed yet. - Sign-in failed:
Jukebox sign-in failed: atrium refused the sign-in for JUKEBOX_ATRIUM_USERNAME=… (401 invalid_credentials)…, thenJukebox link stopped. Nothing retries until a restart. - Forbidden:
Jukebox link stopped: atrium refused the Bot account (JUKEBOX_ROLE_FORBIDDEN); check its atrium user has the jukebox role. - Atrium unreachable:
atrium unreachable while waiting for commands (…),Jukebox link retrying in N s, thenJukebox link back. Behind the dev proxy a stopped atrium shows as "answered 500". - Each Track:
playing "<title>" in <guild> (live|buffered).
npm run discord:jukebox-check tests the sign-in on its own. Its session is in
memory, so it can run beside the bot.
The command
/jukebox (commands/jukebox.ts) is the second remote; atrium's
page is the first. Every subcommand calls atrium and changes nothing locally.
Each call gets 2.5 s; a reply pending at 1.5 s is deferred ephemerally, so
errors stay ephemeral and controls still answer publicly. On that slow path
the placeholder is filled in with editReply before the public followUp
(2026-11-02): Discord fills a still-loading deferred reply with the first
follow-up and keeps it ephemeral, and the placeholder's deletion then took
the answer with it. Reply text and the
autocomplete filter are in jukebox/format.ts. play (when it joins) and
join first check the member's channel with discord.js's joinable and
speakable and refuse a stage channel, since atrium's page only offers
channels with Connect and Speak and the Discord path had no such filter.
Owed: hearing it in Discord
Not run yet. The deploy pushes the local bots/discord/.env when there is one,
so production's token must never be used for a local run. The local copy is
production's (restored by the owner on 2026-11-02), so a local run needs the
dev application's values passed on the command line, which beat dotenv. With
a dev application, a dev guild, npm run discord:register and npm run discord:dev against the local atrium:
- Join puts the bot in the channel; a cold start is audible within 2 s; a buffered next Track starts with no gap over 1 s.
- Pause, resume, stop, next and previous are heard; a Track that ends advances.
- Everyone leaves → the Player pauses. Shorten
LEAVE_AFTER_MSlocally to see the leave, and don't commit that. - SIGTERM leaves the channel visibly.
- Every
/jukeboxsubcommand, a song added from Discord showing on atrium's page with the Discord name, and/helplisting it under Music. - Production: deploy with
node cli.ts just-a-bot deployfromvps-deployonce atrium's Jukebox and its own accounts are deployed and itsjukeboxuser is seeded (the owner's steps are in the 2026-10-13 log entry). The deploy registers the slash commands.
Before 2026-10-13: the Ward sign-in
From 2026-10-09 the Bot account was a Ward account (discord-bot), with a
password login and Ward's rotating refresh token saved in
data/jukebox-session.json. Atrium stopped reading Ward's cookie on
2026-10-13, so the bot moved to atrium's own login; the spent file is deleted
at boot. Two Ward facts seen live on 2026-10-08, in case anything signs in to
Ward this way again: accessTokenExpiresAt comes as epoch seconds and
refreshTokenExpiresAt as an ISO string, and a rotated refresh token used
twice (two processes sharing the file) gets the session revoked. The
not-before time after a 429 or a network failure survives in today's
session.ts.