ManuTheCoder
ManuTheCoder's Blog

ManuTheCoder's Blog

How to make your very own discord bot using Node.JS!

How to make your very own discord bot using Node.JS!

Using repl.it

ManuTheCoder's photo
ManuTheCoder

Published on Nov 1, 2021

3 min read

Subscribe to my newsletter and never miss my upcoming articles

Let's create a discord bot using Node.JS!

Step 1

Let's go to replit.com. Sign in to your account, or sign up if you don't have an account

Once you're done, click on the "Create Repl" button in the side navigation menu

The, click "Node.JS"

You should see something like this:

If you see this screen, great job! You've set up repl.it. It's now time to code!

Step 2

For this app, we'll use discord.js, and express.js. Click on the "Shell" tab

npm install discord.js
npm install express

Step 2

Sign into your discord account on discordapp.com and create a new application here. Then add a bot user to that application:

Next save your bot's token for later use:

The last thing we need from this portal is your bot's invite URL. This determines what your bot has permission to do. At the very least it needs permission to send messages. Make sure to invite it to your server using that URL!

Then, click on the "lock" icon in repl.it's sidebar. Create an environment variable, discordToken, with the value of your token

Step 3

Let's write this code in index.js

const express = require('express');
const app = express();
const port = 3000;

app.get('/', (req, res) => res.send('Hello World!'));

app.listen(port, () => console.log(`Example app listening at http://localhost:${port}`));

// start bot code
const Discord = require('discord.js');
const client = new Discord.Client();

client.on('ready', () => {
  console.log(`Logged in as ${client.user.tag}!`);
});

client.on('message', msg => {
  if (msg.content === 'foo') {
    msg.reply('bar!');
  }
 else if(msg.content.startsWith("prefix ") {
   msg.reply(msg.content.replace("prefix ", "")
 }
});

client.login(process.env.discordToken);

Step 4 - Keeping the bot always online

Once deployed, the server will continue to run in the background, even after you close the browser tab. The server will stay awake and active until an hour after its last request, after which it will enter a sleeping stage. Sleeping repls will be woken up as soon as it receives another request; there is no need to re-run the repl. However, if you make changes to your server, you will need to restart the repl in order to see those changes reflected in the live version.

We're going to use Uptime Robot to keep our bot online, uptimerobot.com

  • Copy the url for your repl's webserver.
  • Create an account on Uptime Robot and create a monitor to ping your server every 5-10 minutes
  • Save the changes

Great! Now, you've made your very first discord bot!

Wanna test it out?

Invite your bot to a discord server, and then type "foo". You can also type "prefix text you want here" and send it. It'll give the message without the prefix

 
Share this
Proudly part of