Running a Discord music bot from your computer means it goes offline when you shut down. A VPS keeps your bot live 24/7 for your community.
Bot Framework Choices
discord.js + @discordjs/voice (Node.js)
Most popular for JavaScript developers:
const { Client, GatewayIntentBits } = require('discord.js');
const { joinVoiceChannel, createAudioPlayer, createAudioResource } = require('@discordjs/voice');
const client = new Client({
intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildVoiceStates]
});
discord.py + wavelink (Python)
Python alternative with good audio library support:
import discord
import wavelink
class MusicBot(discord.Bot):
async def on_ready(self):
print(f'Bot ready: {self.user}')
Lavalink (Java)
Dedicated audio server that handles audio processing separately from your bot. Best for bots serving many servers simultaneously.
Node.js Music Bot Setup
Install Dependencies
npm init -y
npm install discord.js @discordjs/voice @discordjs/opus sodium-native ytdl-core
Basic Bot Structure
const { Client, GatewayIntentBits } = require('discord.js');
const { joinVoiceChannel, createAudioPlayer, createAudioResource, AudioPlayerStatus } = require('@discordjs/voice');
const ytdl = require('ytdl-core');
const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildVoiceStates,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent
]
});
const queue = new Map();
client.on('interactionCreate', async interaction => {
if (!interaction.isChatInputCommand()) return;
if (interaction.commandName === 'play') {
const channel = interaction.member.voice.channel;
if (!channel) {
return interaction.reply('Join a voice channel first!');
}
const url = interaction.options.getString('url');
const connection = joinVoiceChannel({
channelId: channel.id,
guildId: interaction.guildId,
adapterCreator: interaction.guild.voiceAdapterCreator
});
const player = createAudioPlayer();
const stream = ytdl(url, { filter: 'audioonly', quality: 'highestaudio' });
const resource = createAudioResource(stream);
player.play(resource);
connection.subscribe(player);
await interaction.reply('Now playing!');
}
});
client.login('YOUR_BOT_TOKEN');
Deployment on VPS
Process Management with PM2
npm install -g pm2
pm2 start bot.js --name "music-bot"
pm2 startup # Auto-start on reboot
pm2 save # Save process list
systemd Alternative
Create /etc/systemd/system/music-bot.service:
[Unit]
Description=Discord Music Bot
After=network.target
[Service]
Type=simple
User=botuser
WorkingDirectory=/home/botuser/music-bot
ExecStart=/usr/bin/node bot.js
Restart=always
RestartSec=10
Environment=NODE_ENV=production
[Install]
WantedBy=multi-user.target
Resource Requirements
| Scale | CPU | RAM | Plan | |-------|-----|-----|------| | 1-5 servers | 1 core | 512MB-1GB | Discord Bot Small (Free) | | 5-20 servers | 1 core | 1-2GB | Discord Bot Middle (€3/6mo) | | 20-100 servers | 2 cores | 2-4GB | Discord Bot Large (€6/6mo) | | 100+ servers | VPS | 4-8GB | VPS plan |
Space-Node's Discord Bot hosting starts free and scales with your bot's growth.
Audio Quality Tips
Opus Encoding
Discord uses Opus codec. Install native Opus for best quality:
npm install @discordjs/opus
# or
npm install opusscript # Fallback (slower)
Native @discordjs/opus uses significantly less CPU than the JavaScript fallback.
Buffer Management
Audio stuttering indicates buffer underrun:
const resource = createAudioResource(stream, {
inlineVolume: true,
inputType: StreamType.Arbitrary
});
resource.volume.setVolume(0.5);
Memory Management
Music bots can leak memory through unreleased audio streams:
player.on(AudioPlayerStatus.Idle, () => {
// Clean up stream resources
connection.destroy();
});
Lavalink for Scale
If your bot serves 50+ servers, Lavalink offloads audio processing:
- Run Lavalink server (Java) on the VPS
- Your bot sends commands to Lavalink
- Lavalink handles audio streaming
- Much lower bot RAM/CPU usage
This separates concerns: your bot handles Discord logic, Lavalink handles audio.
