How to set up a Discord music bot (Updated December 2021)
Tags: discord, bot, javascript, node
Published: 2019-09-13 17:32:42

---
title: How to set up a Discord music bot (Updated December 2021)
published: true
description: How to set up and run a Discord music bot
tags: discord, bot, javascript, nodejs
cover_image: https://images.unsplash.com/photo-1505740420928-5e560c06d30e?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1000&q=420
---

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](https://discord.com/developers/applications) 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**](https://code.visualstudio.com/Download).
You can also use other great editors like [**Sublime Text**](https://www.sublimetext.com/) and [**Atom**](https://atom.io/).


## 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**](https://nodejs.org/en/) (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](https://www.geeksforgeeks.org/installation-of-node-js-on-windows/).
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](https://codeburst.io/git-and-github-in-a-nutshell-b0a3cc06458f).

If you're on Windows, install Git from [here](https://git-scm.com/download/win). 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](https://www.azul.com/downloads/?version=java-13-mts&package=jdk), 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](https://github.com/Cog-Creators/Lavalink-Jars/releases) (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](https://github.com/freyacodes/Lavalink/blob/master/LavalinkServer/application.yml.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](https://developer.spotify.com/dashboard/login)
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:
```json
{
  "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"
}
```
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](https://unsplash.com/@maltewingen) :)