Discord Server Twitter
Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Tutorial Creating a discord bot with discord.js
#1

Introduction


Discord.js is a node.j mod ule that allows your code to interact with the Discord API. In this tutorial, i'll teach you how to create a very simple bot using this module. To get started, you need to install somethings:
  • Node.js: u can download it in here;
  • Discord.js: after get node.js, open the command prompt and type: npm install discord.js --save



Creating an Discord App



To interact with discord, your code must be associated with a Discord App. To create one, go to this link, after that, click on New App.
[Image: IuV0k2O.png]

 You'll be redirected to a screen where you can define your bot's name, description and image. When you're done, click on Create App.

[Image: qj9NlGM.png]



Configuring the Bot User



Well, now you already have your app, but you still need to configure one more thing. If u want that your app be treated as a discord bot, u need to create a "User Bot" by clicking in Create a Bot User.


[Image: image.png]


Now, click in "Yes, do it!"


[Image: image.png]



Getting a token



To make the code that we'll write, connect through the application that we created, we will need a token. The token is basically a code that every User Bot has, and can use it to connect to discord. To generate a token for your bot, click on "click to reveal".


[Image: image.png]

Write your token in somewhere, and don't it to anyone! If someone has the token, it can control your bot. So it's very important that you keep it safe.


Inviting a bot to a server



Now that you have your token, u need to invite your bot to a server. We can make it using the Client ID:
First, get your Client ID

[Image: image.png]

Now, put it in place of YOUR_ID_HERE in discordapp.com/oauth2/authorize?&client_id=YOUR_ID_HERE&scope=bot and enter the URL you got. You'll be taken to a screen where  you can select a server to place your bot.


Programming the bot



Finally! It's time to code. First, create a folder and inside of it, an file named bot.js. Do not write anything yet! Now create another file, caled start.bat and inside of it, put this code:


Code:
color a
echo off
cls
echo Loading bot...
node bot.js
PAUSE

Now let's go to the bot.js file. Start the code with the following lines:

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

This are some configurating variables. Let's move on:


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

client.on('ready', () => {
 console.log('I am ready!');
});

Let me explain a little bit:

Code:
client.on('ready', () => {
 
});

Is an event. It'll be triggered when the bot get ready to connect.

Code:
console.log('I am ready!');

It's a method that send something in the console.

So when the bot's get ready to connect, it'll say: "I am ready!" in the console.


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

client.on('ready', () => {
 console.log('I am ready!');
});


client.login('token');

Now i add the login method! It's used to login in discord, by using a token. The bot is already working. But let's add a command to make it better!


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

client.on('ready', () => {
 console.log('I am ready!');
});

client.on('message', message => {
 if (message.content === 'ping') {
   message.reply('pong');
 }
});

client.login('token');

Do you know what this code will do? The event will be triggered when someone send a message. If this message is ping, the bot will send "pong". Pretty cool, huh?


Final



Txh for be here with me during this tutorial. I hope that u use what i show here for all your life. Knowledge is one of the few things that people can't take from u, so use it, enjoy it, share it!



[-] The following 1 user says Thank You to Exception for this post:
  • Mr.Kurd
Reply
#2
Thanks bro, very nice tutorial.

[Image: 0yaXvcy.gif]
We Are Anonymous, We Are Legion, We Do not Forgive, We do not Forget
Expect Us
Reply




Users browsing this thread: 1 Guest(s)