Skip to main content
Back to Blog
TutorialJanuary 5, 202512 min read

Building a Discord Music Bot from Scratch

Create a fully functional music bot with queue management, filters, and more. Works with YouTube and Spotify.

Emily Zhang

Emily Zhang

Developer Advocate

Building a Discord Music Bot from Scratch

Building a Discord Music Bot from Scratch

Music bots are among the most popular Discord bots. Let's build one with modern features.

Prerequisites

  • Node.js 18+
  • A NexusHost account
  • Discord Bot Token

Project Setup

bash
npm init -y
npm install discord.js @discordjs/voice @distube/ytdl-core

Basic Structure

javascript
const { Client, GatewayIntentBits } = require('discord.js');
const { joinVoiceChannel, createAudioPlayer } = require('@discordjs/voice');

const client = new Client({
  intents: [
    GatewayIntentBits.Guilds,
    GatewayIntentBits.GuildVoiceStates
  ]
});

// Player setup
const player = createAudioPlayer();

client.login(process.env.DISCORD_TOKEN);

Adding Commands

/play command

javascript
const playCommand = {
  name: 'play',
  description: 'Play a song',
  options: [{
    name: 'query',
    description: 'Song name or URL',
    type: 3,
    required: true
  }]
};

Queue Management

Implement a proper queue system for seamless playback:

  • Add to queue
  • Skip current song
  • View queue
  • Clear queue
  • Shuffle

Deploying

  1. Add your DISCORD_TOKEN to NexusHost environment variables
  2. Push to GitHub
  3. Your music bot is live!

Performance Tips

For the best audio quality on NexusHost:

  • Use Tier 2 or higher for music bots (more RAM for audio buffers)
  • Implement proper voice connection handling
  • Add reconnection logic for stability

Conclusion

Building a music bot is a great way to learn Discord.js. Host it on NexusHost for reliable 24/7 operation.

Related Articles