Architecture
How the framework works end to end, following one !play from chat to sound.
The big picture
The whole system in one picture. A session token is captured once from the official client and stored as ROOT_TOKEN. The Bot polls chat through the grpc-web API, dispatches commands through middleware to handlers, and replies in the same channel. For the music example, a handler resolves audio through a Lavalink sidecar, ffmpeg decodes it to PCM, and the Player publishes it into a LiveKit room joined with a SessionCreate call.
There is no Root client process, no browser, and no official bot API involved at runtime, just HTTPS calls with a captured token, a Lavalink sidecar, and the LiveKit SDK.
Auth: Bearer token over grpc-web
Every API call is a POST to https://api.rootapp.com/root.<Service>/<Method> with:
Authorization: Bearer <ROOT_TOKEN>, the alt's session token, captured once from the real client (see Token Capture). Same model as Discord token login.Content-Type: application/grpc-web, framed with the standard 5-byte grpc-web prefix.User-Agent: grpc-dotnet/2.85.0, matching the desktop client.
Bodies are hand-encoded protobuf. Root identifies communities, channels, users, and messages with GUIDs, encoded as a two-fixed64 message (each half is the little-endian storage of the big-endian u64 of that GUID half). rootbot.adapters.root.wire implements just enough protobuf and grpc-web framing for all of this, there is no protobuf dependency.
Two protocol quirks the library handles for you:
- Omit the context field. Requests that include the
RootContextfield makeSessionCreateandMessageCreatereturn an empty error. The library never sends it. - Errors can ride in headers. A
MessageListlimit outside 10–50 returns HTTP 200, an empty body, andgrpc-status: 3in the response headers. The library clamps the limit to 10–50.
Discovery: ListMine + GetExtended
At startup (and every 60 s after), serve discovers where the bot lives:
CommunityGrpcService/ListMinewith an empty request returns every community the account is a member of (user_communities[]→ community id + name).- For each community,
CommunityGrpcService/GetExtendedreturns all channel groups and channels: id, name, and type (1 = text, 4 = voice). A channel's id is its messagecontainer_id, the same GUID addresses its chat and, for voice channels, its voice room. - If
ListMinereturns nothing, serve falls back to thecommunitieslist inconfig.jsonand logsdiscovery=config.
New channels are primed (their history marked seen) so old messages never replay as commands; removed channels are unwatched.
Chat: polling List
Realtime chat delivery rides the client's hub socket, which the framework does not implement, polling is the read path:
ChatWatcher.poll()callsv2.MessageGrpcService/Listper watched container (direction "before now", newest first, limit 20).- Messages carry no
created_at; ids are time-ordered, so a seen-ID set primed at startup is enough to yield only fresh messages. - The watcher drops the bot's own messages, empty content, and anything that isn't a command, text starting with
!, plus bare single digits (search-picker replies). - Each message is tagged with its community id, so replies, presence checks, and voice joins all happen in the community the command came from.
- Replies go out via
MessageGrpcService/Create(content as aStringValuewrapper) into the same container the command arrived in.
Poll interval in serve is ~2 s.
Voice: SessionCreate → LiveKit
Joining a voice channel is a single API call:
POST /root.WebRtcGrpcService/SessionCreate
field 10: community_id (GUID message)
field 11: container_id (GUID message)
field 13: supports_v2 = 1The response carries the backend selector, a LiveKit server URL (wss://…livekit.cloud), and a short-lived LiveKit JWT (~15 min) with publish rights for microphone/camera/screen. The bot ignores the backend selector, observed joins land on LiveKit, and hands URL + JWT to the official LiveKit Python SDK, joining the room as a normal participant and publishing a mic-source audio track.
Follow-me: before joining, serve resolves the target channel with the read-only WebRtcGrpcService/List presence call, for each voice channel in the command's community, it lists current members; the channel containing the command author's user id wins. Nobody in voice → !play is refused with "Join a voice channel first".
The in-voice gate: while the bot is in a voice channel, commands are accepted only from current members of that channel (checked with the same presence call). If the presence call itself errors, the gate fails open rather than locking everyone out.
Audio: Lavalink → ffmpeg → PCM frames
For a track, the player picks a source:
- Lavalink first (YouTube URLs):
GET /youtube/stream/<videoId>on the local Lavalink, authenticated with its password. Lavalink's youtube-source plugin fetched real audio via the TV client + OAuth - the route that survives datacenter IPs (see Lavalink). - yt-dlp fallback (non-YouTube URLs, or no Lavalink password):
yt-dlp -f bestaudiopiped the same way. Expect YouTube through this path to fail from a server IP.
Either source's stdout feeds ffmpeg, which decodes to raw PCM (s16le, 48 kHz, mono). Player.play_process reads exactly one 20 ms frame (960 samples) at a time and pushes it into the LiveKit AudioSource with capture_frame.
Pause and volume mechanics
- Pause is implemented at the frame pump:
play_processsimply stops reading the ffmpeg pipe and stops callingcapture_frame. ffmpeg and the source process block on their full pipes, so nothing is lost and nothing desyncs, resume continues at the exact sample. Position tracking is a monotonic clock minus accumulated paused time, so!npprogress stays honest across pauses. - Volume is per-frame PCM gain: samples are multiplied by
volume / 100with int16 clamping (scale_pcm). It applies live, mid-track, and persists across tracks for the serve session.
Leaving
When the queue drains, serve schedules an idle auto-leave after 300 s instead of dropping immediately; a new !play cancels the timer and reuses the connection. !stop sets a flag that makes the playback loop leave as soon as the current track is torn down, and also suppresses autoplay.