How to create a bot in Diskord: instructions for writing your own assistant on the server

There was a time when people did everything themselves. Of course, they could entrust hard physical work to automated machines. But the mental operations, where you need to make a logical analysis, there was no one to do it. Recently, this problem has been solved by programs with ready-made logical instructions, called bots. Here’s how to create your own bot in the Discord service, which is considered one of the best messengers in 2022.

Where to Start

Maybe somewhere on the Internet there are “walking” ready-made bots (sometimes called Creator), but they are quite limited in functionality. Since it is impossible to predict all the desires of the user. Therefore we will consider the development of the Discord bot based on the JavaScript (JS) programming language, more specifically the Node.js platform. The first thing to do is to create an application in the Discord database.


To do this, follow these steps:

  • Follow this link. We recommend that you do this not on your phone, but through your computer.


  • Click on the “New Application” button.

  • Enter a name for the application. In the future it will appear as the name of the bot.


  • Go to the “Bot” section, which is on the left navigation menu.
  • Click on the “Add bot” button. Confirm the action. If an error occurs, then most likely the name of your application has already been used and you need to come up with a new one. You can edit it in the “General” section.


  • Find the “Token” field. Click on the link below it. A long text consisting of different characters should appear. This is the token by which you access the bot.
  • Copy it and save it somewhere. Without it, you won’t be able to call the bot. We also recommend that you don’t show this token to anyone, since this someone will be able to control your app.


Done. We have already prepared a place for the application. It remains to install the code itself on the dedicated hosting.

Preparing the Server

In our case we will use a VDS (Virtual Private Server) with Ubuntu 18.04. If you are a beginner, we recommend that you choose this distribution. The server itself needs to be rented. You can also find free hosting for training purposes. To prepare the server, do the following steps:

  • Start the server and open the terminal. In Ubuntu it is enough to click on the desktop and select “Terminal”.
  • Enter the following commands:
    sudo apt update

sudo apt install docker npm.

Thus we installed npm, which is needed to install node.js packages. Already unclear? Then we recommend reading a basic course on JS and Node.js to get started. However, if you follow the instructions completely, you won’t need any additional material to learn.

Creating basic bot files

Now let’s move on to working out the program code itself. Any executable code must be placed in a text file in a separate directory. A detailed algorithm of the environment preparation:

  • Open a terminal.
  • Go to your home directory with the command “cd /home”.
  • Create a directory with an arbitrary name. In our case this will be mkdir newbot.


Done, the environment is prepared. Now move on to the files.

Adding commands.

First create a package.json file in this directory with the command “touch package.json”. With a text editor fill in the file with the following lines:


“name”: “*here specify the name of the bot*”,

“version”: “*its version (at your own discretion) *”,

“description”: “*description that users will see*”,

“author”: “author’s email address”,

“main”: “main.js”,

“keywords”: [



{ “dependencies”: {

“discord.js”: “^12.1.1”,

“dotenv”: “^8.2.0”




Note the line “”main”: “main.js”. Inside the quotation marks you specify the executable file. In our case it is main.js. Create a file with that name in the same directory, and then enter the following ready-made code:


const Discord = require(‘discord.js’);

const bot = new Discord.Client();

const token = process.env.token;

const prefix = process.env.prefix;


bot.on(‘ready’, () => {`Logged in as ${bot.user.tag}!`);`Current prefix: ${prefix}`);


bot.on(‘message’, message => {

if (message.content === `${prefix}hello`) {




bot.on(‘message’, message => {

if (message.content === `${prefix}server`) {

console.log(message.content,;`Society: ${}{participants: ${message.guild.memberCount}`);




This is the simplest script that will greet the user and on the command “Server” display the number of participants. Now create a .env file, in which you should write the following lines:

token=*token, which was copied when you create the bot*

prefix=”*character from which commands will start*”.

The asterisks indicate the places where you need to enter your data. These asterisks don’t need to be written when preparing your code.



In fact, the script above doesn’t interact with the standard JS interface, but “communicates” directly through the discord API. Consequently, you can’t test its functionality right away. The only thing the user can do at this stage is to check if the code will run at all. To do this, enter the command “npm start” from the terminal, in the directory with the project. If there are no errors, then move on to the next step.

Docker container

Docker is a special environment for a project that allows you to run code regardless of the parameters of the underlying system. It also helps to build the project itself and run it locally on the server. To do so, create a Dockerfile with the following content:

FROM node:14

WORKDIR /home/newbot

COPY package*.json ./

RUN npm install



CMD [“node”, “app.js”]

Now create a .dockerignore file to store components not used during the build. Add these lines to it:



Use the following commands to start and build:

docker build -t author/test

docker run -p 42150:8080 -d author/test


How to add your bot to the server

You can breathe easy, this is the end of the difficulties. Adding it will not take more than 1 minute. To install (from the word “install”) go back to the application creation window. But now go to the “OAuth2” section. In the “Scopes” section, select the “Bot” item. The service will give you a link to paste into the address bar.


Next, you will be asked in which server to add the bot. When these procedures are complete, you can safely test your creation. So far the bot can perform only one command: “!server”. Instead of “!” the prefix you used in the script will be given. Ideally the following string should return:

Community: *community name

Participants: *number of participants

And that’s it. Don’t forget that when your hosting lease expires, the app will also expire.


Related Posts:
Share to friends
Mobile Pedia
Mobile Pedia