Learn to Code via Tutorials on Repl.it!

← Back to all posts
How to make a Discord Bot | Part 2
RocketSpot

How to Make a Discord Bot | Tutorial #2

Intro:

So I am guessing that you want to make a Discord Bot that functions, eh?
Well, your at the right place!

The actual tutorial:

Step #1:

Create a repl. Create a repl so you can code the Discord Bot! Make sure the language is Node.js, otherwise, the bot won't work. :/

(You really don't need to listen to my explanations of each piece of code.)

Step #2:

The coding. So let's start with the packages and variables.

// PACKAGES
const keepAlive = require("./server.js")
const Discord = require('discord.js');
const client = new Discord.Client();
const fs = require('fs');
const PutItUpHere = process.env['TOKEN']

// PREFIX
const prefix = 'PrefixGoesHere';

(keepAlive will be the way we make your bot online, so don't change it please. Discord is so the bot can login to Discord and talk! client is the Discord Client. PutItUpHere is where you will put the token. Make sure to go to the environment section of the repl, make the name, "TOKEN' and in the value section, put the token of your bot. It can be found if you go to the Discord Developer Portal, select your bot that you are working with, go click the "Bot" button on the side bar, than click "Copy Token". Than paste it into the value section on the repl. prefix is what command your bot will respond to. It makes sure you use the right prefix, not the wrong one. So change PrefixGoesHere to a prefix of your choice.)

// COMMANDS
client.commands = new Discord.Collection();

(This variable will make commands, so the bot can answer certain commands)

// FILTER ONLY .JS FILES
const commandFiles = fs.readdirSync('./commands/').filter(file => file.endsWith('.js'));
for(const file of commandFiles){
const command = require(./commands/${file});

client.commands.set(command.name, command);
}

(This will make sure that if you accidently add a different type of file that will effect the bot, it will not effect it unless it is a .js file. But still, the commands will be attached manually not automatically.)

// STATUS/ACTIVITY SET
client.on('ready', () => {
setInterval(() => {
client.user.setActivity(${client.guilds.cache.size} Servers & ${client.users.cache.size} Users, { type: 'WATCHING' })
},5000);});

(This will let you and everybody else know how many servers the bot is in and how many people it is serving. You don't need to put this, but I suggest you do.)

Step 3:

Making the bot stay alive. Make a file in the repl called "server.js".

Inside the file, paste this piece of code inside it.

keepAlive()

Step 4:

Making our first command. Under the keepAlive() code, paste paste this code.

// COMMANDS
client.on('message', message => {
if (!message.content.startsWith(prefix) || message.author.bot)return;
const args = message.content.slice(prefix.length).split(/ +/);
const command = args.shift().toLowerCase();
if(command === 'test'){
message.channel.send("Test successful. Bot online, up and running!");

Next, make a folder in the repl and name it "commands". Make sure it is all lowercase, otherwise, it would not work.

Step 5:

Now lets make our second command/last commands for this part.

Let's make a simple ping command.

Right under the test command (Like on the next line), paste this code.

else if(command === 'ping'){
client.commands.get('ping').execute(message, args, Discord);
}

Now, create the file in the "commands" folder called "ping.js". Paste this code in the "ping.js" file.

module.exports = {
name: 'ping',
description: 'Gives you the bot\'s latency',
execute(message, args, Discord){
message.channel.send("Running through bot\'s database...").then(msg => {
const ping = msg.createdTimestamp - message.createdTimestamp;
let exampleEmbed = new Discord.MessageEmbed()
.setTitle("🏓 **Pong!** 🏐")
.setDescription(``The bot\'s ping is ${ping}! Now let\'s continue playing out game of Ping Pong!'``)
.setColor("RANDOM")
.setTimestamp()
.setFooter("The bot is running fine and smoothly!")
message.channel.send(exampleEmbed);
})
}
}

Now in "index.js", paste this code right under (Like literally on the next line).

});

client.login(PutItUpHere)

Step 6:

Go to UptimeRobot and signup for an account. If you already have an account, please login to it. After that, create a new monitor, than go name it whatever you want, than the website should be to site you see on the top right corner of the repl. The URL should look like this:

https://YourReplName.YourUsername.repl.co

(If there is an error please comment and tell me what is wrong.)

Next publish the monitor. Now you can close both the UptimeRobot and the repl tab.

Step 7:

Now, open Discord. Than go to the Discord server where you invite your bot. You can type these two commands:

PrefixGoesHeretest

PrefixGoesHerePing

(These should work, so if there is an error, please contact me in the comments sections. Others might help you too if I am late.)

Change PrefixGoesHere to the prefix you set in the repl.

Conclusion:

Great job! You finished Part 2. I will release Part 3 one this reaches 30 upvotes (Since the last one got 25 the last time I checked). If you have any questions, please put them down in the comments, I'll try to answer ASAP. See you in the next tutorial. (P.S. Please tell me about anything if you are concerned about anything or if you have a problem.)

Code that you may fork if you want: https://replit.com/@RocketSpot/How-to-Make-a-Discord-Bot-or-Part-2s-Source-Code

Voters
Rowci00
MascotGenuine
joandersonso
eduardogarza4
365DaysOfCode
Stal6
itsHIPEOPLE
KingStoleGame
JuanCarlos32
RocketSpot
Comments
hotnewtop
Pfannerstill

Thanks to this article I can learn more. Expand my knowledge and abilities. Actually the article is very real.

https://www.targetpayandbenefits.review/

RocketSpot

@Pfannerstill bruh it looks like an ip grabber

itsHIPEOPLE

I forked your code, but it says Syntax Error: Unexpected token '?' in the discord.js RESTManager.js line 32.

Skripthut

@itsHIPEOPLE discord.js requires Node 16+ now, there was an announcement in the repl.it Discord server showing how to update

itsHIPEOPLE

@Skripthut
ok thanks

KingStoleGame

it's saying that I am Missing an initializer in const declaration?

RocketSpot

@KingStoleGame Just fork the repl. If you did, please let me edit the repl to see what is going on

KingStoleGame

@RocketSpot Forked it still nothing.

RocketSpot

@KingStoleGame share the repl with me

KingStoleGame

@RocketSpot There should've gotten access.

RocketSpot

@KingStoleGame This error wasn't because of the code I gave you. You put the wrong code in the wrong places and did the wrong things. I fixed the mistake you made but idk why it is still showing an error.

KingStoleGame

@RocketSpot Okay thank you so much! I will check it out and respond an update.

RocketSpot

@KingStoleGame your welcome

KingStoleGame

@RocketSpot Now it's saying "Unexpected Token '-:-' Did you see this when you were editing it?

RocketSpot

@KingStoleGame Yes I did, I told you I don't know how to fix it because it was an error with discord.js

KingStoleGame

@RocketSpot Ok just had to confirm.

RocketSpot
Skripthut

You do realize there's multi-line code blocks in markdown, right?

RocketSpot
Highwayman

the syntax for code blocks is:

```language_name
code
```

for example:
```js
// this is some code:
console.log("hello, world!");
```

@RocketSpot

RocketSpot

@Highwayman ight thanks