Discord has continually demonstrated that it is the instant messaging platform for anybody wishing to talk, video chat, or stream with pals online during the last five years or so. Discord bots are one of the causes.
Bots can let you accomplish everything from automating monotonous jobs to start playing music throughout your server, and in this guide, we’ll teach you how to construct a Discord bot.
Although automation is the primary purpose for using a Discord bot, you can truly create one to do anything (anything that you can cram in some JavaScript code, at least).
In this tutorial, we will use Node.js to create our own basic Discord bot.
Requirements
- Discord account
- Fresh discord server
- Node.js installed on your computer.
- Visual Studio Code
- And, of course, a wifi connection.
Create a new Discord application.
To begin, we must register the application using the Discord Developer’s site. Navigate to in order to reach this portal.
Next, select the New Application button that is situated on the right side.
When you click the button, a dialog box asking you to give your bot a name will appear. In my case, I’ll just call the bot “jaysbot.”
Select the bot choice next, and then click the “add bot” button. The following dialog box will show up:
When you approve the establishment of the new application, you now have a bot.
However, I don’t think you want a dumb bot. Let’s add some additional features and improve user engagement by giving it a number of permissions.
By choosing OAuth2 from the menu, as shown below, check the boxes.
The link will be produced at the bottom of the page when permission has been added after giving the bot any further permissions.
The link you previously copied should now be pasted in any browser. The screen that follows should appear and add the server to it.
After choosing your server, press the “authorize” button:
You’ve now successfully added our bot to your server.
Making a Node.js project
Now that we have registered our bot, we must make a folder for Node.js files.
Setting up dependencies
Packages that the program needs to function properly are known as dependencies. In this instance, all we require is the discord.js package. To help with managing the Jaysbot token, we will still install the dotenv package.
This will guarantee that each call has the appropriate authorization, nodemon is also necessary for us to keep the server functioning. Enter the following command to start the project in a new terminal:
The outcomes are as follows:
The following command will install the Dotenv library and Discord.js for the project:
Use the following command to install nodemon library:
Each time we make a modification to our files, Nodemon restarts the server.
Developing our bot
In the developers’ portal, we must approve calls from Discord.js. This requires that we copy our bot’s token.
After it has been copied, put it in the manner below in a new file that you should have named.env.
Build the bot file.
Build a separate file in the main project directory called bot.js. We will use the dotenv package to set up environment variables, import classes from discord.js, and then launch a new client:
To activate the bot, enter the following command into your terminal:
Conclusion
The platform for Discord is expanding quickly. It has produced a safe means of communication and connection for bitcoin enthusiasts, business owners, and gamers.
An extra benefit is having a capable bot that can respond, ban, and mute users. This post explained how to use node.js to build a simple bot. Therefore, you can apply this understanding to include new capabilities.
Happy coding.
Leave a Reply