Create a simple Discord Bot using NodeJS

This is supposed to be a thumbnail

Making a Discord bot has a lot of guides everywhere. So why don't I make a guide too? In this guide, we'll be making a Discord bot using a programming language called JavaScript.

Disclaimer

This is in no way a guide that would allow you to make a decent public bot. This is barely an introduction to Discord bot development.

Prerequisites

  • Something you can type code in.
  • A good PC. You could also opt to use something like Heroku or Repl.it but I ain't familiar with those.
  • NodeJS (v12x to support ESM syntax). You can easily install NodeJS on your pc from https://nodejs.org/en/. Cool, right?
  • A Discord bot.

Creating the Discord bot

  • Move over to Discord Developer Portal
  • Click on the "New Application" button on the top right. New Application
  • Give it a good name and a profile pic. Add Details
  • Head over to the bot page. Bot Page
  • Copy the token. Token
  • Also don't forget to invite the bot to your server for testing. The invite URL can be created in the OAuth2 tab under the URL Generator page.

Getting started

Now that you have all the prerequisites ready, let us begin making the actual bot.

Create a new folder

Create a new folder to make your bot in. Make sure it is an empty folder.

Open the command line.

If you use Windows, this is your command prompt or powershell. If you use GNU Linux, you'd know what a command line is.

Navigate to your folder through

cd <path-to-your-folder>

like

cd /home/mydumbpc/projects/discord-bot

Install Eris

We will be using Eris for this guide. Why? Because it is simple.

npm install --save eris

This should install Eris in your folder. You should now see a package.json file and a node_modules folder in it.

Create your main file

  • Create a file with a .js extension. Name it whatever you want. For ease of explaining, I'll name mine coolbot.js.
  • Create another file
  • Let's name this token.js.

The content should be

export default 'your discord bot token here'

Yes, the token you copied a while ago.

Messing up package.json a bit

  • Open package.json.
  • Enter the following line above your dependencies line.
"type": "module",
  • This line makes sure that your program can use stuff like import and export.

Start coding

Creating your bot file

Inside your coolbot.js,

import Eris from 'eris'

We first import Eris into your bot. Eris is what we use to connect your bot to Discord.

Next, we import the token you saved in token.js.

import TOKEN from './token.js'

Now you have Eris and your token ready. Let's move on to the bot part now.

const myCoolBot = new Eris.Client(TOKEN)
  • Here, we are creating a new "Client", which will be used to connect to Discord. It is similar to how you use the Discord app to access chats and stuff.
  • We supply your bot token into this client so that the client can log into your bot's account.
  • The final step would be, starting up!
myCoolBot.connect()

With this, your bot can finally connect to discord. But how will you know if your bot successfully connected?

myCoolBot.on('ready', () => {  console.log('I am alive!')})

With this, when your bot becomes "ready", you will see a "I am alive!" in your console.

import Eris from 'eris'import TOKEN from './token.js'const myCoolBot = new Eris.Client(TOKEN)myCoolBot.on('ready', () => {  console.log('I am alive!')})myCoolBot.connect()

Let's start the bot!

  • Open your command line like before, and enter the following:
node coolbot.js
  • In your case, replace coolbot.js with whatever you named your bot as.
  • Tada! Your bot is now alive!
  • You should see "I am alive!" printed in your console.

Making the bot respond

What use is a bot that does nothing? Clout?

Let's make it do something Making it respond to messages.

Before your myCoolBot.connect() line, add this.

myCoolBot.on('messageCreate', (message) => {  myCoolBot.createMessage(message.channel.id, 'I see a message')})

This makes your bot respond to every message with a I see a message. Annoying, isn't it? Let's patch this up a bit

myCoolBot.on('messageCreate', (message) => {  if (message.content.startsWith('!')) {    myCoolBot.createMessage(message.channel.id, 'I see a message')  }})

We made a small change now. The bot only responds to messages that start with a !. Let's be more precise!

myCoolBot.on('messageCreate', (message) => {  if (message.content === 'cool bot') {    myCoolBot.createMessage(message.channel.id, 'I see a message')  }})

Now your bot only responds to cool bot. More functions!

myCoolBot.on('messageCreate', (message) => {  if (message.content === 'cool bot') {    myCoolBot.createMessage(message.channel.id, 'I see a message')  } else if (message.content === 'not cool bot') {    myCoolBot.createMessage(message.channel.id, 'Thats not me!')  }})

Now your bot responds to two messages! You can do more by adding more else if lines.

myCoolBot.on('messageCreate', (message) => {  if (message.content === 'cool bot') {    myCoolBot.createMessage(message.channel.id, 'I see a message')  } else if (message.content === 'not cool bot') {    myCoolBot.createMessage(message.channel.id, 'Thats not me!')  } else if (message.content === 'is it raining?') {    myCoolBot.createMessage(message.channel.id, 'I do not know!')  }})

Complete Code

I know 90% of y'all were waiting for this.

import Eris from 'eris'import TOKEN from './token.js'const myCoolBot = new Eris.Client(TOKEN)myCoolBot.on('ready', () => {  console.log('I am alive!')})myCoolBot.on('messageCreate', (message) => {  if (message.content === 'cool bot') {    myCoolBot.createMessage(message.channel.id, 'I see a message')  } else if (message.content === 'not cool bot') {    myCoolBot.createMessage(message.channel.id, 'Thats not me!')  } else if (message.content === 'is it raining?') {    myCoolBot.createMessage(message.channel.id, 'I do not know!')  }})myCoolBot.connect()

Conclusion

You made a bot. The end. Now keep experimenting with it.


Neko Of The Abyss