Last Music Bot

A self-hosted Discord music bot with a React dashboard. Slash commands drive the same player the web UI does, with Lavalink handling audio and Spotify supplying autoplay recommendations.

Open Source Self-hosted TypeScript / Node 24 discord.js / Lavalink AGPL-3.0
GitHub Repo

Overview

Last Music Bot is two halves of one product: a Discord bot and a React dashboard that control the same player. Whether you type /play in a channel or click a track in the browser, the request lands in the same player service. The dashboard is not a read-only mirror of the bot; it is a second front end to it.

Audio is delegated to Lavalink, so the bot process never touches an audio stream; it sends the node a track and receives events back. That is what lets a single instance serve several guilds without turning into an encoding server.

It is designed to be self-hosted. You run it, you own the data, and the whole stack comes up under Docker.

Key Features

  • Two Front Ends Slash commands and a React dashboard drive the same player service, so the two never disagree about what is playing.
  • Lavalink Audio Playback is handled by a Lavalink node over the standard client, keeping the bot process free of audio work.
  • Spotify Autoplay When the queue runs dry the bot keeps going, choosing related tracks rather than repeating the last one.
  • Playlist Import Pull existing playlists in from Spotify and YouTube instead of rebuilding them by hand.
  • Persistent Library Playlists, likes, history, and per-guild settings live in SQLite and survive restarts.
  • Drag-and-Drop Queue Reorder the queue from the dashboard, on desktop or touch.
  • Audited Actions Actions are written to an append-only JSONL log, so who-did-what is answerable after the fact.
  • Scheduled Backups A backup service snapshots the databases on a loop, with a restore path that does not involve improvising.
  • One-Command Stack Bot, dashboard, Lavalink, and the cipher helper all come up together under Docker Compose.
  • Tested Unit, smoke, and end-to-end suites cover the player, the services, and the command handlers.

Slash Commands

The command surface is deliberately small. Anything beyond transport control belongs in the dashboard, where there is room for it.

Command What it does
/play Queue a track, playlist, or search. Joins your voice channel if the bot is not already in one.
/pause · /resume Hold and release playback without losing the queue.
/skip Advance to the next track. With autoplay on, an empty queue is refilled rather than stopped.
/connect · /disconnect Move the bot into or out of a voice channel explicitly.
/control Bring up the interactive player controls in-channel.
/admin Owner-side maintenance. Not for general use.

Architecture & Stack

Discord and the dashboard are two doors into one player service. Below it, the work fans out: Lavalink for audio, the recommendation engine for autoplay, and SQLite for anything that must survive a restart. Click a node to jump to the section that documents it.

Discord

Slash commands · voice

React Dashboard

Vite · queue · library

discord.js Client

Command handlers

REST API

Express · JWT · zod

PlayerService

Queue · state · events

Lavalink

Audio node · LavaSrc

Recommendation

Autoplay · ANN index

SQLite Store

Library · player state

yt-cipher

Signature helper

Spotify Catalog

Related tracks · import

Audit & Backup

JSONL log · snapshots

The Containers

Four services come up together. Splitting them apart is what allows a redeploy of the bot to leave audio playback untouched.

Service Role
bot discord.js client, the REST API, and the player service.
ui The React dashboard, built by Vite.
lavalink The audio node. Does the actual streaming and encoding.
yt-cipher Signature helper Lavalink needs to keep resolving YouTube sources.
Repo layout
.
├── apps/bot/          # discord.js client, REST API, player, services, tests
│   └── src/
│       ├── api/           # Express route registration
│       ├── discord/       # Slash commands
│       ├── player/        # PlayerService - the one source of truth
│       ├── lavalink/      # Audio node client
│       ├── recommendation/# Autoplay + ANN index
│       ├── spotify/       # Auth, catalog, playlist import
│       ├── youtube/       # Playlist import
│       ├── store/         # SQLite stores + repositories
│       ├── audit/         # Append-only JSONL logger
│       └── backup/        # Scheduled snapshots
├── src/               # React dashboard (production UI)
├── lavalink/          # Lavalink node config
└── docker-compose.local.yml

Lavalink & the Audio Pipeline

The bot never touches an audio stream. It sends a Lavalink node a track and gets events back, and the node does the streaming, decoding, and mixing. That separation is the reason a redeploy of the bot does not interrupt playback: the audio container keeps running.

Almost every built-in source is switched off

Lavalink ships with source managers for YouTube, Bandcamp, Twitch, Vimeo and more. Nearly all of them are disabled here, and only http is left on. That looks like a mistake until you see the plugin list: the built-ins are turned off because the plugins replace them, and running both would mean two code paths competing to resolve the same URL.

Plugin What it is for
youtube-plugin Handles YouTube resolution, in place of the built-in source manager.
LavaSrc Adds Spotify as a catalog. See below: it does not do what most people assume.

Spotify is a catalog, not a stream

This is the part worth understanding. Spotify audio is never played. Spotify is used to identify the music; the audio is then sourced from YouTube. LavaSrc resolves each Spotify track by trying, in order:

LavaSrc resolution order
providers:
  - "ytsearch:%ISRC%"     # 1. match the exact recording
  - "ytsearch:%QUERY%"    # 2. fall back to artist + title text

The ISRC is the recording's global identifier, so searching by it finds that recording, not a cover, a live take, or a remaster that happens to share the title. Only when a track has no usable ISRC does it fall back to a plain text search, which is the path that occasionally lands on the wrong version. That two-step order is the whole reason Spotify-sourced playback usually gets the right take.

Surviving YouTube

YouTube is a moving target, and two pieces of the config exist purely to cope with that.

The YouTube plugin is configured to rotate through several client identities: MUSIC, ANDROID_VR, WEB, WEBEMBEDDED, and TV. They break at different times and under different conditions, so if one client stops resolving, another is likely to still work. It is redundancy, not cleverness.

The second piece is yt-cipher, which is why there is a fourth container in the stack that otherwise looks unnecessary. YouTube protects stream URLs with a signature computed by JavaScript that changes regularly. Rather than making the audio node run that JavaScript itself, the node delegates signature work to a small dedicated service over the local network. Keeping it separate means the thing that breaks most often is also the thing you can update or restart on its own.

Tuning

Setting Value Why
Frame buffer 10 s, non-allocating Absorbs network jitter without churning memory per player.
Buffer duration 1 s Keeps the gap between a command and hearing it short.
Playlist load limit 250 tracks A ceiling on how much one /play of a huge playlist can queue.
Spotify page limit 6 pages Bounds how far an album or playlist import will page through the API.

Dashboard & REST API

The dashboard is a Vite + React 18 app. It holds no player logic of its own; it calls the same API the bot exposes, which is why the queue you drag in the browser is the queue Discord is playing.

The API is Express, authenticated with JWT, and validates request bodies with zod so a malformed payload is rejected at the edge rather than halfway through a state change. Routes are grouped by concern:

Route group Covers
Auth Sign-in and session issuing for the dashboard.
Playback Play, pause, skip, seek, volume, queue order.
Library Playlists, likes, and listening history.
Guild Per-server settings and state.
Spotify Account linking, catalog lookups, playlist import.
Admin Owner-only maintenance operations.

Library, Storage & Backups

State is split across two SQLite databases, and the split is deliberate: library.db holds what you want to keep (playlists, likes, history, settings, linked accounts), while player.db holds what is merely true right now. Losing the second is an inconvenience; losing the first is a loss.

Each concern gets its own repository rather than sharing one god-object over the connection, and schema changes run through an explicit migration path instead of being applied by hand.

Two safety nets sit underneath. Actions are appended to a JSONL audit log, which is append-only and therefore still trustworthy after something has gone wrong. And a backup service snapshots the databases on a schedule, with a restore command as a first-class script rather than an improvised copy.

Autoplay & Recommendations

An empty queue is where most music bots stop. This one keeps going: when the last track ends, the recommendation service picks what follows.

Rather than asking an API for "related tracks" every time, the bot maintains its own approximate nearest-neighbour index over a track index it builds up. Candidates are drawn from that index and enriched through the Spotify catalog, which keeps suggestions relevant to what has actually been playing instead of drifting into whatever is globally popular.

Playlists can be imported wholesale from Spotify (private, public, or embedded) and from YouTube, so an existing collection does not have to be rebuilt track by track.

Self-Hosting

The whole stack is Docker Compose. Node 24 or newer is required if you intend to run the bot outside a container.

Bring the stack up
# Fill in your own configuration first
cp .env.example .env

npm run dev:up:build     # first run: builds the bot and UI images
npm run dev:up           # thereafter
npm run dev:logs         # tail the bot
npm run dev:down         # tear it down

Configuration is entirely environment-driven: the same image runs locally and in production, with no code branching between them. .env.example documents every key the stack reads. Your credentials stay yours: they are gitignored, and the deploy path is built so that they are never uploaded from a developer machine.

You will need your own credentials. A Discord application for the bot, and Spotify API credentials if you want autoplay and playlist import. There is no shared or hosted instance to borrow. That is what self-hosted means, and it is why the data stays on your machine.

Terms & License

Last Music Bot is open source under the GNU Affero General Public License v3.0. The Affero clause matters here: if you run a modified version as a network service that other people use, you must offer those users the source of your modifications. Running it unmodified for your own server carries no such obligation.

Lavalink, discord.js, and the other dependencies remain under their own licenses. Discord, Spotify, and YouTube are trademarks of their respective owners; this project is not affiliated with any of them, and you are responsible for complying with their terms of service.