How to Make a Discord Bot

How to Make a Discord Bot in 2022 (Easy Guide)

In case you are a Discord consumer, it’s essential to have seen a number of Discord bots on in style servers. Whether or not it’s a music bot or a server moderation bot, there’s a bot for nearly every thing. However in case you have an interest to create a Discord bot your self, observe our information to make a easy Discord bot in 2022 and past.

Create a Discord Bot (2022)

Conditions to Make a Discord Bot

download nodejs

We have been initially planning to make use of the favored Discord.py Python bot for this information. Nonetheless, considering the future of the library, we now have determined to make use of the discord.js Node library as a substitute. Therefore, ensure you have put in Node.js in your PC. You may obtain the newest model of Node.js from the official web site (visit). You may run the next instructions to just be sure you’ve correctly put in Node.js and npm in your PC:

node -v
npm -v

To effectively view, write, and modify code, you should use any of those greatest textual content editors for coding. If we have been to select one, we’d go together with Microsoft’s Visible Studio Code (download), which additionally has a wide range of VSCode themes.

Set Up Discord Bot Account and Add to Discord Server

1. Go to Discord’s developer portal and register along with your Discord account. As soon as you’re there, click on on the “New Utility” button on the top-right nook to get began.

create new application discord

2. Enter a reputation to your utility and click on on “Create”. Do notice that the applying identify doesn’t essentially need to be the identical because the bot’s identify. You may select to vary the bot’s identify later.

name and create application

3. Now, change to the “Bot” part from the left sidebar and click on on “Add Bot”.

add bot discord dev

4. When the pop-up seems, click on “Sure, do it!” to proceed additional.

confirm adding bot to app

5. Discord has now created your bot. Now you can select to vary the bot’s identify and icon from the “Bot” part. Earlier than that, click on on the “Copy” button beneath “Token” to repeat the bot’s token. The bot’s token is required later. Do remember that you shouldn’t share your bot’s token with anybody because it grants them entry to your bot.

copy token

6. Now, develop the “OAuth2” tab from the left sidebar and change to the “URL Generator” part. Right here, select “bot” because the scope and permit acceptable permissions for the bot. For this demonstration, we’re giving admin permissions to the bot.

choose scope and bot permissions

7. Scroll down and click on on the “Copy” button to repeat the bot’s URL.

copy bot url

8. Go to the copied URL out of your browser so as to add the bot to your Discord server. All it’s important to do is select the server from the “Add to Server” record and click on “Proceed” so as to add the bot.

add your bot to server

Create and Host Discord Bot Regionally on Your PC

Now that you’ve got added the bot to your Discord server, it’s time to configure the bot. Simply observe the steps beneath to regionally host a Discord bot in your PC.

1. Create a brand new folder wherever in your PC. It’s best to then create two recordsdata on this folder — .env, bot.js. Within the .env file, paste the bot’s token you copied earlier within the following format:

DISCORD_TOKEN= Paste your token right here with out quotes
paste token in env file

2. Up subsequent, add the next code to your bot.js file. With this code, the bot will reply “pong” at any time when a consumer sends “ping”.

require('dotenv').config();
const Discord = require("discord.js");
const consumer = new Discord.Consumer({intents: ["GUILDS", "GUILD_MESSAGES"]});
consumer.on("prepared", () => {
  console.log(`Logged in as ${consumer.consumer.tag}!`)
})
consumer.on("message", msg => {
  if (msg.content material === "ping") {
    msg.reply("pong");
  }
})
consumer.login(course of.env.DISCORD_TOKEN);

discord bot code

4. Now, set up the Discord.js library utilizing the next command:

npm set up --save discord.js dotenv
install discordjs lib

5. Up subsequent, you need to create a package deal.json file utilizing the command “npm init -y”.

create package json

6. You may lastly use the command “node bot.js” to run your Discord bot.

run your bot

7. As you possibly can see beneath, the bot works as supposed and has replied to my check message.

bot output

Create and Host Discord Bot within the Cloud

If you happen to would fairly host your Discord bot on-line, we suggest utilizing Replit. For individuals who don’t know, Replit is a web-based IDE, the place you possibly can run and host over 50 programming languages. As well as, the method to arrange Replit is pretty easy too. So let’s get proper into it.

1. It’s best to first join a brand new Replit account. You may use your electronic mail handle or proceed along with your Google, GitHub, or Fb accounts.

sign up replit

2. Click on on the “Create” button on the top-left nook to create a brand new undertaking.

create new replit project

3. From the pop-up that seems, select Node.js template, identify your undertaking, and click on on “Create Repl”.

create repl

4. Search for a lock image within the left sidebar and paste the bot’s token you beforehand copied. It’s best to paste the token within the “worth” area and set the token identify within the “key” area. Click on “Add new secret” to substantiate the token and add it to your Node.js code.

add your token replit

5. Paste the next code and press the inexperienced “Run” button on the high to run the undertaking. Now you can use your bot even after shutting down your PC.

paste the code and run
const mySecret = course of.env[`TOKEN`]
const Discord = require("discord.js");
const consumer = new Discord.Consumer({intents: ["GUILDS", "GUILD_MESSAGES"]});
consumer.on("prepared", () => {
  console.log(`Logged in as ${consumer.consumer.tag}!`)
})
consumer.on("message", msg => {
  if (msg.content material === "ping") {
    msg.reply("pong");
  }
})
consumer.login(course of.env.TOKEN);

6. You may come again to the Replit undertaking and click on on the “Cease” button to cease the bot. And there you’ve it. You may modify the bot’s code so as to add or take away the options you want.

stop replit

Regularly Requested Questions

Q: Can I create a Discord bot totally free?

Sure, you possibly can create a Discord bot totally free and host it regionally in your PC or within the cloud.

Q: The way to make a Discord bot with out coding?

If it’s important to create a Discord bot with out coding, you’ll have to search for pattern Discord bot initiatives others have executed and modify the bot based mostly in your wants. If that proves to be a trouble, you possibly can at all times use one of many out there greatest Discord bots to enhance your server’s capabilities.

Q: Can I exploit Discord.py for creating my Discord bot?

The developer of the favored Discord Python library “discord.py” isn’t pleased with Discord’s obligatory transition to slash instructions for verified bots from April 2022 and has determined to deprecate the library. The library is not underneath improvement, and the GitHub page of Discord.py is now in read-only mode. Whereas unverified bots usually are not affected in the intervening time, that might probably change sooner or later.

Create Your Personal Discord Bot

So, that’s how one can create a bot on Discord. Whereas we now have proven a easy bot that responds to a textual content, the probabilities of customizing a bot’s capabilities are infinite. In order for you inspiration or concepts to your new Discord bot, undergo our article on the very best Discord bots and check out implementing the options you want probably the most.

Leave a Comment

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