loading...
Cover image for Create your own Bot Discord
Mr. Robøt

Create your own Bot Discord

thomasbnt profile image Thomas Bnt Updated on ・4 min read
  • Preamble

Before beginning this tutorial, follow the instructions below. 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 

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

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 in 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 an library related to Discord and developed in javascript.

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@11.3.2
updated 1 package in 1.241s

We see in the end, that the package discord.js version 11.3.2 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

})

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!")
  
}

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.")
}

Private message example :

if(message.content == "mp") {
   message.author.send("Here is a private message !")
}
  • Robot 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 activate the 'mode' bot

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");

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

There you go ! Your bot is on ! 🎉

Preview


🇫🇷 The tutorial comes from my website, it is written in French and is more optimized to read it.
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 BuyMeaCoffee.

Posted on by:

thomasbnt profile

Thomas Bnt

@thomasbnt

French web developer maintly but touches everything, servers, bots, community management, events, social networks and moore. I learn VueJs at this moment and MySQL. — Addict to Cappuccino and Music

Mr. Robøt

A French Discord bot for stopping spams and harassments. Modding tool and logging.

Discussion

pic
Editor guide
 
 

Oh thanks 👍

 

do you have a download of app.js in english i really cant use french

 

No i'm sorry 😐

Some comments have been hidden by the post's author - find out more

Code of Conduct Report abuse