A Guide to Discord Bots
Getting Started
Installing our tools
Before we start coding in Node.js, we need to install it.
Get Node.js here, and install the recommended version.
If you already have it, make sure your version is above 8.0.0 (open a command prompt and type node --version
).
Now, we need something to write our code. Notepad++ won't be adequate.
Get either Visual Studio Code (the one I use), Atom, or Sublime Text.
Choose whichever you like the most, they're all free and equally good.
You have almost everything now, let's make a folder for your project.
I recommend having a folder for all your projects, and a sub-folder for each project inside (C:\Node_Projects\MyDiscordBot - this is just an example).
We will now use NPM, the Node Package Manager (which comes with Node.js).
This will allow us to install packages (or, in others languages, sometimes called libraries) to add more functionalities.
To use Discord with Node.js, we will use the discord.js package.
Either open the command prompt in this folder, or open VS Code and go to View - Open Pannel (CTRL + J).
Type npm init -y
and hit enter.
This will create a new file, "package.json" which you can edit at any time.
Now type npm install discord.js
.
This will install the current version of discord.js in a new folder, "node_modules".
To update the discord.js package (the latest version is installed by default), type the command again: npm install discord.js
.
Making a bot account
Go to the Discord Application Page, log in if you didn't yet, and click on "New Application".
Choose a name for your bot, and click "Create App". You can edit everything later.
Now scroll down and click "Create a Bot User".
Now scroll up again and copy your "Client ID", then head over to this page and paste it there.
You can choose which permissions your bot will have in your server.
Permissions can be edited later (by editing its role), but I suggest putting at least one NOW!
This is so Discord creates a unique role for your bot, which can't be given to anyone else.
If you didn't put any permission, the role might not be created.
Don't edit anything else and click on the generated link.
Select your server, invite your bot in it and... done!
Note: I recommend you first make an empty server only for testing your bot.
Getting your bot's token
Your bot is offline, let's change that!
You can close the page that allowed you to invite your bot, then go back to your bot's application page.
Scroll down to the "App Bot User" and click on "click to reveal" to get your bot's token.
DO NOT EVER SHARE THIS TOKEN WITH ANYONE, KEEP IT SECRET
Why? Because anyone that has it can take control of your bot, even if they are not in your server.
User accounts have tokens too, and same thing: keep it secret, or anyone could access your account.
If you think someone might have gotten it, you can generate a new one on your bot's application page. For user accounts, just change your password and your token will change.
Bringing your bot to life
Create a new file, and name it index.js
(.js = JavaScript file).
It will contain your bot's code, so put it in your project's folder.
Now paste the following code in it, and take time to read it.
Note: Useful shortcut for VS Code: CTRL + B.
/*
A ping pong bot, whenever you send "ping", it replies "pong".
*/
// Import the discord.js module
const Discord = require('discord.js');
// Create an instance of a Discord client
// This is sometimes called 'bot', but 'client' is prefered
const client = new Discord.Client();
// The token of your bot - https://discordapp.com/developers/applications/me
const token = 'your bot token here';
// The ready event is vital, it means that your bot will only start reacting to information
// from Discord after ready is emitted
client.on('ready', () => {
console.log('I am ready!');
});
// Create an event listener for messages
client.on('message', message => {
// If the message is "ping"
if (message.content === 'ping') {
// Send "pong" in the same channel
message.channel.send('pong');
}
});
// Log our bot in
client.login(token);
Yes, replace your bot token here
with your bot's token (keeping the apostrophes).
Anything after //
or between /*
and */
is a comment: your bot will ignore it.
You can remove all of the comments if you understand what all of this do.
Tip: Always comment your code, because you might forget what some parts do (this will be useful for when you actually make your own code).
You can close the application page, and type in the terminal node .
.
If you didn't name your file index.js
, you can change it in package.json or type node myfile.js
.
Everything is installed and your bot is running...
Try typing ping
on your Discord server, and your bot should answer with pong
.
Awesome! You made your first bot!
Use CTRL + C in the terminal to disconnect it.
You can take a break now, we will talk about events (client.on('')...
) and how to use the documentation in the next part.
Useful links
Last but not least, some very useful links:
Discord.js documentation: consider bookmarking it.
Discord.js server
Node.js documentation