Introduction
Music bots are among the most popular Discord bots. They let server members listen to music together, creating a shared experience. Building one yourself is a great learning project.
In this guide, you will learn how to create a Discord music bot using Lavalink for audio processing. We will cover the architecture, setup, and basic commands.
Understanding the Architecture
A music bot has two main components. Your Discord bot handles commands and user interaction. Lavalink handles audio fetching, decoding, and streaming.
This separation is important. Audio processing is CPU intensive. By offloading it to Lavalink, your bot stays responsive even when playing music in many servers.
Prerequisites
You need Node.js 18 or higher installed, a Discord bot application created in the Discord Developer Portal, a Lavalink server (you can use HeavenCloud Lavalink Hosting), and basic JavaScript knowledge.
Setting Up Your Project
Create a new directory for your project and initialize it with npm. Install the required packages including discord.js for the Discord API and a Lavalink client like Shoukaku or Erela.js.
Create your main bot file and set up the basic Discord client. Configure the intents your bot needs including Guilds, GuildVoiceStates, and GuildMessages.
Connecting to Lavalink
Configure your Lavalink client with the server details. You need the host address, port, and password. If using HeavenCloud Lavalink Hosting, you receive these after purchase.
Initialize the Lavalink client when your bot starts. Handle connection events to know when Lavalink is ready.
Basic Commands
Play Command
The play command is the core feature. It takes a song query, searches for the track, joins the voice channel, and starts playback.
First check if the user is in a voice channel. Then search for the track using Lavalink. Create a player for the guild if one does not exist. Add the track to the queue and start playing if nothing is currently playing.
Skip Command
The skip command stops the current track and plays the next one in queue. Check if there is an active player and if music is playing before skipping.
Stop Command
The stop command clears the queue and disconnects from voice. Destroy the player to clean up resources.
Queue Command
Show users what tracks are coming up. Display the current track and the next several tracks in queue.
Pause and Resume
Simple commands to pause and resume playback. Toggle the player pause state.
Handling Events
Listen for track events from Lavalink. When a track ends, automatically play the next track in queue. When a track has an error, notify the user and skip to the next track.
Handle voice state updates to detect when everyone leaves the voice channel. Disconnect the bot after a timeout to save resources.
Audio Sources
Lavalink supports multiple audio sources. YouTube is the most common. Spotify requires additional configuration but works well. SoundCloud, Bandcamp, and direct HTTP links also work.
Configure your Lavalink server to enable the sources you need. Some sources require plugins.
Error Handling
Music bots face many potential errors. Tracks might be unavailable, voice connections can drop, and Lavalink might disconnect. Handle these gracefully.
Implement reconnection logic for Lavalink. If the connection drops, attempt to reconnect automatically. Notify users when issues occur.
Scaling Considerations
As your bot grows, you need to think about scaling. One Lavalink server can handle many guilds, but eventually you need multiple nodes.
Implement load balancing across Lavalink nodes. Distribute players based on region or load. HeavenCloud offers multiple Lavalink plans for scaling.
Hosting Your Music Bot
Your music bot needs reliable hosting to run continuously. HeavenCloud offers both Discord bot hosting for your main bot and Lavalink hosting for audio processing.
The combination provides a complete music bot infrastructure. Bot hosting starts at 29 rupees per month and Lavalink hosting starts at 99 rupees per month.
Conclusion
Building a Discord music bot teaches you about audio streaming, API integration, and bot architecture. With Lavalink handling audio, you can focus on creating a great user experience.
Start with basic commands and expand from there. Add features like playlists, volume control, and search suggestions as you learn.
Ready to deploy? Check out HeavenCloud hosting for both your bot and Lavalink server.

