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.

screenshot_1

To do this, follow these steps:

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

screenshot_2

  • Click on the “New Application” button.

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

screenshot_4

  • 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.

screenshot_5

  • 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.

screenshot_6

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.

screenshot_8

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”: [

“nodejs”.

],

{ “dependencies”: {

“discord.js”: “^12.1.1”,

“dotenv”: “^8.2.0”

}

}

screenshot_9

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:

require(‘dotenv’).config();

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

const bot = new Discord.Client();

const token = process.env.token;

const prefix = process.env.prefix;

bot.login(token);

bot.on(‘ready’, () => {

console.info(`Logged in as ${bot.user.tag}!`);

console.info(`Current prefix: ${prefix}`);

});

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

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

console.log(message.content, message.author);

message.channel.send(‘hello!’);

}

});

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

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

console.log(message.content, message.author);

message.channel.send(`Society: ${message.guild.name}{participants: ${message.guild.memberCount}`);

}

});

screenshot_10

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.

screenshot_11

Testing

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

COPY .

EXPOSE 8080

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

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

node_modules

npm-debug.log

Use the following commands to start and build:

docker build -t author/test

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

screenshot_12

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.

screenshot_13

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.

screenshot_14

Related Posts:
Share to friends
Mobile Pedia
Mobile Pedia