loading...

Writing a music command for your discord.js bot (Updated March 2020)

galnir profile image Nir Updated on ・8 min read

If you need a guide on how to set up a bot, please read this

Discord is a popular voice and chat app for gamers and non-gamers who use it to communicate with their friends without paying for a voice server like before.

Almost every big server has a Discord bot, which can be used for managing it, administrative commands like ban and kick, gifs and playing music. So why pay for a music bot when you can create one for free?

No time or energy to read this guide? Just clone my music bot from Github, follow the Prerequisites section, hit npm install and you'll have a working music bot!

This guide assumes you have a basic Discord bot that uses Commando already set up. If the bot does not use discord.js-commando, I highly recommend you to read this guide as Commando makes your life so much easier, and it's required for this guide.

The code for the music command is available here.

Prerequisites

Make sure ffmpeg, python 2.7, and Node(v12 atleast!) are installed.
Obtain a Youtube API key here.

Installing packages

Let's install the packages we're going to work with:
npm:

npm install discordjs/discord.js discordjs/Commando ffmpeg-static node-opus simple-youtube-api ytdl-core

yarn:

yarn add discordjs/discord.js discordjs/Commando ffmpeg-static node-opus simple-youtube-api ytdl-core@latest

Index.js (your main file)

Before we get to play.js, we need to extend the 'Guild' class so we could add a property that will hold our song queue. That will allow the bot to play music at multiple servers at a time.
To do that we'll import 'Structures' at the top of index.js, and use it to extend the 'Guild' class:

// your index.js should look similar to this:
const { CommandoClient } = require('discord.js-commando');
const { Structures } = require('discord.js');
const path = require('path');
const { prefix, token } = require('./config.json');
// It's vital this is before the initiation of the client
Structures.extend('Guild', Guild => {
  class MusicGuild extends Guild {
    constructor(client, data) {
      super(client, data);
      this.musicData = {
        queue: [],
        isPlaying: false,
        volume: 1,
        songDispatcher: null
      };
    }
  }
  return MusicGuild;
});
const client = new CommandoClient({
  commandPrefix: prefix,
  owner: 'your-discord-user-id',
  unknownCommandResponse: false
});

client.registry
  .registerDefaultTypes()
  .registerGroups([
    ['music', 'Music Command Group']
  ])
  .registerDefaultGroups()
  .registerDefaultCommands()
  .registerCommandsIn(path.join(__dirname, 'commands'));

client.once('ready', () => {
  console.log('Ready!');
});

client.login(token);

play.js

In your 'commands' folder, create a folder named music and inside it create play.js.
We'll start with importing packages and our Youtube API key:

const { Command } = require('discord.js-commando');
const { MessageEmbed } = require('discord.js');
const Youtube = require('simple-youtube-api');
const ytdl = require('ytdl-core');
const { youtubeAPI } = require('../../config.json');
const youtube = new Youtube(youtubeAPI);

Next we'll declare the 'PlayCommand' class which extends 'Command':

module.exports = class PlayCommand extends Command {
  constructor(client) {
    super(client, {
      name: 'play', 
      memberName: 'play',
      group: 'music', // this means the folder the file is inside
      description: 'Play any song or playlist from youtube',
      guildOnly: true, // make this command available only in servers not dm's
      clientPermissions: ['SPEAK', 'CONNECT'],
      args: [
        {
          key: 'query', // here we name the variable that will hold the input
          prompt: 'What song would you like to listen to?', // send this msg if
          // the user hasn't provided any arg or if the arg was not a string
          type: 'string',
          validate: query => query.length > 0 && query.length < 200 
        }
      ]
    });
  }

Every command starts with the run method(the code you want the bot to run when the command is used):

  async run(message, { query }) {
    // don't let users run this command if they are not in a voice channel
    var voiceChannel = message.member.voice.channel;
    if (!voiceChannel) return message.say('Join a channel and try again');

Users have 3 options when running this command:

  1. Run it with a song name
  2. Run it with a Youtube URL(any kind of URL)
  3. Run it with a Youtube playlist URL

For example:

!play Darude Sandstorm
!play https://www.youtube.com/watch?v=y6120QOlsfU (and other url kinds)
!play https://www.youtube.com/playlist?list=PLuUrokoVSxlfUJuJB_D8j_wsFR4exaEmy

In order to do that we will write an if statement that checks against regex for any type of a Youtube URL. If the input matches the regex, we will apply different logic than the one we will apply on queries by song names.

First of all check if query is a playlist URL:

    if (
      query.match(
        /^(?!.*\?.*\bv=)https:\/\/www\.youtube\.com\/.*\?.*\blist=.*$/
      )
    ) {
      try {
        const playlist = await youtube.getPlaylist(query); // get playlist data 
        const videosObj = await playlist.getVideos(); // songs data object
        //const videos = Object.entries(videosObj); // turn the object to array
        // iterate through the videos array and make a song object out of each vid
        for (let i = 0; i < videosObj.length; i++) { 
          const video = await videosObj[i].fetch();

          const url = `https://www.youtube.com/watch?v=${video.raw.id}`;
          const title = video.raw.snippet.title;
          let duration = this.formatDuration(video.duration);
          const thumbnail = video.thumbnails.high.url;
          if (duration == '00:00') duration = 'Live Stream';
          const song = {
            url,
            title,
            duration,
            thumbnail,
            voiceChannel
          };

          message.guild.musicData.queue.push(song); // if you remember, the queue lives in the guild object so each server has its own queue

        }
        if (message.guild.musicData.isPlaying == false) { // if nothing is playing
          message.guild.musicData.isPlaying = true;
          return this.playSong(message.guild.musicData.queue, message); // play the playlist
        } else if (message.guild.musicData.isPlaying == true) { // if something is already playing
          return message.say(
            `Playlist - :musical_note:  ${playlist.title} :musical_note: has been added to queue`
          );
        }
      } catch (err) {
        console.error(err);
        return message.say('Playlist is either private or it does not exist');
      }
    }

Youtube URL regex:

    if (query.match(/^(http(s)?:\/\/)?((w){3}.)?youtu(be|.be)?(\.com)?\/.+/)) {
      const url = query; // temp variable
      try {
        query = query
          .replace(/(>|<)/gi, '')
          .split(/(vi\/|v=|\/v\/|youtu\.be\/|\/embed\/)/);
        const id = query[2].split(/[^0-9a-z_\-]/i)[0];
        const video = await youtube.getVideoByID(id);
        const title = video.title;
        let duration = this.formatDuration(video.duration);
        const thumbnail = video.thumbnails.high.url;
        if (duration == '00:00') duration = 'Live Stream';
        const song = {
          url,
          title,
          duration,
          thumbnail,
          voiceChannel
        };
        message.guild.musicData.queue.push(song);
        if (
          message.guild.musicData.isPlaying == false ||
          typeof message.guild.musicData.isPlaying == 'undefined'
        ) {
          message.guild.musicData.isPlaying = true;
          return this.playSong(message.guild.musicData.queue, message);
        } else if (message.guild.musicData.isPlaying == true) {
          return message.say(`${song.title} added to queue`);
        }
      } catch (err) {
        console.error(err);
        return message.say('Something went wrong, please try again later');
      }
    }

If the user has entered a song name as an argument:

    try {
      // search for the song and get 5 results back
      const videos = await youtube.searchVideos(query, 5);
      if (videos.length < 5) {
        return message.say(
          `I had some trouble finding what you were looking for, please try again or be more specific`
        );
      }
      const vidNameArr = [];
      // create an array that contains the result titles
      for (let i = 0; i < videos.length; i++) {
        vidNameArr.push(`${i + 1}: ${videos[i].title}`);
      }
      vidNameArr.push('exit'); // push 'exit' string as it will be an option
      // create and display an embed which will present the user the 5 results
      // so he can choose his desired result
      const embed = new MessageEmbed()
        .setColor('#e9f931')
        .setTitle('Choose a song by commenting a number between 1 and 5')
        .addField('Song 1', vidNameArr[0])
        .addField('Song 2', vidNameArr[1])
        .addField('Song 3', vidNameArr[2])
        .addField('Song 4', vidNameArr[3])
        .addField('Song 5', vidNameArr[4])
        .addField('Exit', 'exit'); // user can reply with 'exit' if none matches
      var songEmbed = await message.say({ embed });
      try {
        // wait 1 minute for the user's response
        var response = await message.channel.awaitMessages(
          msg => (msg.content > 0 && msg.content < 6) || msg.content === 'exit',
          {
            max: 1,
            maxProcessed: 1,
            time: 60000,
            errors: ['time']
          }
        );
        // assign videoIndex to user's response
        var videoIndex = parseInt(response.first().content);
      } catch (err) {
        console.error(err);
        songEmbed.delete();
        return message.say(
          'Please try again and enter a number between 1 and 5 or exit'
        );
      }
      // if the user responded with 'exit', cancel the command
      if (response.first().content === 'exit') return songEmbed.delete();
      try {
        // get video data from the API
        var video = await youtube.getVideoByID(videos[videoIndex - 1].id);
      } catch (err) {
        console.error(err);
        songEmbed.delete();
        return message.say(
          'An error has occured when trying to get the video ID from youtube'
        );
      }
      const url = `https://www.youtube.com/watch?v=${video.raw.id}`;
      const title = video.title;
      let duration = this.formatDuration(video.duration);
      const thumbnail = video.thumbnails.high.url;
        if (duration == '00:00') duration = 'Live Stream';
        const song = {
          url,
          title,
          duration,
          thumbnail,
          voiceChannel
        };

        message.guild.musicData.queue.push(song);

        if (message.guild.musicData.isPlaying == false) {
          message.guild.musicData.isPlaying = true;
          songEmbed.delete(); // delete the selection embed
          this.playSong(message.guild.musicData.queue, message);
        } else if (message.guild.musicData.isPlaying == true) {
          songEmbed.delete();
          // add the song to queue if one is already playing
          return message.say(`${song.title} added to queue`);
        }
    } catch (err) {
      // if something went wrong when calling the api:
      console.error(err);
      if (songEmbed) {
        songEmbed.delete();
      }
      return message.say(
        'Something went wrong with searching the video you requested :('
      );
    }
  }

So what's that playSong function we called multiple times above? This function takes the queue and the message object as arguments. When called, it tells the bot to join the user's channel and start playing music!

// this is inside the PlayCommand class
  playSong(queue, message) {
    let voiceChannel;
    queue[0].voiceChannel
      .join() // join the user's voice channel
      .then(connection => {
        const dispatcher = connection
          .play(
            ytdl(queue[0].url, { // pass the url to .ytdl()
              quality: 'highestaudio',
              // download part of the song before playing it
              // helps reduces stuttering
              highWaterMark: 1024 * 1024 * 10
            })
          )
          .on('start', () => {
            // the following line is essential to other commands like skip
            message.guild.musicData.songDispatcher = dispatcher;
            dispatcher.setVolume(message.guild.musicData.volume);
            voiceChannel = queue[0].voiceChannel;
            // display the current playing song as a nice little embed
            const videoEmbed = new MessageEmbed()
              .setThumbnail(queue[0].thumbnail) // song thumbnail
              .setColor('#e9f931')
              .addField('Now Playing:', queue[0].title)
              .addField('Duration:', queue[0].duration);
            // also display next song title, if there is one in queue
            if (queue[1]) videoEmbed.addField('Next Song:', queue[1].title);
            message.say(videoEmbed); // send the embed to chat
            return queue.shift(); //  dequeue the song
          })
          .on('finish', () => { // this event fires when the song has ended
            if (queue.length >= 1) { // if there are more songs in queue
              return this.playSong(queue, message); // continue playing
            } else { // else if there are no more songs in queue
              message.guild.musicData.isPlaying = false;
              return voiceChannel.leave(); // leave the voice channel
            }
          })
          .on('error', e => {
            message.say('Cannot play song');
            message.guild.musicData.queue.length = 0;
            message.guild.musicData.isPlaying = false;
            message.guild.musicData.nowPlaying = null;
            console.error(e);
            return voiceChannel.leave();
          });
      })
      .catch(e => {
        console.error(e);
        return voiceChannel.leave();
      });
  }

formatDuration function:

  formatDuration(durationObj) {
    const duration = `${durationObj.hours ? durationObj.hours + ':' : ''}${
      durationObj.minutes ? durationObj.minutes : '00'
    }:${
      durationObj.seconds < 10
        ? '0' + durationObj.seconds
        : durationObj.seconds
        ? durationObj.seconds
        : '00'
    }`;
    return duration;
  }

That's it!

You can check out other music commands on the bot's repo

If you're running into issues, either comment down below or open an issue in the bot's GitHub repository.

I've also written a guide on writing a music quiz(trivia) command, you can check it out here

Posted on by:

Discussion

markdown guide
 

Hello Nir, I got a problem at "Something went wrong with searching the video you requested :("

 

Did you generate an API key from youtube? And did you clone my repo or copy code?

 

I play the song and the bot suddenly exits the voice channel and says "Cannot play song"

can't play playlist and bots don't join on Voice Channel
thepracticaldev.s3.amazonaws.com/i...

So you did something wrong, did you generate an API key from youtube?

yes i generated API key from youtube

Did you install everything I listed? Git, ffmpeg etc

I installed everything in your package.json, and I didn't change it at all

I mean git, ffmpeg and python 2.7
It's in the prerequisites

I have not installed ffmpeg and python2.7 on my computer, and I only install git

What should I do after installing ffmpeg, python 2.7 and git?

You need to install them all, doesn't matter in which order

I will try if there is a problem I will call you back, Thank you for the help.

hello Nir, now the problem is here and I try to install node-opus in visual studio which is always error

thepracticaldev.s3.amazonaws.com/i...

 

Hello Nir
Having error
TypeError: Cannot read property 'isTriviaRunning' of undefined

 

Did you extend the Guild class in index.js?

 

I seem to have an issue with extend for the guild class line.
"Structures.extend('8', Guild => {
^

TypeError: Cannot read property 'extend' of undefined"

Did you import Structures like I did above?

const { Structures } = require('discord.js');

Ya, It's in line 2, But when I 'node .' it still doesn't run due to the error

Did you install the master branch of discord.js?

npm install discord.js#master

I tried to but it says that the '#' is an invalid character or smth

Sorry, it's

npm install discordjs/discord.js

And you need git in order for this to work

hello nir,
I want to ask what is the purpose of git? and how to install it. sorry i am a beginner

 

I get this error when using a link: 'No filter selected. Expected one of: idParam, myRated, chart, id'

Works fine when using the search function, only happens with links

 

What link did you try?

 

any youtube link, when you do !play it gives this error. But not if you just do !play

This error is on your end, it's a youtube API error. I'll try to figure out why you're getting it

Did you modify any code? Also check if your dependencies are the same version as the repo's

Didn't modify anything no, what do you mean with repo?

Repository. Compare your package.json with the package.json on my repo: github.com/galnir/Master-Bot

my ytdl-core is 0.29.7 instead of 0.29.5 but youtube api and discord js + commando are the same

Found the issue, was missing a / or \ somewhere in the query .split

 
 

Hey, everything seems to be working, but as soon as the bot joins the vc, it automatically leaves

 

Yeah there's an issue now with ytdl-core/YouTube

 

Is there any way to get around the issue, or is it like that until youtube fixes it?

Try removing line 252 in play.js
If that solves it tell me, not near a pc atm

I deleted the line, there seems to still be an issue, is this the right bit of code?
quality: 'highestaudio',

Yeah so there's an issue with ytdl, I just saw more ppl reporting it

 

Update
Hey, updating ytdl-core to the latest version solves that error.

npm install ytdl-core@latest

 

Upon running the node.js and fixing all the missing modules. I get this error and I can't find a solution.

First, the bot starts without a problem but after trying to play a song the bot joins the channel and crashes with the error: secretbox.methods.close is not a function.

Any help is appreciated,
Thanks in advance.


D:\Git\Private\Master-Bot-master>node index.js
Ready!
D:\Git\Private\Master-Bot-master\node_modules\discord.js\src\client\voice\dispatcher\StreamDispatcher.js:262
return [secretbox.methods.close(buffer, this._nonceBuffer, secret_key), this._nonceBuffer.slice(0, 4)];
^

TypeError: secretbox.methods.close is not a function
at StreamDispatcher._encrypt (D:\Git\Private\Master-Bot-master\node_modules\discord.js\src\client\voice\dispatcher\StreamDispatcher.js:262:33)
at StreamDispatcher._createPacket (D:\Git\Private\Master-Bot-master\node_modules\discord.js\src\client\voice\dispatcher\StreamDispatcher.js:281:49)
at StreamDispatcher._playChunk (D:\Git\Private\Master-Bot-master\node_modules\discord.js\src\client\voice\dispatcher\StreamDispatcher.js:253:27)
at StreamDispatcher._write (D:\Git\Private\Master-Bot-master\node_modules\discord.js\src\client\voice\dispatcher\StreamDispatcher.js:107:10)
at doWrite (_stream_writable.js:403:12)
at writeOrBuffer (_stream_writable.js:387:5)
at StreamDispatcher.Writable.write (_stream_writable.js:318:11)
at SingleSilence.ondata (_stream_readable.js:717:22)
at SingleSilence.emit (events.js:315:20)

at SingleSilence.Readable.read (_stream_readable.js:506:10)

 
          const video = await youtube.getVideoByID(id);
                        ^^^^^

SyntaxError: await is only valid in async function
?[90m at wrapSafe (internal/modules/cjs/loader.js:1072:16)?[39m
?[90m at Module._compile (internal/modules/cjs/loader.js:1122:27)?[39m
?[90m at Object.Module._extensions..js (internal/modules/cjs/loader.js:1178:10)?[39m
?[90m at Module.load (internal/modules/cjs/loader.js:1002:32)?[39m
?[90m at Function.Module._load (internal/modules/cjs/loader.js:901:14)?[39m
?[90m at Module.require (internal/modules/cjs/loader.js:1044:19)?[39m
?[90m at require (internal/modules/cjs/helpers.js:77:18)?[39m

-can you plz help me with that?

 

Hello Nir, I know everyone says that word but Idk what to say other than that
I checked your bot and used it but there is a small problem, is that it doesnt enter the vc and it doesnt
responds to me ever, i added every thing and the api, youtube api summary i added everything but the bot doesnt reponds to me so if you can help i would apreciate that

 

Hi Nir! Thx for this awesome work u r sharing. But I'd still need some more of ur help... I installed discord.js-commendo but it won't recognize it, leaving me with the problem:

"Error: cannot find module 'discord.js-commando'"

I've been searching what could it be for the last couple of days and I've got nothing... What should I do?

 

I think you misspelled the name of the package, it's as the error says 'discord.js-commando' and not 'commendo'.
It shouldn't throw any errors, all you need to do is install it in the right directory using npm or yarn

 

Hello Nir, I know everyone says that word but Idk what to say other than that
I checked your bot and used it but there is a small problem, is that it doesn't enter the VC and it doesn't
responds to me ever, I added every thing and the API, youtube API summary I added everything but the bot doesn't respond to me so if you can help I would appreciate that

 

Make sure you installed all the requirements correctly(python 2.7, ffmpeg) and also make sure that running 'npm i node-opus' doesn't fail

 

Hey! I'm having a lot of problems with the audio quality ('play' command), is there anything I can do to fix? I know it isn't my internet because this happens with any video and on any network.

 

how to auto play song without choosing?

 

hi nir, whenever i give a command the bot says the same response 3-5 times. Pls helpp

 

Hello Nir, I imported the packages and got This Error.

TypeError: Cannot read property 'extend' of undefined

Please help!

 

You did not install the master version of discord.js

enter
npm i discordjs/discord.js

 

Current you just told us where to put those commands because nothing works for me

 

Hello Nir,
I have an error with your code.

This is the Error:
TypeError: Cannot read property 'channel' of undefined

 

Try the solutions I provided in this issue:
github.com/galnir/Master-Bot/issue...

 

tons of error, outdated sadly, good start tho gained time
i think

 

Can you elaborate what errors?

 

TypeError: connection.play is not a function

 

Sorry for the late response.

This error is emitted because you are using the stable version of discord.js.
In order to use .play() you need to be on the master version

 

Is there any way around ffmpeg? I'm trying to make it on repl.it but it seems like it doesn't support it :/

 
 

Hello Nir, i get a problem on the line => const thumbnail = video.thumbnails.high.url; says url is not defined

 

Did you install everything I listed?

 

yes it works fine without that line but once that line is added it gives that error

edit - i had to replace the variable of the embed with the yt link for imgs - i.ytimg.com/vi/${song.id}/hqdefaul... for getting the thumbnail instead of that declaration and it still worked fine

I don't know why you're getting thumbnails errors, I run the bot on 2 machines and never got that error

 

If the bot is playing on server 1, and i use the play command on server 2, it will add it to the queue, and only join server 2 voice channel after it is done in server 1. How do i fix this?

 

This no longer happens. The bot supports playing on multiple guilds at a time.

 

Hello Nir,
I'm having problems when i tried node .
I've added config.json, changed my discord owner id, youtube API, as well as the bot's token

When I tried to on my bot, it says "MODULE_NOT_FOUND"

_EDIT: I cloned your bot from Github

 

Hii~ I'm having a problem with the file downloads themselves. When I NPM install some of the downloads don't function and I get error messages. Are there any replacements/updated links?
Thankss~

 

The dependencies names don't change. Try to Google the error messages to find what's wrong. Probably something permission related