A Guide to Discord Bots
Events & The Documentation
Events
An event is a something precise that occurs (in our first example code, the event is "a message is created").
When an event is emitted, if there is a function attached to that event, this function will be executed.
In our case, discord.js has plenty of events that we can use.
The full list can be seen here, under the "Events" category.
We can see the "ready" and "message" events that we used earlier.
// "ready" event
client.on('ready', () => {
// This function is executed each time after your bot connects to Discord
// aka at each client.login();
});
// "message" event
client.on('message', message => {
// This function is executed each time your bot sees a message
// in a server OR DM!
});
//bonus: example of the "guildMemberAdd" event
client.on('guildMemberAdd', member => {
// This function is executed when a user joins a server
// that has your bot in it
// /!\ Discord.js uses the term "guild", but it just means "server"
// Remember to use "guild" instead of "server" in your code!
});
Using the documentation
You probably understood our "ping" command, but how does one know how to make such a command?
And why wouldn't you use message.reply()
instead?
Well, first things first: the documentation.
The message event has a "message" parameter, as seen here.
Click on the Message class (under "Type") and you can see all of its properties and methods.
We can see the "channel" property, which gives us an object representing the channel where the message was sent.
It returns either a TextChannel, DMChannel, or GroupDMChannel depending on where it was sent.
TextChannel: in a server
DMChannel: in a DM (direct/private message)
GroupDMChannel: in a group DM (aka DM containing more than 2 people)
It is important to note that, in the current state, sending a DM to your bot that says "ping" will make it answer with "pong".
To prevent this, we will use the "type" property of the TextChannel class.
client.on('message', message => {
// if the message wasn't sent in a server text channel
if (message.channel.type != 'text')
return; //exit the function
});
Now we only get the messages sent in a server... by everyone.
We need to exclude messages sent by other bots to avoid having two bots respond to each others (which could cause endless loops).
We need to find if a user is a bot or not.
Take a quick look at the Message class again, and you'll see an interesting property: message.author.
So now we get the user that sent the message, but how do we check if it's a bot?
Thankfully, the User class has a quick way to check that: user.bot (returns true if the user is a bot, false otherwise).
client.on('message', message => {
// if the message wasn't sent in a server text channel
// OR the author is a bot
if (message.channel.type != 'text' || message.author.bot)
return; //exit the function
});
||
simply means "or".&&
means "and", but it is not used here.
By now, you might have understood how to use the discord.js documentation.
It's not hard, but you must know how to reach what you're looking for.
You can get tons of informations with the Message class, which you'll probably use the most in the "message" event.
It is also the only parameter of this event, so always look into it first.
Remember when I talked about message.reply()
?
You can use it exactly like message.channel.send()
.
The only difference is that message.reply()
will mention the author of the message.
message.channel.send('Hello!');
//Hello!
message.reply('Hello!');
//@Maah#0040, Hello!
Final code
You should have something that looks like this:
const Discord = require('discord.js');
const client = new Discord.Client();
const token = 'your bot token here';
client.on('ready', () => {
console.log('I am ready!');
});
client.on('message', message => {
if (message.channel.type != 'text' || message.author.bot)
return;
if (message.content === 'ping') {
message.channel.send('pong');
}
});
client.login(token);