Create your own Bot Discord
Tags: javascript, node, npm, discord
Published: 2018-06-11 21:09:39

---
title: Create your own Bot Discord
published: true
description: A basic Discord bot to start developing his own. Developed with JavaScript and run with NodeJS.
tags: javascript,nodejs,npm,discord
cover_image: https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7v29u99etnu6uhv1ocjt.png
canonical_url: https://thomasbnt.dev/blog/robot-discord-basique
series: Discord Stuff
---



|DISCLAIMER|This tutorial is for creating commands with a prefix. It is not for using Slash Commands.|
|---|---|

* **Preamble**

Before beginning this tutorial, follow the instructions below. 
You must know JavaScript. You need to install several tools in order for the bot to work, and create a folder with any name, preferably short and without spaces. This will be your folder containing your bot files.

List of tools :

- Install [the latest stable version of NodeJS](https://nodejs.org/)
- Having access to the Terminal
- Have a good internet connection
- To know how to read a minimum English for documentation
- A text editor or IDE (Notepad ++, Atom, JetBrains, VSC..)

We will at first if everything has been installed. Open your Terminal and not the one installed by NodeJS, and type :

```bash
node --version 
```

It is supposed to take you to the version you have installed. Leave this window open for the rest of the installation.

* **Installation**

After you have successfully installed the latest version of NodeJS, you must install in your bot's folder. To move in the order guest :

```bash
cd /folderName
```

For simplicity, go to your folder by navigating with the GUI and not with the command prompt and copy the URL at the top. Then paste it into the Terminal by adding `cd` at the beginning to indicate that you want to navigate to this folder.

> ⚠ If you are not in the folder of your bot with the Terminal, please review the top lines.

We will install the package for the bot to work. This package is called Discord.js, it is a library related to Discord and developed in JavaScript.

```bash
npm install discord.js --save 
```
You will normally have some errors but nothing that will prevent you from starting the bot, as well as a **node_modules** folder created in your folder. This is where we install all the packages from NodeJS to make the code work.


`+ discord.js@13.5.1
updated 1 package in 1.241s`

We see, in the end, that the package discord.js version 13.5.1 has been installed in the folder.

* **Examples**

Everything is installed? Wonderful ! Please now create a file that you will name `app.js`. Be careful that your file extension does not end with anything other than `.js` because otherwise we will not be able to execute the script. Then open it.

> ⚠ Reminder : Open with a text editor or IDE such as VSC, Atom, Notepad ++.

For each JavaScript file, we must call discord.js so that everything works perfectly, we will also declare the robot client :

```javascript
const Discord = require('discord.js')
const client = new Discord.Client()

client.on("message", message => {

  // Your commands here

})
```
Example of a ping pong message :

```javascript
// Result in: If the user's message contains "ping" then

if(message.content === "ping"){

   // the bot answers pong!
   message.channel.send("Pong!")
  
}
```
Example message with a prefix :

```javascript
// We declare the prefix
const prefix = '?'

// The bot will answer this if a user does ?Help
if(message.content.startsWith(prefix + "help")){
   message.channel.send("You did `?help` to get help.")
}
```

Private message example :

```javascript
if(message.content == "mp") {
   message.author.send("Here is a private message !")
}
```

* **Bot creation**

Now you have to create the bot on the Discord platform and add it to your server. For that, we will do it in two stages. Follow this process :

> ⚠ You must be logged into your Discord account in order to access this page.

- Go to: [https://discord.com/developers/applications/me](https://discord.com/developers/applications/me)
- Then **MyApplications** → **New Application**.
- Complete the form. Once done, click on "**Create App**" then **again** on the same button.
- Click "**Create a Bot User**" and click "**Yes, do it!**"

![This is a preview](https://thepracticaldev.s3.amazonaws.com/i/8016yfpogcurzb3gsjph.gif)

And activate the 'mode' bot

![This is a preview for activating the 'mode' bot](https://thepracticaldev.s3.amazonaws.com/i/lcpkmvhiby8n61nsyykm.png)

For the permissions, the robot needs to read the messages on the server (MESSAGE CONTENT INTENT).

It remains to add it on your own server. To do this, just click on "**Generate OAuth2 URL**" and **generate your link**. Copy it and open it in a new tab, you are asked to **select a server**. Select yours and click **Allow**.

You now have your bot on your server, but it is offline. This is completely normal ! Follow the sequence to turn it on.

* **Start-up**

You must copy your bot token from the Discord for Developers page and insert it with this piece of code at the end of your `app.js` :

```javascript
client.login("YOUR_TOKEN_HERE_WITHOUT_SPACE");
```

Example of a copy token :

![Screen of example of a copy token](https://thepracticaldev.s3.amazonaws.com/i/ywvuv78t9y0uv8fcabuz.png)

Save your file and make this command in the command prompt :

```bash
node app.js
```
There you go ! Your bot is on ! 🎉

![Preview](https://thepracticaldev.s3.amazonaws.com/i/jyjac7sagqzplicjvuy3.gif)


___________

🇫🇷 The tutorial [comes from my website](https://thomasbnt.dev/blog/robot-discord-basique), it is written in French.
You can [download files](https://github.com/thomasbnt/Robot-Discord-Basique-en-JS/releases/) and [here is the discord.js documentation](https://discord.js.org/#/docs/main/stable/general/welcome).

|☕|Check my [Twitter account](https://twitter.com/thomasbnt_). You can see many projects and updates. You can also [support me on Buy Me a Coffee, Stripe or GitHub Sponsors](https://thomasbnt.dev/donate). Thanks for read my post ! 🤩|
|---|---|