DEV Community

Cover image for How to set up a Discord music bot (Updated December 2021)
Nir
Nir

Posted on • Updated on

Music Bot for Discord How to set up a Discord music bot (Updated December 2021)

Setting up a music bot isn't easy since it requires lots of different things to be installed. In this guide I will list all the requirements and provide you with the code you need. Make sure you follow all the steps :)

Let's start!

Create a bot

First we need to create the bot, go here and make sure you're logged in. Click on New Application and give your bot a name.
Go to the "Bot" tab on the left and click Add Bot under the "BUILD-A-BOT" tab. Copy the token and save it because we will need it for later.
Copy the Application ID under the "General Information" tab and save it for later as well.

Now click on the "OAuth2" tab and then on "URL Generator". Under "Scopes", check "bot" and on "applications.commands". Under "Bot Permissions", check "Administrator". Now copy the URL under "Generated URL" and paste it into your browser in order to invite your newly created bot to your Discord server!

Code Editor

A code editor let's us write and modify code. The code editor I use and recommend is Visual Studio Code.
You can also use other great editors like Sublime Text and Atom.

Node.js and npm

Node.js is a JavaScript runtime. It basically lets us run JavaScript code on our machine. We also use its package ecosystem, npm, which we will discuss next.
Download Node.js from its official website (LTS or latest).
To check if you installed it correctly, open your terminal(cmd or bash depending on which OS you are using), and run node -v, you should get a number back. If you're struggling with installing Node, check out this guide.
nom comes installed with Node.js, we will use npm later to install dependencies.

Git

Git is an open source version control system. It helps developers to manage projects by keeping track of the entire history of a project and makes it easy to add features without worrying about breaking the entire codebase. We will use Git in order to obtain code for our bot from GitHub. GitHub is a hosting service for Git repositories. You can learn more about Git and Github from this superb article.

If you're on Windows, install Git from here. For macOS users, install Xcode and then hit git --version, it'll prompt you to install it. If you're using Linux based OS, hit sudo dnf install git-all or sudo apt install git-all for debian.

Java 13

Download Java 13 from here, make sure you download the correct one for your Operating System.

Cloning the repository

Go to your terminal(cmd, git bash or bash), you probably see on the left that you are now in your user folder(C:\Users/"username"). Enter cd Desktop to go to your Desktop. Then 'git clone https://github.com/galnir/Master-Bot'. You are cloning(copying) the repository from GitHub so you can use its code.

Getting Lavalink

Download the latest Lavalink jar file from here (under assets). Place the jar file in the root directory of the project (same folder as index.js and package.json)

Application.yml

Create a file named application.yml in the same folder you placed the jar file from before. Copy everything from this example to your application.yml

Tokens

For everything to function we need the following tokens:

  • The bots discord ID (saved from the first section)
  • The bots token (saved from the first section)
  • A Spotify client ID
  • A Spotify client secret

In order to generate the last 2 tokens you need to create a Spotify account and login to the dev dashboard
After you login, simply create an app and copy the client ID and secret.

Config.json

In the root directory of the project create a file named config.json and have it look like this:

{
  "client_id": "the-bots-discord-id",
  "token": "Your-Bot-Token",
  "lava_host": "0.0.0.0",
  "lava_pass": "youshallnotpass",
  "spotify_client_id": "get-it-from-spotify-dev",
  "spotify_client_secret": "get-it-from-spotify-dev"
}
Enter fullscreen mode Exit fullscreen mode

Put every token you generated in its place surrounded by quotes

Running the bot

Open a terminal in the root directory of the project and run java -jar Lavalink.jar.
Now open another terminal instance in the same folder and run node index.js

After running both commands you should be able to run music commands using the new Slash feature! Simply type / in a text channel and you should see the command list!

If you have any questions/issues you can comment here or open an issue in the repo. Also don't forget to leave a star in the repo if you found it helpful :)

Bot Repo: https://github.com/galnir/Master-Bot
Cover image by Malte Wingen :)

Latest comments (72)

Collapse
 
suprx44 profile image
SupRx44 • Edited

THERE IS NO "index.js" IN THE ROOT DIRECTORY. WHY?
ALL THE HELP YOU GIVE US IS "did u install this?"
YEAH! EVERYBODY INSTALLED THE PREREQUISITES AND YOUR CODE IS STILL BUNKASS.

Collapse
 
suprx44 profile image
SupRx44 • Edited

Please Help. I've followed the steps described, but this same error keeps coming up when I run "node index.js".

PS C:\Users[user]\Desktop\Master-Bot> node index.js
node:internal/modules/cjs/loader:936
throw err;
^

Error: Cannot find module 'C:\Users[user]\Desktop\Master-Bot\index.js'
←[90m at Function.Module._resolveFilename (node:internal/modules/cjs/loader:933:15)←[39m
←[90m at Function.Module._load (node:internal/modules/cjs/loader:778:27)←[39m
←[90m at Function.executeUserEntryPoint as runMain←[39m
←[90m at node:internal/main/run_main_module:17:47←[39m {
code: ←[32m'MODULE_NOT_FOUND'←[39m,
requireStack: []
}

I have Node v16.14.2 and Python 2.7. I downloaded LavaLink. Copied the freyacodes application.yml. I made a new Spotify account. I created a MongoDB cluster. I setup my config file with the necessary IDs, tokens, and keys. I spent 3 hours trying to set this up, and what, I'm missing ONE GODDAMN FILE?! Thanks for the help, OP.

Collapse
 
funndot profile image
FunnDot

can u help me?
import { load } from '@lavaclient/spotify';
^^^^^^
SyntaxError: Cannot use import statement outside a module
at Object.compileFunction (node:vm:352:18)
at wrapSafe (node:internal/modules/cjs/loader:1032:15)
at Module._compile (node:internal/modules/cjs/loader:1067:27)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1155:10)
at Module.load (node:internal/modules/cjs/loader:981:32)
at Function.Module._load (node:internal/modules/cjs/loader:822:12)
at Function.executeUserEntryPoint as runMain
at node:internal/main/run_main_module:17:47

Collapse
 
mostafa12a profile image
MOSTAFA12A

PS C:\Users\roke\Desktop\igdj> npm i node-opus
npm WARN deprecated node-opus@0.3.3: This project is unmaintained. See @discordjs/opus for an alternative.
npm ERR! code 1
npm ERR! path C:\Users\roke\Desktop\igdj\node_modules\node-opus
npm ERR! command failed
npm ERR! command C:\Windows\system32\cmd.exe /d /s /c node-gyp rebuild
npm ERR! gyp info it worked if it ends with ok
npm ERR! gyp info using node-gyp@8.2.0
npm ERR! gyp info using node@16.9.1 | win32 | x64
npm ERR! gyp ERR! find Python
npm ERR! gyp ERR! find Python Python is not set from command line or npm configuration
npm ERR! gyp ERR! find Python Python is not set from environment variable PYTHON
npm ERR! gyp ERR! find Python checking if "python3" can be used
npm ERR! gyp ERR! find Python - "python3" is not in PATH or produced an error
npm ERR! gyp ERR! find Python checking if "python" can be used
npm ERR! gyp ERR! find Python - "python" is not in PATH or produced an error
npm ERR! gyp ERR! find Python checking if Python is C:\Users\roke\AppData\Local\Programs\Python\Python39\python.exe
npm ERR! gyp ERR! find Python - "C:\Users\roke\AppData\Local\Programs\Python\Python39\python.exe" could not be run
npm ERR! gyp ERR! find Python checking if Python is C:\Program Files\Python39\python.exe
npm ERR! gyp ERR! find Python - "C:\Program Files\Python39\python.exe" could not be run
npm ERR! gyp ERR! find Python checking if Python is C:\Users\roke\AppData\Local\Programs\Python\Python39-32\python.exe
npm ERR! gyp ERR! find Python - "C:\Users\roke\AppData\Local\Programs\Python\Python39-32\python.exe" could not be run
npm ERR! gyp ERR! find Python checking if Python is C:\Program Files\Python39-32\python.exe
npm ERR! gyp ERR! find Python - "C:\Program Files\Python39-32\python.exe" could not be run
npm ERR! gyp ERR! find Python checking if Python is C:\Program Files (x86)\Python39-32\python.exe
npm ERR! gyp ERR! find Python - "C:\Program Files (x86)\Python39-32\python.exe" could not be run
npm ERR! gyp ERR! find Python checking if Python is C:\Users\roke\AppData\Local\Programs\Python\Python38\python.exe
npm ERR! gyp ERR! find Python - "C:\Users\roke\AppData\Local\Programs\Python\Python38\python.exe" could not be run
npm ERR! gyp ERR! find Python checking if Python is C:\Program Files\Python38\python.exe
npm ERR! gyp ERR! find Python - "C:\Program Files\Python38\python.exe" could not be run
npm ERR! gyp ERR! find Python checking if Python is C:\Users\roke\AppData\Local\Programs\Python\Python38-32\python.exe
npm ERR! gyp ERR! find Python - "C:\Users\roke\AppData\Local\Programs\Python\Python38-32\python.exe" could not be run
npm ERR! gyp ERR! find Python checking if Python is C:\Program Files\Python38-32\python.exe
npm ERR! gyp ERR! find Python - "C:\Program Files\Python38-32\python.exe" could not be run
npm ERR! gyp ERR! find Python checking if Python is C:\Program Files (x86)\Python38-32\python.exe
npm ERR! gyp ERR! find Python - "C:\Program Files (x86)\Python38-32\python.exe" could not be run
npm ERR! gyp ERR! find Python checking if Python is C:\Users\roke\AppData\Local\Programs\Python\Python37\python.exe
npm ERR! gyp ERR! find Python - "C:\Users\roke\AppData\Local\Programs\Python\Python37\python.exe" could not be run
npm ERR! gyp ERR! find Python checking if Python is C:\Program Files\Python37\python.exe
npm ERR! gyp ERR! find Python - "C:\Program Files\Python37\python.exe" could not be run
npm ERR! gyp ERR! find Python checking if Python is C:\Users\roke\AppData\Local\Programs\Python\Python37-32\python.exe
npm ERR! gyp ERR! find Python - "C:\Users\roke\AppData\Local\Programs\Python\Python37-32\python.exe" could not be run
npm ERR! gyp ERR! find Python checking if Python is C:\Program Files\Python37-32\python.exe
npm ERR! gyp ERR! find Python - "C:\Program Files\Python37-32\python.exe" could not be run
npm ERR! gyp ERR! find Python checking if Python is C:\Program Files (x86)\Python37-32\python.exe
npm ERR! gyp ERR! find Python - "C:\Program Files (x86)\Python37-32\python.exe" could not be run
npm ERR! gyp ERR! find Python checking if Python is C:\Users\roke\AppData\Local\Programs\Python\Python36\python.exe
npm ERR! gyp ERR! find Python - "C:\Users\roke\AppData\Local\Programs\Python\Python36\python.exe" could not be run
npm ERR! gyp ERR! find Python checking if Python is C:\Program Files\Python36\python.exe
npm ERR! gyp ERR! find Python - "C:\Program Files\Python36\python.exe" could not be run
npm ERR! gyp ERR! find Python checking if Python is C:\Users\roke\AppData\Local\Programs\Python\Python36-32\python.exe
npm ERR! gyp ERR! find Python - "C:\Users\roke\AppData\Local\Programs\Python\Python36-32\python.exe" could not be run
npm ERR! gyp ERR! find Python checking if Python is C:\Program Files\Python36-32\python.exe
npm ERR! gyp ERR! find Python - "C:\Program Files\Python36-32\python.exe" could not be run
npm ERR! gyp ERR! find Python checking if Python is C:\Program Files (x86)\Python36-32\python.exe
npm ERR! gyp ERR! find Python - "C:\Program Files (x86)\Python36-32\python.exe" could not be run
npm ERR! gyp ERR! find Python checking if the py launcher can be used to find Python 3
npm ERR! gyp ERR! find Python - "py.exe" is not in PATH or produced an error
npm ERR! gyp ERR! find Python
npm ERR! gyp ERR! find Python **********************************************************
npm ERR! gyp ERR! find Python You need to install the latest version of Python.
npm ERR! gyp ERR! find Python Node-gyp should be able to find and use Python. If not,
npm ERR! gyp ERR! find Python you can try one of the following options:
npm ERR! gyp ERR! find Python - Use the switch --python="C:\Path\To\python.exe"
npm ERR! gyp ERR! find Python (accepted by both node-gyp and npm)
npm ERR! gyp ERR! find Python - Set the environment variable PYTHON
npm ERR! gyp ERR! find Python - Set the npm configuration variable python:
npm ERR! gyp ERR! find Python npm config set python "C:\Path\To\python.exe"
npm ERR! gyp ERR! find Python For more information consult the documentation at:
npm ERR! gyp ERR! find Python github.com/nodejs/node-gyp#install...
npm ERR! gyp ERR! find Python **********************************************************
npm ERR! gyp ERR! find Python
npm ERR! gyp ERR! configure error
npm ERR! gyp ERR! stack Error: Could not find any Python installation to use
npm ERR! gyp ERR! stack at PythonFinder.fail (C:\Users\roke\Desktop\igdj\node_modules\node-gyp\lib\find-python.js:330:47)
npm ERR! gyp ERR! stack at PythonFinder.runChecks (C:\Users\roke\Desktop\igdj\node_modules\node-gyp\lib\find-python.js:159:21)
npm ERR! gyp ERR! stack at PythonFinder. (C:\Users\roke\Desktop\igdj\node_modules\node-gyp\lib\find-python.js:228:18)
npm ERR! gyp ERR! stack at PythonFinder.execFileCallback (C:\Users\roke\Desktop\igdj\node_modules\node-gyp\lib\find-python.js:294:16)
npm ERR! gyp ERR! stack at ChildProcess.exithandler (node:child_process:404:5)
npm ERR! gyp ERR! stack at ChildProcess.emit (node:events:394:28)
npm ERR! gyp ERR! stack at maybeClose (node:internal/child_process:1064:16)
npm ERR! gyp ERR! stack at Process.ChildProcess._handle.onexit (node:internal/child_process:301:5)
npm ERR! gyp ERR! System Windows_NT 10.0.19041
npm ERR! gyp ERR! command "C:\Users\roke\Desktop\igdj\node_modules\node\bin\node.exe" "C:\Users\roke\Desktop\igdj\node_modules\node-gyp\bin\node-gyp.js" "rebuild"
npm ERR! gyp ERR! cwd C:\Users\roke\Desktop\igdj\node_modules\node-opus
npm ERR! gyp ERR! node -v v16.9.1
npm ERR! gyp ERR! node-gyp -v v8.2.0
npm ERR! gyp ERR! not ok

npm ERR! A complete log of this run can be found in:
npm ERR! C:\Users\roke\AppData\Local\npm-cache_logs\2021-09-26T13_47_14_392Z-debug.log

Collapse
 
justreallyquick profile image
sav

im struggling so much here, i cannot figure out what this error means or what im supposed to do to fix it... it just keeps saying "Error: Cannot Find Module" when i try to do node index.js it just keeps saying that.

Collapse
 
suprx44 profile image
SupRx44

Same. Big thanks to the author, eh... wow rolls eyes

Collapse
 
nvptvjp profile image
NVPTVJP

help me plz beg u
Error: Cannot find module 'timers/promises'
Require stack:

  • C:\Users\kgev\node_modules\@discordjs\rest\dist\lib\handlers\SequentialHandler.js
  • C:\Users\kgev\node_modules\@discordjs\rest\dist\lib\RequestManager.js
  • C:\Users\kgev\node_modules\@discordjs\rest\dist\index.js
  • C:\Users\kgev\index.js ←[90m at Function.Module._resolveFilename (internal/modules/cjs/loader.js:889:15)←[39m ←[90m at Function.Module._load (internal/modules/cjs/loader.js:745:27)←[39m ←[90m at Module.require (internal/modules/cjs/loader.js:961:19)←[39m ←[90m at require (internal/modules/cjs/helpers.js:92:18)←[39m at Object. (C:\Users\kgev\node_modules\←[4m@discordjs←[24m\rest\dist\lib\handlers\SequentialHandler.js:6:20) ←[90m at Module._compile (internal/modules/cjs/loader.js:1072:14)←[39m ←[90m at Object.Module._extensions..js (internal/modules/cjs/loader.js:1101:10)←[39m ←[90m at Module.load (internal/modules/cjs/loader.js:937:32)←[39m ←[90m at Function.Module._load (internal/modules/cjs/loader.js:778:12)←[39m ←[90m at Module.require (internal/modules/cjs/loader.js:961:19)←[39m { code: ←[32m'MODULE_NOT_FOUND'←[39m, requireStack: [ ←[32m'C:\Users\kgev\node_modules\@discordjs\rest\dist\lib\handlers\SequentialHandler.js'←[39m, ←[32m'C:\Users\kgev\node_modules\@discordjs\rest\dist\lib\RequestManager.js'←[39m, ←[32m'C:\Users\kgev\node_modules\@discordjs\rest\dist\index.js'←[39m, ←[32m'C:\Users\kgev\index.js'←[39m ] }
Collapse
 
therealmaestro profile image
TheRealMaestro

Hey I need help, done all you said and getting an error when I try to do node index.js

throw err;
^

Error: Cannot find module 'timers/promises'
Require stack:

  • C:\Users\Oringe\Desktop\Master-Bot\node_modules\@discordjs\rest\dist\lib\handlers\SequentialHandler.js
  • C:\Users\Oringe\Desktop\Master-Bot\node_modules\@discordjs\rest\dist\lib\RequestManager.js
  • C:\Users\Oringe\Desktop\Master-Bot\node_modules\@discordjs\rest\dist\index.js
  • C:\Users\Oringe\Desktop\Master-Bot\index.js at Function.Module._resolveFilename (internal/modules/cjs/loader.js:889:15) at Function.Module._load (internal/modules/cjs/loader.js:745:27) at Module.require (internal/modules/cjs/loader.js:961:19) at require (internal/modules/cjs/helpers.js:92:18) at Object. (C:\Users\Oringe\Desktop\Master-Bot\node_modules\@discordjs\rest\dist\lib\handlers\SequentialHandler.js:6:20) at Module._compile (internal/modules/cjs/loader.js:1072:14) at Object.Module._extensions..js (internal/modules/cjs/loader.js:1101:10) at Module.load (internal/modules/cjs/loader.js:937:32) at Function.Module._load (internal/modules/cjs/loader.js:778:12) at Module.require (internal/modules/cjs/loader.js:961:19) { code: 'MODULE_NOT_FOUND', requireStack: [ 'C:\Users\Oringe\Desktop\Master-Bot\node_modules\@discordjs\rest\dist\lib\handlers\SequentialHandler.js', 'C:\Users\Oringe\Desktop\Master-Bot\node_modules\@discordjs\rest\dist\lib\RequestManager.js', 'C:\Users\Oringe\Desktop\Master-Bot\node_modules\@discordjs\rest\dist\index.js', 'C:\Users\Oringe\Desktop\Master-Bot\index.js' ] }

Please and thank you

Collapse
 
thesenseigamer profile image
Michael Ervin

I'm looking in index.js and there is nothing on line 7 for a client id, where would the client id go now? I'm looking around the code and can't figure out where it would go

Collapse
 
cybermagmadev profile image
cybermagma-dev

Hey When I play some music music it says no perms to join your channel and leaves it pls help

Collapse
 
bennett_rivers_393fd162f6 profile image
Bennett Rivers

I keep getting this error when running a play command through discord

An error occurred while running the command: TypeError: Cannot read property 'length' of undefined
You shouldn't ever receive an error like this.
Please contact the bot owner.

I have ffmpeg, python, and git all installed, so I don't know what I did wrong.

Collapse
 
shamithoysal profile image
shamithoysal

Heyo! Absolutely LOVED the post. It worked perfectly. I just wanted to know if I could edit the help command? I would do that because as I build up on this bot, I can add to the help command. Thanks in advance!

Collapse
 
kostasxp12 profile image
Kostasxp12

Can someone help me, I am trying to install npm but it just hangs.
Its not a network issue as it just gets stuck forever
this is the last thing it shows before getting stuck : extract:discord.js-commando: sill extract github:discordjs/Commando extracted to C:\Users\"Myuser"
\Desktop\Master-Bot\node_modules.staging\discord.js-commando-c5a3c176 (31760ms)

Collapse
 
pagonasp3 profile image
PagOnASp

throw err;
^

Error: Cannot find module 'C:\Users\pagonasp\Desktop\New folder (2)'
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:965:15)
at Function.Module._load (internal/modules/cjs/loader.js:841:27)
at Function.executeUserEntryPoint as runMain
at internal/main/run_main_module.js:17:47 {
code: 'MODULE_NOT_FOUND',
requireStack: []
}

Collapse
 
grantttt profile image
grantttt • Edited

Hi, I'm getting this error when trying npm install. Any idea how I can solve this?

C:\Users\grant\Desktop>npm install
npm WARN saveError ENOENT: no such file or directory, open 'C:\Users\grant\Desktop\package.json'
npm WARN enoent ENOENT: no such file or directory, open 'C:\Users\grant\Desktop\package.json'
npm WARN Desktop No description
npm WARN Desktop No repository field.
npm WARN Desktop No README data
npm WARN Desktop No license field.

the bot doesn't go online

Collapse
 
blythethea profile image
blythethea

Did you ever solve this, I am having the same errors

Some comments may only be visible to logged-in visitors. Sign in to view all comments.