DEV Community

Ankita Sahu
Ankita Sahu

Posted on

A Two Steps Easy Discord Bot Creation

Step 1: Create a Bot User
a) Sign into your discord account on https://discordapp.com and create a new application by heading to discord developers page. Then add a bot user to that application

b) Next save your bot's token for later use:
Save your bot's token

c) 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!
Get your bot's invite url.

Step 2: Head to Repl.it
If you haven't heard of it Repl.it is an online IDE of sorts that lets you create and share small projects. It's pretty amazing and has constantly been adding features to help you do more online. We call these repls.

a) Create a new repl named JavaScript .
Create a new Nodejs Repl

b) First thing we need to make sure a webserver is running in our repl. Repl.it will kill a running repl when you close the browser tab unless it's serving web content. Then Repl will keep it alive for an hour even if you close the tab. Paste the following code in your repl and Repl.it will automatically install packages for you and start an express webserver.

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}`));

Enter fullscreen mode Exit fullscreen mode

If you look to the left, a file called package.json should have appeared.
packagejson
This file holds any packages you require in your repl. Plus any other scripts we might make. Just like a regular NodeJs project.

I chose express as my webserver. If you're working with Python you'd probably use Flask, Ruby would have Sinatra. For java however I would recommend investigating com.sun.net.httpserver.HttpServer for a speedy start up time.

c) Next we need to instantiate our bot.

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}`));

Enter fullscreen mode Exit fullscreen mode

// ================= 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 === 'ping') {
    msg.reply('pong!');
  }
});

Enter fullscreen mode Exit fullscreen mode

// You really don't want your token here since your repl's code
// is publically available. We'll take advantage of a Repl.it
// feature to hide the token we got earlier.
client.login(process.env.DISCORD_TOKEN);
ping pong is sort of like the hello world for bots. Once we start this up you should see your bot online in your server. If you send the word ping in your server the bot should reply with pong.

After you've pasted that code snippet in, don't click restart yet!

d) Create a .env file

On the left create a file called .env, the content of the file should be:

DISCORD_TOKEN=your_token

Enter fullscreen mode Exit fullscreen mode

This will help us hide your token from the rest of the world. Read more about it here in Repl.it's Docs.

Now that the proper credentials are in, you can click restart now. Your bot should be online!
bot online
it alive boss

Congrats! You can peruse the discord.js documentation to implement all the cool bot features you can dream of!

Top comments (0)