Parsbot | Discord Bot Tutorial Node.js | Part 1
Node.js is a JavaScript runtime built on Chrome's V8 JavaScript engine. Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient. Node.js' package ecosystem, npm, is the largest ecosystem of open source libraries in the world.
Source
Useful Links
What Will I Learn?
Node.js Install Control
Node.js Create JS file (app.js)
Npm Discord.js Install (For our discord bot)
Coding Discord Bot (app.js)
Create new Discord App
Create new Discord Server and add our Bot
Run and test Parsbot
Requirements
Node.js (Download)
A text editor (e.g Visual Studio Code)
A Discord Account
Difficulty
- Intermediate
Curriculum
Not available since this is the first part. But you can read about my other tutorials here.
Tutorial Contents
Node.js Install Control
Firstly we need to download and install Node.js from Node.js Website.
Command Prompt Code(For node version control)
node -v
After you install Node.js you can control the node version via cmd. If you are fail to install it
'node' is not recognized as an internal or external command, operable program or batch file.
you will get the error.
But if your installation is successful, you will see your node version successfully.
Node.js Create JS file (app.js)
We will create our first js file. I created a folder called "Discord Pars Bot" on my desktop, we will use this folder as the workspace for our Discord Bot project.
Now open the visual studio code program, you will encounter a welcome page.
Okey now click "Add workspace folder...".
Select your workspace folder and click the add button.
The workspace folder must have been successfully installed. I will use github for version control so I added license file and readme files.
Now we click on the new file button above your workspace folder to create app.js file.
We write app.js and press enter.
We successfully created our app.js file. Now I close the welcome page.
Our app.js file is ready for coding. Now I write console.log code for test.
Code:
console.log("App.js Test!")
Save the app.js. (Ctrl+S)
Now open the cmd write "cd your workspace folder directory" and press enter.
I write "cd C:\Users\gffdg\Desktop\Discord Pars Bot\parsbot".
Now we will run our app.js file with node.
Command Prompt Code(Run file)
node app.js
Our app.js file worked successfully and showed "App.js Test!" message.
Npm Discord.js Install (For our discord bot)
Now, we will install the discord.js file via npm for our project. Again open cmd write "cd your workspace folder directory" and press enter.
I write "cd C:\Users\gffdg\Desktop\Discord Pars Bot\parsbot".
Type
Command Prompt Code(Npm install discord.js)
npm install discord.js
command.
Our packages was successfully installed.
Coding Discord Bot (app.js)
Now, let's use discord.js our app.js file.
Code:
const Discord = require('discord.js');
And We will create a new discord client with discord.js file.
Code:
const parsbot = new Discord.Client();
Okay now we need to login also, this can be done using this.
Code:
parsbot.login('<discordtoken>');
For discordtoken section we create a app and a bot user on discord. For now I write "discordtoken".
Now we create a listener event runs whenever the bot sends a ready event (when it first starts for example).
Code:
parsbot.on('ready', () => {
console.log('Pars Bot started.')
});
I write a post for console "Pars Bot started".
We need to define a prefix for message control.
Code:
const prefix = '$';
I write "$" you can change it to whatever you want.
Now we create a listener event for messages runs whenever a message is received.
Code:
parsbot.on ('message' , message =>{
});
Now I write message and sender variables in messages listener event.
Code:
parsbot.on ('message' , message => {
let msg = message.content.toUpperCase();
let sender = message.author;
});
"msg" variable is takes the message, and turns it all into uppercase so it isn't case sensitive and "sender" variable is takes the message, and finds who the author is.
Now we can write our commands for messages.
Code:
parsbot.on ('message' , message => {
let msg = message.content.toUpperCase();
let sender = message.author;
if (msg === prefix + 'PING') {
message.channel.send('Pong!');
}
else if (msg === prefix + 'HI') {
message.channel.send('Hi' + sender);
}
});
Now I write in messages listener event if received message = "$Ping" or "$PING" bot send the "Pong!" message. And if received message = "$Hi" or "$HI" bot send the "Hi + @messagesender" message.
The coding of our app.js is for now complete for now.
Create a new discord app (For our bot)
Now create a new Discord App. At first you need to visit Discord App Developer.
In My Apps section, we click on New App.
You can add app name, redirect url, app icon and description. And click Create App button.
My application has been created successfully.
Now we clicking on Create a Bot User.
Copy the bot token for our app.js discord token part.
And paste in our app.js code discord token part.
Create new Discord Server and add our Bot
Now create a new Discord Server. At first you need to visit Discord App.
Click the "+" button.
Click the create a server button.
Write your server name, select icon whatever you want and click create button.
Our server has been successfully created.
Now we return our Discord app and click "Generate OAUTH2 URL" button.
Now select bot permissions and scopes (I select bot and Administrator). And click the Copy button.
Paste our browser.
The incoming page wants us to choose a server for our bot. I choose the Parsbot test server we created. You select our server and click verift button.
Verify google captcha.
Our bot was successfully authorized on our server.
Run and test Parsbot
Okay, now we have to run our discord bot. The bot needs to be in working order so that we can see and respond to our messages.
Open cmd and write "cd yourbotdirectory" and press enter. I write "cd C:\Users\gffdg\Desktop\Discord Pars Bot\parsbot".
Now we will run our app.js file with node.
node app.js
Parsbot started successfully and printed our message "Pars Bot started." on our ready event.
Now return our server and we are writing messages for bot commands. Our bot works nicely.
We came to the end of our education. Thank you for your attention.
Posted on Utopian.io - Rewarding Open Source Contributors
@pars11, I like your contribution to open source project, so I upvote to support you.
Thanks :)
Güzel anlatım olmuş, ellerine sağlık. Parsbot'a selamlar😂
Onunda selamı var :)
Ahahah 😄😄
misss
😄 Baya sevindin sen
:D adınıza adınıza
Dibine kadar detay var, maşallah :D Emeğine sağlık. Güzel görünüyor.
Teşekkürler hocam 😄 sağolun.
emeğinize sağlık, güzel anlatım olmuş
Teşekkürler hocam
Elinize sağlık, harika anlatım! ☺️
Çok teşekkürler 😀
Thank you for the contribution. It has been approved.
You can contact us on Discord.
[utopian-moderator]
Hey @pars11 I am @utopian-io. I have just upvoted you!
Achievements
Suggestions
Get Noticed!
Community-Driven Witness!
I am the first and only Steem Community-Driven Witness. Participate on Discord. Lets GROW TOGETHER!
Up-vote this comment to grow my power and help Open Source contributions like this one. Want to chat? Join me on Discord https://discord.gg/Pc8HG9x