👨🏻‍💻 Tutorial: Creating a simple Discord Bot

As of April 2019, this tutorial is now outdated, please see my new article here for newer version


First thing first, you might have already used an app called Discord. It’s an IRC thing used mostly by gamers. You might have been able to join communities with some bots among them, I don’t want to dig in to deeper stuff on how bots are used in an IRC but just think of it as an automation helper tool…. or something like that.

If you’re like me you might have been amazed how bots were used to do cool automation stuffs (playing music, manage simple chat games, etc.) via chat commands. So I was quite curious on how did they make it. Then when I research about it, then when I found out how to create one using Node JS (I highly require you should have basic knowledge with Javascript) and it was very easy as pie! Let’s get started shall we?

PREREQUISITE(S)

  • You must have NodeJS installed. link
  • You must have a text editor installed. (Notepad, Sublime Text, VS Code, Atom, etc.)
  • A Discord account (obviously…)

GETTING STARTED

First thing first, let’s create a folder for our project (it depends on you on where to put it, it doesn’t matter) and name we’ll name it for now as“greeter-bot”.

Next, let’s create the following files package.json, bot.js, and auth.json

Now let’s put something in them shall we?

package.json

auth.json

To get your token follow this guide on how to setup a Discord app.

bot.js

INSTALL DEPENDENCIES

Okay, so now our script relies on some node libraries. so we need to them to get installed to.

Open up your command line and navigate inside the project folder, once you’re in, simply install the dependencies by executing the following command:

npm install discord.io winston --save

RUNNING THE APP

TIME TO RUN THE BOT! 
To be able to run the script you simply have to execute the following command:
node bot.js

MOMENT OF TRUTH

Now go to your chat server and make sure your bot is connected and online, then type in to the chat box !ping it should reply pong! to you.

CONGRATULATIONS YOU’VE MADE YOUR OWN DISCORD BOT!

Well it’s a start, but you never thought that it was easy huh?!

There are advance topics out there especially if you’ll deep dive in the Discord API. For now this is a good start to setup a bot.

I hope you have learned something on this tutorial, let me know if you have question(s) or issue(s) with your script.

Have fun and keep learning! 👊🏽

(10/12/2017) UPDATE: Thank you guys for supporting my tutorial and I noticed that a lot of people are having some difficulties through out lesson and it seems it’s very difficult to track your issues here, instead please head to https://github.com/renesansz/discord-greeter-bot/issues and report your issues there for easy tracking and archive purposes. Thanks again!

(06/12/2018) UPDATE: For those people who have already served their bot on the server but still getting an offline status. An additional NPM library should be installed (thanks to @axelgreavette):

npm install https://github.com/woor/discord.io/tarball/gateway_v6