How To Read Discord.js Documentation (v12)
How To Read Discord.js Documentation (v12)
Discord.js Documentation: https://discord.js.org/#/docs/main/stable/general/welcome
If you're a new Discord.js developer, looking at the documentation can for sure be scary. The goal of this guide will help you properly use the documentation and will make the tutorials more clear.
When first looking at the welcome page, it is a good idea to go through the examples given on the left hand side. When you click on a class, you are normally given a list of properties and methods and a breif description at the top explaining what the class is. Understanding those statements are very important to using the library properly.
Properties give you information of the class you are using, these usually just end with the word given. Methods are used to update the property or class that it is under, these usually are followed by () with data that you provide.
Property Example: client.user.tag
The client represents your new Discord.Client() and the class can be found here. Find user, click on the Type and find tag. From there you should understand how you got that data and what type of data it is (string, array etc.).
Method Example: client.user.setStatus()
Same as above with the client and user, but instead we are using the setStatus method. Following the examples shown can help you understand how the method works. Notice the paramaters, their type and if they're optional. PresenceStatusData is required and you must provide one of those options.
When Classes Can Be Used
Classes can only be used when they are defined using another method or class. For example: you cannot use the Guild class if you do not have a way of actually getting a guild. Another example is that you cannot use the Role class if no role is fetched or defined.
A role can be accessed from the Guild class and a guild can be accessed from the Message class.
Types
The entire Discord.js library is connected to itself in numerous ways. Types can help you easily get to what you want.
When looking at a property, there is always a type that comes with it that tells you what it represents or what kind of data it will give you. Clicking on those types will give you more information and will let you know how to use it or what specifically is the data you're being given. Because of the length of different types of properties the library offers, it is best to look at the type yourself.
Some types will be number, boolean, string, snowflake or array. Those links will direct you to the Mozilla Developer page which will explain to you the exact type of data you will be given. It is very important to know these terms because you will be able to use them the future for other class methods.
Some other type will include but are not limited to Guild, GuildChannel, Role, GuildEmoji, User, GuildMember. Every type that you find in a property, you should look at and learn how to use it and what it represents.
Client Class
You probably already know what this is, but do you know why you're using it and how you're using it?
// the variable you will use
const client = new Discord.Client(options) // the constructor
The client represents your bot. You only need to use this line once in the main file of your code. There are functions that can allow you to use client throughout your code.
The options are optional settings you can put on your client. Many methods in the Discord.js library have options, usually in the Object syntax. Click on the Type for the options to see what's actually available. The ClientOptions are in the form of an Object as stated at the top of the page. Each option also has its own type which is very important to know.
Options Example:
let options = { fetchAllMembers: true }
const client = new Discord.Client(options)
Going back to the Client class, there are 3 columns that list different ways to use the class. Properties, methods and events.
The properties give you information of the Client. If you click on any of the properties, you will be brought to specific details of the property. Look at the type that the property represents, it is very important to know what data you're getting.
#1 Example: bot.user represents Clientuser. From there, you can use any of those properties or methods from the Clientuser class.
#2 Example: bot.guilds represents the GuildManager class, not guilds themselves. From here you can use .cache to access all of your guilds. The type of data of your guilds will be in is a Collection. It is very important to know how to use collections since the Discord API already gives you a number of those.
Other Information
There isn't really a need to define any other classes like your client. This includes getting a guild, message, role or a member. Most information can be accessed simply from message because you can get the guild directly from it. Check the Message class and look at properties to see all the data you can get.
If you still have trouble understanding the documentation, please contact Fyrlex#2740 on Discord to see if more needs to be added to this article.
Updated on: 14/11/2020
Thank you!