How to Make a Discord Bot Using NodeJS

by thepcgeek in Circuits > Computers

210 Views, 2 Favorites, 0 Comments

How to Make a Discord Bot Using NodeJS

Thumbb.png
Result.PNG

Hello there!

This guide will show you how to create a simple bot in NodeJS. This guide is mainly for beginners to Discord bots. Keep in mind you must already know basic JavaScript in order to continue developing your bot. We will try to explain things as simple as possible and we hope you have fun creating a bot!

Let's get started!

Creating the App on Discord Developers

bots.PNG
createApp.PNG
createBotUser.PNG
copyTokenAndMakePublic.PNG

Before you can start programming your bot, you must create an application first. To do this, head on over to https://discordapp.com/developers/applications/me Note that you must be logged into your account before continuing (and it must be verified).

The pictures shown in this step are in the correct sequence for showing the process of creating a bot.

Here's what you need to do to create your app:

1. Navigate to Discord Developers and create an app.

2. Give it a name, description, and avatar. The avatar is optional, but is recommended.

3. Once created, convert it into a "Bot User". This will allow it to act like a normal user account.

4. Go to the app page and store your token somewhere (you'll need it later). Keeping it in a text file is also a good idea. Note that you must never share your token or someone else may use your bot to log in!

Congratulations, you have created your bot app! Now, it's time to code!

Installing NodeJS

Before we start coding the bot, you may need to install (or update) Node.js (Node for short). Node is a JavaScript runtime based on the powerful V8 engine developed by google. To install it, head on over to https://nodejs.org/en/download/ and select the appropriate binary for your platform. Once downloaded, install it by opening it.

Creating a Simple Bot

Now that you have got Node installed, you will need to create a folder to store your bot files. We suggest you to store it in a location you will have easy access to, to save time. Once created, open a terminal (or command prompt) window and navigate to that directory. Before you start, you will need to run a couple of commands. Here's what you need to run:

1. npm init -y

2. npm install discord.js --save

3. [Windows Only: copy nul index.js], [Other Platforms: touch index.js]

You have now successfully prepared your bot environment, hooray!

Creating a Simple Bot (continued)

VSC.PNG

Now you will need to open your favorite text editor (we suggest Visual Studio Code for JavaScript) and open the file index.js. If the file index.js is not present in your bot directory, create it. You are now ready to edit your code and create your bot. Here is a simple bot we made in under 5 minutes to get you started (image attached).

To run it, use command [node index.js] to run it (without brackets).

Now that it is running, you need to add it to your server. For this, we will need to generate an invite link for the bot. To do this, head back to the developer panel and select your bot. From there, click the "Generate OAuth2 Url" button and click the "Copy" button. Your bot currently doesn't need any permissions so copy it as-is.

To add it, navigate to the invite link and select your server, complete the captcha and you're done!

Source code: https://pastebin.com/snJZBM0V

Finshing Up

Result.PNG

You have successfully completed the guide to creating a simple Discord bot! The expected results are attached to this step and hopefully you'll have fun creating your own, unique bot! The sample bot includes one command which is "hello" (as shown in the picture above) and is fully customizable.