Skip to content

Instantly share code, notes, and snippets.

@jaxxibae
Created January 23, 2018 00:48
Show Gist options
  • Save jaxxibae/3284318167b6ebee88c5bc7040aad95f to your computer and use it in GitHub Desktop.
Save jaxxibae/3284318167b6ebee88c5bc7040aad95f to your computer and use it in GitHub Desktop.
Building your first Discord bot in discord.js

Building your first Discord bot

So, you want to write a bot. Sure, it may seem something from another world like beating the final boss on God of War 3 while playing on the hardcore difficulty, but it's actually pretty basic. Today, we're going to use node.js and discord.js to build your first ever Discord bot!

Step 1: Create an Application & a Bot account

No, you're not going to create an account with an e-mail and password. Discord API which makes the bots work uses tokens. No, not casino tokens. Tokens are often described as a long string of jibberish, and it's almost impossible to remember that code every single time.

Creating the Application

Head through the internet tubes over through the magical Discord Applications Page. Just make sure you're already logged in. If not, do it. Just do it. Then, you want to make sure that you click that not so shiny New app button.

0w0

After that, add a name to your bot, a tiny description, a profile picture, then just click on that dandy Create App button and it should be done! Ignore the redirect URIs, please.

Your bot should look something like this:

de wae

Now, it comes the most interesting part of the tutorial: Transforming your app in a bot user, so that you can use it in Discord. Scroll down the page until you see the Create a bot user button. Click it with all your might like you were clicking on that cookie. oh god, not the cookie clicker throwbacks.

Adding your bot to a server

It may seem VERY EARLY now to add your bot on a server without having any line of code, but we'll do it to speed up the process. You know what Sonic says, GOTTA GO FAST.

On the bot page, you should find a OAuth2 URL Generator. Yes, that's what Discord uses for bot adding. OAuth2. I'm serious.

.

Click the Generate OAuth2 URL button, add the scope as a bot and copy the URL. We're almost there, yey!

OAuth

Insert that URL onto a new tab, then add your handy dandy bot to a server. Just, make sure that you have the manage server permissions on the server, or it won't show up there. Logics.

Getting your bot token

Ok, so HUGE WARNING here. Don't share your token, NEVER, with anyone else. As mentioned before, the token is the way your bot is actually going to enter the Discord API tubes. And if someone has that token, they have TOTAL CONTROL of your bot. It's like telepathy, but it's worse. If you suspect that your token got leaked, then instantly RENEW IT!

Now, you shall go to the Bot section of your app, find the Token section, then click on that blue click to reveal text. It will show a large piece of random jibberish. Save that for later, when we get down to serious coding. It should look something like this:

e

Step 2: Get your coding gear ready

I'll just say this right here: It's not easy to code. And for you to code, you need some seriously powerful tools to help you on this coding journey you'll about to go into. First of all, download node.js on your favourite flavor: Windows Mac OS Linux Secondly, you can't just code with a notepad.exe. I mean, you can, but you will need more than that. Just download Atom or Visual Studio Code and you're good to go.

After you've downloaded all of the software needed, it's time to get to the real deal. But before that, create a folder to where you're going to save your bot, and keep it away from that furry pictures you have there laying. We know it's true, just admit it.

Now, let's get to the EXTREME.

Installing Discord.JS

This is starting to heat up. Oh boy.

First of all, open you CLI, wether if it's cmd.exe, Terminal or MINGW32.

Windows users: Windows button + R, type in cmd then click OK Linux users: Ctrl+Alt+T Mac OS users: Find "Terminal" on your Finder, that's what he is good at. Finding stuff.

Then, you'd want to create a new directory and automagically enter that directory.

mkdir your-first-bot && cd your-first-bot

See? Just like magic.

After that, you just need to init your project so that it's valid at the eyes of the node.js. Just type an

npm init -y

and you're good to go. Seriously.

gif

Actually, you aren't good to go because we forgot about the special ingredient in this: discord.js.

Just run

npm i -S discord.js

on your CLI, wait for it to do its magic and you're on your way to become a professional coder!

Getting your pasta ready, i mean, getting your first bot made

Coding is just like making pasta: it takes a lot of time but it's delicious when it's done. analogy 11/10

Create a file named index.js on your editor, and let's try for example this freaking delicious plate of a ping-pong command:

const Discord = require("discord.js"); //this defines the Discord API wrapper, discord.js
const client = new Discord.Client(); //this defines the client that we're going to make

client.on("ready", () => { //when the client gets ready
  console.log("I am ready!"); //send a log to the console saying it's ready
});

client.on("message", (message) => { //when the client recieves a message
  if (message.content.startsWith("ping")) { //and if the message content starts with a ping
    message.channel.send("pong!"); //send back a pong
  }
});

client.login("SuperSecretBotTokenHere"); //change the "SuperSecretBotTokenHere" with your bot token. as a string.

After that, paste the code on your index.js, make your changes, save the file as index.js, obviously, go to your CLI then execute the magical node index.js command. If everything went allright, you should see on the console the "I am ready" message (or whatever you put it in) and when you type ping on a server the bot is on, it should reply you back with a pong!

png

I've made that, now what?

There are a lot of examples and a lot of guides on how to make cool discord.js stuff. For example, the discord.js docs are quite dandy. They may seem complicated at first sight, but believe me: they aren't if you get the hang of it. Or, if you need some more serious help, there's the discord.js official™ server, for all your bot-ish needs. If you're more advanced with your bot and you want to release it to the public, you can always add it to Discord Bot List, where you can get your bot popular from 0 to 100, real quick. Oh yeah.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment