This short tutorial will help you get started with discord.js - the node-based javascript library for building Discord bots.
It is expected that you have a basic understanding of programming, JavaScript, nodejs, npm and working with CLI. Otherwise, check these wonderful resoruces and then come back to this tutorial:
- Computer programming
- The JavaScript language
- Command-line Interface
- What is npm?
- Node.js / Intorduction to Node.js
Make sure you have npm
installed as it will be used to install and manage node packages/libraries including discord.js
.
-
Start by making a new folder, let's call it
my-discordjs-bot
and then enter it -
Open a console/terminal window in your folder and run the
npm init
command. The console will start asking you questions, you can simply skip all by clicking enter. Final result should look similar to this: npm init gifYou will notice that
npm init
created a file in your folder calledpackage.json
, this file holds info about your project and used packages. -
Now run
npm install discord.js --save
in your console to install the latest stable version of discord.js, demo: npm install discord.js
- Create a new file in root folder, called
bot.js
- Add the following code
/**
* use the Discord.js library which we installed with npm
*/
const Discord = require('discord.js');
/**
* initialize client
*/
const client = new Discord.Client();
Now we have access to the client
object which we can use to listen to events and run commands.
All bots on Discord have a unique token for authentication, to get a token you need to register a new bot on the official Discord Developers website.
- Head over to https://discordapp.com/developers/applications/me and login with your Discord account.
- Click on
New App
- Enter a name and avatar (optional) for your bot then click on
Create App
- On the bot page, scroll down to the
Bot
section and click onCreate a Bot user
- Your token should be available in the
Bot
section
Return to your bot.js
file and add the following code:
/**
* Use `client.on` to handle discord events
*/
client.on('ready', () => {
console.log('I am ready!');
});
/**
* add your token here to connect bot
* keep this line at end of file to make sure your bot has read
* the entire script before going online
*/
client.login('your token');
You can now run the bot to see it online. All you have to do is invite your bot to a discord server. It is recommended that you make a new Discord server to test your creations.
- To general an invitation link, go to your
Bot App
page on Discord Developers site - Scroll down and click on the
Generate OAuth2 URL
- Make sure the scope is
bot
- Select all the neccessary permissions that you think your bot will require - you can leave it blank for now
- Click on
copy
and open the URL in your browser. - Select your server from the drop menu then hit
Authorize
At this point your will see your bot on your server, but it will be offline. To log it in, go back to your console and run:
node bot.js
If everything went as expected, you should see a message in your console I'm ready
and your bot will be online. Congratulations!
Commands are essentially messages, that means we can listen to them through the message
event listener that is available in our client
object:
/**
* listen for new messages
*/
client.on('message', message => {
/**
* basic command condition
*/
if (message.content === 'say hi') {
message.reply('hello to you!');
}
});
The above code instructs bot client to listen for all new messages, and then check if the message content is say hi
(the condition) and then replies with hello to you
if the condition was met. This is the basis for creating various commands.
It is recommended that your commands start with a unique prefix so that your bot does not process something it shouldn't have.
A basic prefix implementation would look something like this:
const myPrefix = '!mybot';
client.on('message', message => {
// message: !mybot ping
if (message.content.startsWith(myPrefix + ' ping')) {
return message.reply('pong');
}
// message: !mybot
if (message.content.startsWith(myPrefix)) {
return message.reply('I have your attention');
}
});
You almost never need to process message by other bots, so it is a good practice to ignore messages by bots to prevent any potential chaos and infinite loops..
if (message.author.bot) {
return;
}
After you're done, save the bot.js
then run it again in the terminal with node bot.js
(if your bot was already running, use ctrl+c to stop the process or simply close terminal window)
The final bot.js
code should be similar to the following:
/**
* use the Discord.js library which we installed with npm
*/
const Discord = require('discord.js');
/**
* initialize client
*/
const client = new Discord.Client();
/**
* Use `client.on` to handle discord events
*/
client.on('ready', () => {
console.log('I am ready!');
});
/**
* listen for new messages
*/
const myPrefix = '!mybot';
client.on('message', message => {
// ignore other bots
if (message.author.bot) {
return;
}
// message: !mybot ping
if (message.content.startsWith(myPrefix + ' ping')) {
return message.reply('pong');
}
// message: !mybot
if (message.content.startsWith(myPrefix)) {
return message.reply('I have your attention');
}
});
/**
* add your token here to connect bot
* keep this line at end of file to make sure your bot has read
* the entire script before going online
*/
client.login('your token');
This has been a basic tutorial on starting your own Discord bot with discord.js
Contact me for feedback or additional help
Thanks Bro! helped me a lot