A Discord music bot built on discord.js v14, Shoukaku and Lavalink. It plays YouTube tracks in voice channels and shuffles a local music library, and it runs around the clock on an ARM single-board computer under pm2.
The bot never touches audio. Lavalink is a separate Java server that finds tracks, decodes them, encodes them to Opus and streams them straight to Discord's voice servers. The bot sends Lavalink commands and keeps the queue and the Now Playing message up to date. docs/architecture.md explains how the pieces fit together.
| Command | What it does |
|---|---|
/play <url> |
Queues a YouTube URL, or the first search result for some keywords. Playback starts if nothing is playing. A playlist URL queues only its first track. |
/clear |
Removes every upcoming track. The current track keeps playing. |
/playfav [language] |
Queues 80 random tracks from the local library. language limits the pick to one category: Chinese, English, Japanese, Korean, Other or Piano. Without it, everything except Piano can be picked. |
While a track plays, the bot keeps one Now Playing message in the text channel where playback started:
- It shows the title with its YouTube link, a thumbnail, a progress bar that updates every 5 seconds, and a footer with the queue length, the loop mode and who requested the track.
- It is sent silently, so it does not notify anyone.
- When other messages push it up, the bot deletes it and posts it again at the bottom, so the controls stay in reach.
| Control | What it does |
|---|---|
| Heart | Saves the track to the local library, or removes it (see below). |
| Restart | Jumps back to the start of the track. Disabled for live streams. |
| Pause / Resume | Pauses or resumes. The embed turns orange and reads Paused. |
| Skip | Starts the next track. |
| Loop | Cycles through off (grey), the current track (blue) and the whole queue (green). |
| Queue menu | Jumps straight to any of the next 25 tracks. |
/clear, the buttons and the queue menu only respond to members in the bot's voice channel. The heart instead depends on LIBRARY_OWNER_IDS.
- The library is a folder (
MUSIC_LIBRARY_DIR). Every audio file in it, subfolders included, belongs to it. Supported extensions:.mp3 .flac .wav .ogg .opus .m4a .mp4 .aac .webm .mka .mkv. - Files named
Title [videoId].extget their YouTube link and thumbnail from that ID. For other files, the bot searches YouTube in the background for a cover and caches the result indata/library-meta.json. - Categories are labels set by hand in
data/library-meta.json: an array of codes (zh,en,ja,ko,other,piano) per file. A file can have several. An unlabelled file gets a guess from the characters in its title.pianois opt-in: it plays only with/playfav language:Piano, never mixed in with songs that have lyrics. - The heart is limited to the users in
LIBRARY_OWNER_IDS:- On a YouTube track that is not in the library, it downloads the audio with yt-dlp into the library.
- On a track that is already in the library, it deletes the file. Pressing it again downloads the file back, but only if its name contains a video ID.
- The bot leaves the voice channel 30 seconds after the queue runs out, or 30 seconds after the last person leaves.
- If the connection to Lavalink is lost for about 60 seconds, the bot exits and pm2 starts a fresh process.
- Node.js 18 or newer (deployed on 24)
- Java 17 or newer, for Lavalink (deployed on 21)
- Lavalink 4 (deployed on 4.2.2) with the youtube-source plugin
- Python 3, for yt-dlp. Only the heart's downloads need it.
- pm2 (recommended)
- A Discord application with a bot user:
- Invite it with the
botandapplications.commandsscopes. - Give it the View Channels, Send Messages, Embed Links, Connect and Speak permissions.
- No privileged intents are needed.
- Invite it with the
-
Install the dependencies.
npm ci
-
Create
.envfrom the template, then fill in the values (see Environment variables).cp .env.example .env
-
Set up Lavalink in
lavalink/:-
Put
Lavalink.jarinlavalink/. -
Put the youtube-source plugin jar in
lavalink/plugins/. This deployment uses snapshot2be8e542frommaven.lavalink.dev/snapshots, because releases up to 1.18.2 fail on YouTube's TV client. -
Create the live config from the template, and set
lavalink.server.passwordto the same value asLAVALINK_PASSWORD:cp lavalink/application.example.yml lavalink/application.yml
-
YouTube OAuth, preferably with a spare Google account:
- On the first start, Lavalink logs a URL and a code. Open the URL and enter the code.
- Lavalink then logs a refresh token. Put it in
plugins.youtube.oauth.refreshToken, so later starts do not ask again.
lavalink/application.ymlholds secrets and is gitignored. -
-
Download yt-dlp. Only the heart's downloads need it.
mkdir -p bin curl -L https://github.com/yt-dlp/yt-dlp/releases/latest/download/yt-dlp -o bin/yt-dlp
When downloads start failing, update it with
python3 bin/yt-dlp -U. -
Upload the button icons, if you use your own Discord application. The buttons use application emojis, and
assets/icons/emoji-ids.jsonholds the IDs that belong to the original application. To upload the icons to yours and rewrite that file:node scripts/icons/upload-emojis.mjs
-
Start both processes. pm2 runs Lavalink and the bot, both defined in
ecosystem.config.js:pm2 start ecosystem.config.js pm2 logs bot
- Expected log lines:
Logged in as …,Slash commands registered …(once per guild), andNode "main" connected and ready. - Start order: Lavalink takes about 20 seconds to boot. If the bot connects before it is up,
/playanswersMusic service is not ready yetuntil the watchdog restarts the bot. - Without pm2: run
./lavalink/start.sh, thennpm startin another terminal.
- Expected log lines:
src/config.js reads .env from the project root. A variable already set in the environment wins over .env.
| Variable | Default | If missing or wrong |
|---|---|---|
DISCORD_TOKEN |
none | The bot exits at start with [FATAL] DISCORD_TOKEN is not set. |
GUILD_ID |
empty | Commands are registered globally and can take up to an hour to appear. Separate several guild IDs with commas. |
LAVALINK_HOST, LAVALINK_PORT |
localhost, 2333 |
The bot never connects, restarts about every 60 seconds, and /play answers that the service is not ready. |
LAVALINK_PASSWORD |
youshallnotpass |
Same as above if it differs from lavalink.server.password in lavalink/application.yml. |
LAVALINK_SECURE |
false |
true connects over wss://. |
MUSIC_LIBRARY_DIR |
library/ in the project root |
The folder /playfav plays. The bot never creates it. If it is missing, /playfav and the heart reply that it cannot be found. |
LIBRARY_OWNER_IDS |
empty | Discord user IDs allowed to use the heart, separated by commas. If empty, nobody can. |
lavalink/application.example.yml explains each setting. These are the ones that matter for this bot:
plugins.youtube: YouTube playback goes through the youtube-source plugin. Lavalink's built-in YouTube source is turned off.- Client order: Opus-capable clients come first. Clients that only offer AAC cost an extra 44.1 to 48 kHz resample.
sources.local: true: this lets Lavalink play files from the local library.remoteCipher: points at a public signature-deciphering service. Without it, YouTube playback stops working whenever YouTube changes its player.- Low encoding quality and large buffers, on purpose: a Discord voice channel caps at about 92 kbps, so higher quality is inaudible and only costs CPU.
- Volume: the player volume is 50 (
DEFAULT_VOLUMEinsrc/config.js), so every track is decoded and re-encoded. At 100, Lavalink could pass Opus sources through unchanged.
Discord-Music-Bot/
├── index.js entry point for pm2 and npm start; loads src/index.js
├── src/
│ ├── index.js Discord client, event routing, login
│ ├── commands.js slash-command definitions and registration
│ ├── lavalink.js Shoukaku setup and the connection watchdog
│ ├── config.js every adjustable setting; the only reader of .env
│ ├── constants.js fixed values shared across files (button IDs, …)
│ ├── library.js local library: scanning, covers, categories, downloads
│ ├── ui.js Now Playing embed, buttons and queue menu
│ ├── utils.js time formatting and the progress bar
│ └── music/
│ ├── MusicManager.js playback: queues, commands, buttons, player events
│ └── GuildQueue.js per-server playback state
├── lavalink/
│ ├── start.sh starts Lavalink.jar
│ └── application.example.yml Lavalink config template (copy to application.yml)
├── assets/icons/ button icons (SVG, PNG) and their emoji IDs
├── scripts/
│ ├── icons/ build the icon PNGs, upload them as emojis
│ ├── diagnostics/ playback checks against a running Lavalink
│ └── lib/env.mjs shared helpers for the scripts
├── docs/architecture.md how the bot works inside
├── data/ runtime data (library-meta.json), not committed
├── ecosystem.config.js pm2 definitions for lavalink and bot
└── .env.example environment variable template
Not committed: .env, lavalink/application.yml, lavalink/*.jar, lavalink/plugins/, bin/yt-dlp, data/* and node_modules/.
- Playlists: a playlist URL queues only its first track.
- The heart deletes files for real. A file whose name has no
[videoId]cannot be downloaded back. - The remote cipher is a third-party service with no uptime guarantee. If YouTube playback breaks, check it first.
- Unresolved audio glitch: some tracks that YouTube only serves as AAC have a short skip that sounds like a momentary speed-up. Changing the quality or buffer settings did not fix it.
- No automated tests. Changes are checked with syntax and load checks, offline scripts, and by hand in Discord.