In this article, we will explain how you can make a Discord bot from scratch using Discord.js and Node.js functions so that users can communicate directly with the Discord API. We will create a Discord bot profile, get authentication tokens for the bot, and program the bot to process user-submitted commands with arguments.
Discord is an online chat platform that enables millions of users worldwide to message and communicate via voice chat in communities called “guilds” or “servers.” Discord also includes APIs that allow server owners to create Discord bots. Discord Bots can perform various activities such as sending DM messages to users, sending messages to servers, playing audio in voice chats, and controlling servers. This allows developers to create powerful bots with advanced features, including games and even control tools. Once you learn how to generate Discord Bots with this article, you will have the opportunity to interact with thousands of people on your servers.
What Is a Discord Bot?
With bots, you can add features that might not otherwise appear standard on the Discord platform. Robots that reign in toxic behavior provide entertainment that keeps track of activity are only scratching the surface. Anyone with their server or with the appropriate permissions (Manage Server) on another server can add a bot.
What Is Node JS?
Download Node.js and Create a Discord Account
You will require a Discord account and your server to use it to test your bot. If you haven’t created one yet, go to Discord.com and create one. If you own an account, log into your account and open the server to use your bot.
You will require a text editing program like Notepad ++ in Windows to write code.
The First Steps
The first step is to make the app on the official Discord site (make sure you are registered). To do this, you need to follow the link and click on the “New application” button.
In the window that appears, you must come up with a name for your application (future bot):
After specifying the name and creating the application, we can move on to creating the bot. To do this, select the “Bot” item in the left menu and click on the “Add Bot” button.
The last step is to copy the token using the “Copy” button. Keep your token secret and save it somewhere – you will need it later.
We recommend a VDS running the Ubuntu 18.04 operating system to host the bot. You can also host the bot on other distributions and make sure the correct packages are installed.
Commands for installation on Ubuntu 18.04:
- sudo apt update
- Sudo apt install docker npm
If you are using a dissimilar distribution, the structure of the required packages may differ. You can find instructions for a specific distribution in the documentation or a search engine.
Creating Basic Files
As indicated at the beginning of this article, we will be using NodeJS to launch the bot. Additionally, we need two libraries:
- discord.js (the main library for the bot to work);
- dotenv (will be used for the config file).
Let’s create a working directory for the project and a package.json file. In my case, the working directory is located in the / home directory, but you can choose any other one you like.
- mkdir / home / my-discord-bot
- cd / home / my-discord-bot
- touch package.json
In it, we specified the name of the future package (name), version (version), description (description), author (author), main file (app.js), and a list of dependencies.
How to Change Your Bio On Discord
Your Discord bio serves as your online persona, just like your profiles do on other social media platforms. It is how you present yourself to other us...
What Is Discord?
Discord is one of the most popular group-chatting apps, with more than 100 million monthly active users. It was created as a hub for gamers and stream...
Let’s run npm install and make sure that a package-lock.json file has been created, which will also be copied to the container.
Building a Docker Container
Now, in the working directory, we need to create a Dockerfile, which will contain instructions for building the image from which the container will be launched. We will write in it:
- FROM node: 14
- WORKDIR / home / my-discord-bot
- COPY package * .json ./
- RUN npm install
- COPY. …
- EXPOSE 8080
- CMD [“node”, “app.js”]
Let’s create a .dockerignore file and put a few local files there that will be ignored:
Let’s build the image and run the container based on it:
- docker build -t authorname / my-discord-bot
- docker run -p 42150: 8080 -d authorname / my-discord-bot
Adding a Bot to the Server
The bot is up and running in a container. It remains only to invite it to the server and check its work.
For an invitation, you need:
- Return to the link.
- Select the created application.
- Go to the “OAuth2” tab.
- Select “bot” from the “Scopes” list.
- Copy the link and follow it in the browser.
- Select the server to which the bot will be added.
In this introductory article, we registered a new application, created a bot, wrote a few simple commands, launched a container, and invited the bot to the server. Further, anyone can make a Discord bot by filling it with other more complex and useful functions. I hope this material will help everyone who has long wanted to create their bot on Discord but does not know where to start. Now that you are a master of Discord bots, do you want to move on to something else: How to Make a Twitter Bot?