How to Make a Discord Bot
Discord has repeatedly demonstrated over years that it is an online messenger tool of choice for anybody wanting to communicate, video conference, or broadcast with pals on the internet. Discord bots are behind this. They can assist you with everything from automating tedious activities to playing songs from your server, and we'll teach you how to build a Discord bot in this article.
While automation is the primary benefit of using a Discord framework, you can design it to do just about anything you want. You do not need any special coding skills to start. Follow the steps below to create your own Discord bot with mostly no coding skills required.
Written by grex on 09/06/2021.
Your First Discord Bot: A Step-by-Step Guide
In this tutorial, we will use discord.js as library for our code examples. There are many other libraries out there, but since Discord.py will stop being maintained, discord.js will remain the most popular library for the development of Discord bots.
-
Install Node.js and create a Discord account and server
Node.js is an open and free open-source JavaScript runtime that is essential for the smooth operation of your bot. Before you begin working on it, go to nodejs.org and install the software.
If you don't already have one, create an account with Discord and set up your server to deploy your bot. Log into your Discord account and pick a server. We recommend creating a new personal server where you can test your commands without disturbing others. To program, download a text editing application, such as Notepad++. For a more advanced coding experience, use a code editor like Visual Studio Code.
-
Create your bot
For your bot to work on Discord, creating an "application" is essential. This requires some effort, but it isn't very difficult. The aim is to obtain an "authorization token", which will allow Discord to identify your program and integrate it into your bot's servers.
Check out Discord's Application page to get started. Navigate right to your user account's list of apps if you're signed in. To begin, select New Application. Name your bot and click the Save Changes option. Then select Bot from the right-hand panel. Select Add Bot. It should show immediately if you've got only a single app - the one you just created. If not, click it.
Discord Application View - Bot Settings -
Get the authorization token of your bot
Under the section "Token", you'll find a link named Click to Reveal Token. When you select the link, you'll see a string sequence of random characters. This is the authorization token for your bot. Keep it a top-secret. If you believe your token is hijacked, the Generate a New Token option allows you to quickly create a replacement for it. Make a note of your token. It'll come in handy in a flash.
-
Invite the bot to the server
To connect the bot to your server, it has to be invited. Go to the OAuth2 section of your discord application and scroll to the Oauth2 URL Generator. This tool will help you creating an invite link for your bot that looks similar to this:
https://discord.com/api/oauth2/authorize?client_id=CLIENT_ID&permissions=0&scope=bot%20applications.commands
Make sure to use the correct client ID of your application in the link. The generator will do that for you automatically
Discord Bot Invite Link GeneratorCopy the link, open a new tab and paste the link that you just created. That will lead you to a page that will instruct you on where to send your bot. When launching Discord in your browser or an app, go to the server. Your channel will announce that a bot has entered the chat.
-
Create a folder called "Bot" on your computer.
As you are doing this, make a directory in an easily accessible location on your PC to keep all of your bot's data. Name it "MyBot" or "DiscordBot."
Launch the bot's scripts in your text/code editor. In the following, you will create three files called
auth.json
,package.json
andbot.json
for your basic bot file structure.Start with creating a new file, name it
auth.json
and insert the following code below. Make sure to replace Your Bot Token with your secret bot token you took note of before.{ "token": "Your Bot Token" }
Then createpackage.json
and insert the following code. As this file is only listing meta data, you can change the names as you like. Just make sure that thebot.js
file name matches your third filename.{ "name": "MyBot", "version": "1.0.0", "description": "My First Discord Bot", "main": "bot.js", "author": "Your Name", "dependencies": {} }
-
Create a program for the bot.
There is one final file to create, and it's a file that dictates the bot's actions. To truly have complete control of the bot, you'll need to be comfortable using JavaScript, but you want to copy and paste this script to file to create a basic bot if you are not a programmer.
var auth = require('./auth.json'); // Import libraries const Discord = require('discord.js'); const client = new Discord.Client(); // Event listener when a user connected to the server. client.on('ready', () => { console.log(`Logged in as ${client.user.tag}!`); }); // Event listener when a user sends a message in the chat. client.on('message', msg => { // We check the message content and looks for the word "ping", so we can have the bot respond "pong" if (msg.content === 'ping') { msg.reply('pong'); } }); // Initialize bot by connecting to the server client.login(auth.token);
This program creates a simple Discord bot which response to
!ping
will bepong
. Afterwards, you can start replacing "ping" and "pong" with the functionalities you like.Launch the "Command Prompt" on your PC and go to the Discord bot directory. Quickly access the Command Prompt on your PC. Input the term "cd" accompanied by your folder's directory.
-
Deploy the bot's dependencies using your PC's Command Prompt.
To put Node.js to work right now, write "npm install discord.js" in your PC's Command Prompt. This command will install the needed files of the library right into a subdirectory.
-
Launch your bot
You're all set now. In the Command Prompt, execute "node bot.js" to test executing the bot. Return to the Discord server and type "!ping" and the prompt you prepared in the "bot.js" file to check if your bot is functioning. If you programmed the bot properly, this instruction will force the bot to respond with the text you entered. You're now a proud developer of a Discord bot, so congrats on the new win!
Recent Articles
Popular Bots
15,100,000 Servers
10,700,000 Servers
10,100,000 Servers
9,770,000 Servers
9,050,000 Servers