Create your own Bot Discord

thomasbnt

Thomas Bnt

Posted on June 11, 2018

Create your own Bot Discord
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
  • 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 :

node --version 
Enter fullscreen mode Exit fullscreen mode

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 :

cd /folderName
Enter fullscreen mode Exit fullscreen mode

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.

npm install discord.js --save 
Enter fullscreen mode Exit fullscreen mode

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 :

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

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

  // Your commands here

})
Enter fullscreen mode Exit fullscreen mode

Example of a ping pong message :

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

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

   // the bot answers pong!
   message.channel.send("Pong!")
  
}
Enter fullscreen mode Exit fullscreen mode

Example message with a prefix :

// 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.")
}
Enter fullscreen mode Exit fullscreen mode

Private message example :

if(message.content == "mp") {
   message.author.send("Here is a private message !")
}
Enter fullscreen mode Exit fullscreen mode
  • 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.

This is a preview

And activate the 'mode' bot

This is a preview for activating the 'mode' bot

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 :

client.login("YOUR_TOKEN_HERE_WITHOUT_SPACE");
Enter fullscreen mode Exit fullscreen mode

Example of a copy token :

Screen of example of a copy token

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

node app.js
Enter fullscreen mode Exit fullscreen mode

There you go ! Your bot is on ! 🎉

Preview


🇫🇷 The tutorial comes from my website, it is written in French.
You can download files and here is the discord.js documentation.

Check my Twitter account. You can see many projects and updates. You can also support me on Buy Me a Coffee, Stripe or GitHub Sponsors. Thanks for read my post ! 🤩
💖 💪 🙅 🚩
thomasbnt
Thomas Bnt

Posted on June 11, 2018

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

Créer son propre Bot Discord
javascript Créer son propre Bot Discord

May 9, 2019

Create your own Bot Discord
javascript Create your own Bot Discord

June 11, 2018