Parsbot | Discord Bot Tutorial Node.js | Part 1

in #utopian-io6 years ago (edited)

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


1.png

Image Source


Useful Links

Node.js Website

Node.js Github

Parsbot Github


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


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.

1.png


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.

2.png

Okey now click "Add workspace folder...".

3.png

Select your workspace folder and click the add button.

4.png

The workspace folder must have been successfully installed. I will use github for version control so I added license file and readme files.

5.png

Now we click on the new file button above your workspace folder to create app.js file.

6.png

We write app.js and press enter.

7.png

We successfully created our app.js file. Now I close the welcome page.

8.png

Our app.js file is ready for coding. Now I write console.log code for test.

Code:

console.log("App.js Test!")

9.png

Save the app.js. (Ctrl+S)

10.png

Now open the cmd write "cd your workspace folder directory" and press enter.
I write "cd C:\Users\gffdg\Desktop\Discord Pars Bot\parsbot".

11.png

Now we will run our app.js file with node.

Command Prompt Code(Run file)

node app.js

12.png

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

13.png

Type

Command Prompt Code(Npm install discord.js)

npm install discord.js

command.

14.png

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

15.png

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

1.png

We need to define a prefix for message control.

Code:

const prefix = '$';

I write "$" you can change it to whatever you want.

2.png

Now we create a listener event for messages runs whenever a message is received.

Code:

parsbot.on ('message' , message =>{

});

4.png

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.

5.png

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.

6.png

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.

1.png

In My Apps section, we click on New App.

2.png

You can add app name, redirect url, app icon and description. And click Create App button.

3.png

My application has been created successfully.

4.png

Now we clicking on Create a Bot User.

5.png

Copy the bot token for our app.js discord token part.

6.png

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.

7.png

Click the "+" button.

8.png

Click the create a server button.

9.png

Write your server name, select icon whatever you want and click create button.

1.png

Our server has been successfully created.

2.png

Now we return our Discord app and click "Generate OAUTH2 URL" button.

3.png

Now select bot permissions and scopes (I select bot and Administrator). And click the Copy button.

4.png

Paste our browser.

5.png

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.

6.png

Verify google captcha.

7.png

Our bot was successfully authorized on our server.

8.png


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

9.png

Now we will run our app.js file with node.

node app.js

11.png

Parsbot started successfully and printed our message "Pars Bot started." on our ready event.

12.png

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

Sort:  

@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😂

121212.png

Onunda selamı var :)

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

  • You have less than 500 followers. Just gave you a gift to help you succeed!
  • Seems like you contribute quite often. AMAZING!

Suggestions

  • Contribute more often to get higher and higher rewards. I wish to see you often!
  • Work on your followers to increase the votes/rewards. I follow what humans do and my vote is mainly based on that. Good luck!

Get Noticed!

  • Did you know project owners can manually vote with their own voting power or by voting power delegated to their projects? Ask the project owner to review your contributions!

Community-Driven Witness!

I am the first and only Steem Community-Driven Witness. Participate on Discord. Lets GROW TOGETHER!

mooncryption-utopian-witness-gif

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

Coin Marketplace

STEEM 0.26
TRX 0.11
JST 0.033
BTC 64107.21
ETH 3073.84
USDT 1.00
SBD 3.88