How to Make a Discord Bot (from scratch)

This article explains how to use the Discord.js and Node.js functions to create a Discord bot from scratch so that users can communicate directly with the Discord API. We create a Discord bot profile, receive authentication tokens for the bot, and program the bot to process commands submitted by the user with arguments.

Discord is an online chat platform that enables millions of users worldwide to send messages and communicate through voice chat in communities known as “guilds” or “servers”. Discord also includes APIs that server owners can use to create Discord bots. Discord bots can perform various activities, such as: B. Send DM messages to users, send messages to servers, play audio in voice chats, and control 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 from this article, you will have the opportunity to interact with thousands of people on your servers.

What is a Discord Bot?

Bots allow you to add features that might not otherwise appear as standard on the Discord platform. Robots that rule in toxic behavior provide entertainment that tracks activity and just scratches the surface. Anyone with their server or 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 that was developed for server-side and networked applications. If you are using JavaScript, Node JS is required to run your bot as Node JS can meet our requirements to perform server-side operations using JavaScript.

You may also like this post: How to strike out text in discord (what you should know)

Download Node.js and create a Discord account

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

You need a Discord account and your server to test your bot with. If you haven’t created one yet, go to Discord.com and create one. If you have an account, log into your account and open the server to use your bot.

You need a word-editing program like Notepad ++ in Windows to write code.

Getting started for Discord Bot

The first steps

The first step is to create the app on the official Discord website (make sure you are registered). To do this, you have to follow the link and click the “New Application” button.

In the window that appears, you need to enter a name for your application (future bot):

After you have given the name and created the application, you can proceed with creating the bot. To do this, select the “Bot” entry in the menu on the left and click 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’ll need it later.

Server preparation for Discord Bot

Server preparation

We recommend a VDS with the Ubuntu 18.04 operating system to host the bot. You can also host the bot on other distros and make sure the correct packages are installed.

Commands to install on Ubuntu 18.04:

  • sudo apt update
  • Sudo apt install Docker npm

If you use a different distribution, the structure of the required packages may differ. Instructions for a particular distribution can be found in the documentation or on a search engine.

Create base files

As stated at the beginning of this article, we’re using NodeJS to start the bot. We also need two libraries:

You may also like this post: How to join a Discord server (with instructions)

  • discord.js (the main library the bot works in);
  • dotenv (used for the configuration file).

Let’s create a working directory for the project and a package.json file. In my case, the working directory is in the / home directory, but you can choose any other one you want.

  • mkdir / home / my-discord-bot
  • cd / home / my-discord-bot
  • Touch package.json

In it we have given the name of the future package (name), the version (version), the description (description), the author (author), the main file (app.js) and a list of the dependencies.

Run npm install and make sure a package-lock.json file has been created which is also copied into the container.

Create a Docker container

Now we need to create a Docker file in the working directory that contains instructions on how to build 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 ./
  • Install RUN npm
  • COPY. …
  • EXPOSE 8080
  • CMD [“node”, “app.js”]

Let’s create a .dockerignore file and put some local files there that will be ignored:

  • Node modules
  • npm-debug.log

Let’s create 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 ready for use in a container. It remains only to upload it to the server and check its work.

For an invitation you need:

  1. Return to the link.
  2. Select the application you created.
  3. Go to the OAuth2 tab.
  4. Select “Bot” from the “Areas” list.
  5. Copy the link and follow it in the browser.
  6. Select the server that the bot should be added to.

FAQs on building a Discord bot

Are APIs allowed in Discord?

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

Can someone add a bot with a server?

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

Do I need a word processor to write code?

You need a word-editing program like Notepad ++ in Windows to write code.

Do I need the token after copying?

Yes, keep your token secret and save it somewhere – you’ll need it later.

Can I host the bot on other distributions?

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

last words

In this introductory article, we registered a new application, created a bot, wrote some simple commands, started a container, and invited the bot to the server. Also, anyone can create a Discord bot by filling it with other more complex and useful functions. Hope this material helps everyone who has long wanted to create their bot on Discord but don’t know where to start. As a master of Discord bots, want to move on to something else: How do I create a Twitter bot?

We think you might be interested too How to strike out text in discord (what you should know) This is our previous post about Discord Support.

The best smm panel for smm services is this smm panel.

Resellers looking for smm reseller panel should try this smm panel.