How to Make a Discord Bot (From Scratch)

How to Make a Discord Bot (From Scratch)

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

What Is Node JS?

Node.js is an open-source runtime environment developed for server-side and networked applications. If you will be using JavaScript, Node JS is required to run your bot because Node JS can meet our needs to perform server-side operations with JavaScript.

Download Node.js and Create a Discord Account

Node.js is an open-source and free JavaScript runtime, and you will need it for your bot to run. Download this from nodejs.org and install it before starting anything else.

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.

First Steps for Discord bot

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.

Server Preparation for Discord bot

Server Preparation

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.

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:

  • node_modules
  • npm-debug.log

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:

  1. Return to the link.
  2. Select the created application.
  3. Go to the “OAuth2” tab.
  4. Select “bot” from the “Scopes” list.
  5. Copy the link and follow it in the browser.
  6. Select the server to which the bot will be added.

FAQs on Making a Discord Bot

Are APIs allowed on Discord?

Yes, Discord includes APIs that allow server owners to create Discord bots.

Can anyone with a server add a bot?

Anyone with their server or with the appropriate permissions (Manage Server) on another server can add a bot.

Will I need a text editing program to write a code?

You will need a text editing program like Notepad ++ in Windows to write code.

Will I need the token after copying it?

Yes, Keep your token secret and save it somewhere – you will need it later.

Can I host the bot on other distributions?

Yes, You can host the bot on other distributions and make sure the correct packages are installed.

Final Words

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?

We think that you may also be interested in which is our previous post about Discord Support.
[Total: 29 / 5]
Ralph

Posts: 248

After working for so many years in the screenwriting business, Ralph decided that he needed to be more involved with the digital world. Therefore he became a content writer by putting his skills into practice after learning about subjects he's interested in, mostly social media.

1 Comment

Your email address will not be published. Required fields are marked *