Discord.js is a super snazzy library that lets is make extremely powerful Discord bots!
In order to use discord.js, we need to have NodeJS and NPM installed. Head here to download NodeJS (and NPM, which is packaged in to the download)!
If you are not on Mac or Windows, check out here for more installation instructions.
Be sure to have NodeJS installed!
You can check to make sure everything is installed properly by opening a terminal and running node -v && npm -v which should show two version numbers! As long as you get 2 lines of numbers, you'll be good for this lesson! If you're having problems, just Google installing node [PLATFORM], like "installing node windows" and they should help you out. Google and StackOverflow is your friend!
Lets get started by going in to a directory that we want to do create our project. Open a terminal where you want your project to be.
mkdir my-discord-bot
cd my-discord-botNext, we're going to run npm init -y which will create some files for us! (the -y argument keeps everything at defaults. Remove -y if you want to specify project info, which tbh isn't important for this tutorial).
Now that we've set up our project, we can start installing stuff!
NPM is a package repository that allows us to install a variety of open source modules for our projects. We're going to start by installing Discord.js, which we can do by running
npm install discord.js --save(The --save flag adds this module to our package.json, which has a definition of the modules we install. This is very useful for giving your project to your friend or just remembering what you installed)
In order to use your discord bot, it needs to be added to a discord server.
You need administrative privileges to add a discord bot to a server.
If you do not already have the admin role in a server, below are the instructions to create a discord server.
Instructions to Create a Discord Server
Now that you are able to make a bot, you need to make a place for it to live. To get started, open up Discord and follow these steps:
- Select the plus button at the bottom of your server list.
- Select the "Create My Own" button.
- Name the server whatever you want, and then click "Create"
You should now see a blank server in your server list!
In order to make a discord bot, you need to create an application on the discord developer portal.
Head here and click on "New Application".
Once you've done that, head to the "Bot" tab and click "Add Bot", then click "Yes, do it!"
Now you have a bot! You should see a "Copy" button somewhere in the center of the screen. Don't click it yet, but remember where it is because we'll need to access it later.
Now that we've registered our discord bot, its time to build it!
To start, we need a .js file so we can write some code. Create a file called index.js in your project folder. (It can be whatever name you want, but 'index' is recommended because its the root of your project)
# Windows:
echo > index.js
# Max & Linux:
touch index.jsNow we need to require discord.js so we can use it to build our bot.
// index.js
let Discord = require('discord.js');
let client = new Discord.Client();
client.on('ready', () => {
console.log('Logged in as ' + client.user.tag);
});But hold on... how do we get our bot to login? Well, remember when I said to remember where that "Copy" button was? Lets go back there.
In order for our bot to login, we need to use it's secret token so it can authenticate itself. Go ahead and click the copy button.
Now that you have your bot's token on your keyboard, we can put that in our program so it can login!
// index.js
client.login('PASTE_YOUR_TOKEN_HERE');
// BE CAREFUL! If someone gets this token then they can login as your bot!
// Keep it secret! Keep it safe!Eyy, out bot can login now! But we still need to add it to a server in order to properly use it.
Go back to your discord developer portal, click on your bot, and go to the OAuth tab.
Scroll down to "OAuth2 URL Generator" and check the bot checkbox. After that, scroll down and check Administrator.
Once you've done that, copy the link it generated for you and open it up in a new tab.
From there, select the server dropdown and pick the discord server you want to add it to, then click Continue, then Authorize.
Now that your bot is in a server, we can make sure it works by starting our program up.
node index.jsYou should see "Logged in as BOT_NAME#0000" and your bot should be set to "Online" on your server's member list!
// index.js
client.on('message', msg => {
if (msg.content == 'ping') {
msg.reply('Pong!');
}
})Stop your bot with Ctrl+C, then start it up again with node index.js!
Now type ping in one of the text channels on your server.
There's so much other things you can do with Discord bots! Check out the Discord.js Documentation for detailed specifications on everything you can do!
// index.js
client.on('ready', () => {
// This code needs to run after our bot is officially online and connected to discord
client.channels.cache.find(channel => channel.name == 'lesson_chat').send('hi there!')
})Here's a pretty simple command system for your bot. Feel free to copy paste!
// index.js
// Command System
let commands = {
};
let prefix = '/';
client.on('message', msg => {
if (msg.content.substr(0, prefix.length) == prefix) {
let cmd = msg.content.substr(prefix.length).split(' ')[0]; // get first word in our command
if (cmd == 'help') { // Show our commands.
msg.reply(
'Commands:\r\n' + Object.entries(commands)
.filter(([key, value]) => typeof value != 'string') // Filter out Alias's
.map(([key, value]) => `${prefix}${value.syntax || key}: ${value.info}`) // Convert our commands to string format
.join('\r\n') // Join by newline
);
} else {
if (cmd in commands) {
let command = commands[cmd];
if (typeof command == 'string') { // Alias support
commands[command].run(msg);
} else {
command.run(msg);
}
} else {
msg.reply("Sorry, I don't know what command you're referring to! Say `" + prefix + "help` for more info.")
}
}
}
})// index.js
let commands = {
// ...
'8ball': {
info: "Ask the Magic 8-ball for a response.", // For help command
run(msg) {
let responses = [
'Yes',
'No',
'Maybe',
'Perhaps',
"That's up to you",
"Ask again later",
"For Certain",
"Unlikely",
// add more if you want
];
let chosen = responses[Math.floor(Math.random() * responses.length)];
msg.reply(chosen);
},
}
'8-ball': '8ball', // Alias
// ...
}// index.js
let commands = {
// ...
'random': {
syntax: 'random [number]',
info: 'Generate a random number between 1 and 10, or 1 and a chosen integer.',
run(msg) {
let min = 1;
let max = 10;
let args = msg.content.trim().split(' ');
args.shift(); // pop off first argument
if (args.length >= 1) {
max = Number(args[0]) || max;
}
msg.reply(Math.floor((Math.random() * max) + min));
}
}
// ...
}Be sure to check out the Discord.js Documentation! They have everything you can do with your discord bot all nicely documented. Need to create a text channel? Kick a user? Anything? Their docs show you how you would do it!
Discord.js Documentation is your google for JavaScript discord bots.
There's also Discord's own documentation which should answer any questions you have!




