Basic Commands with Discord.js
Introduction
Today I hope to share some basic commands in the Discord.js library. Previously I had written a beginners' guide on how to create your first Discord bot using Discord.js. I gave a simple walkthrough on how to set up your coding environment, how to install the library, and how to create your new bot using the Discord developer portal. If you have not followed that guide I would recommend you do so, click this link to zoom to the past --> link.
Sending Messages
In this section, we are going to introduce a Discord.js method that has your bot send a message or many messages. This is a fundamental command for your bot; This command in its basic form will send a message to the channel that the bot was summoned in. That method isssss… — — — — — — — —->>
channel.message.send(<message content>);
Let's break this command down. The ‘channel’ at the beginning is telling the bot where to send the message. In this case, the channel means the channel the bot was called upon. So then the bot will send a message to that channel with the content it was given.
Getting User Data
In this section, we are going to look at how to get user information with your bot. In this instance, we are going to look at how to get a user's avatar. For this example, we are going to get the user that called on the bit. — — — — — →
;message.author.avatarURL();
With this command was stepping back. We are looking at the message that the bot was called upon with then, we are using the author method. The author's method gives the bot the information of the user that created that message. Next, we use the avatarURL() method, this method gets the users avatar. How could we send the user that called upon the bet their avatar — →
message.channel.send(message.author.avatarURL());
In this code snippet, we are simply sending the user their avatar. You can get many more types of user data using the author command, that is what makes it so useful.
Embeds
What is an embed? Well, an embed is a type of message in Discord. An embed looks a Lil like this — — — — — — — — — — — — — — — — — — — — — — — →
This is an embed. An embed can contain many different attributes. In this specific example, we have the embeds author name, a title, a description, an image, three text fields, and a footer. Embeds look cool, You want one well let's get you one. To initially create an embed we will need this code — — — — — →
const coolEmbed = new Discord.MessageEmbed();
In the code above we are creating a new embed (looks like I'm creating an object🧐). Yes, an embed is an object why you ask well, an embed can contain all these little attributes and these attributes are what give our embed personality. So know that you know let's dissect this code, In the code above we are creating a constant named coolEmbed; coolEmbed currently has a value of an empty embed object. Now we want to give our embed some attributes first let's give it a title so we will do this — — — — — — — — — — →
coolEmbed.setTitle('Our Super Cool Embed');
In this snippet, we are adding a title attribute to our coolEmbed object and giving it the title of “Our Super Cool Embed”. Next, let's give our embed an author — — — — — — — — — — — — — — — — — — — — — — — — — — — -→
coolEmbed.setAuthor('Cool Guy','https://encrypted-tbn0.gstatic.com/images?q=tbn%3AANd9GcT0ypFsmjSPhbso3CVB0-g7ipp86sH1kgoFpw&usqp=CAU', 'https://discord.js.org');
This author may look very intimidating but it is actually quite simple. The author attribute can take in three different arguments, they are — — — →
- The author's name
- An avatar picture for the author
- A URL for the author’s name. When you click the author’s name it will. redirect you to the link given in this argument.
Now let us give our embed an inline text field in order to do this we will give our embed object another attribute and, we do this by… — — — — — — — →
coolEmbed.addField('CoolStuff', 'We makin a bot');
This is how we add a text field. We pass the addField attribute two arguments these are… — — — — — — — — — — — — — — — — — — — — — — — — — →
- The text fields name
- The text fields content
In this case, our text field has a name of “coolstuff” and its content is “we makin a bot”. We only have two more attributes to add and that is… a footer!→
coolEmbed.setFooter('Cool bot', 'https://encrypted-tbn0.gstatic.com/images?q=tbn%3AANd9GcT0ypFsmjSPhbso3CVB0-g7ipp86sH1kgoFpw&usqp=CAU');
A footer can be given two arguments and they are… — — — — — — — — — →
- Footer text
- A little footer avatar image
Now last but not least we wanna know when the embed was sent. So, let us give it a timestamp. We do that like so — — — — — — — — — — — — — →
coolEmbed.setTimestamp();
No arguments needed😎. This attribute will give us a timestamp right above our footer. So now let us take a step back and look at our code as a whole →
const coolEmbed = new Discord.MessageEmbed();
coolEmbed.setTitle('Our Super Cool Embed');
coolEmbed.setAuthor('Cool Guy','https://encrypted-tbn0.gstatic.com/images?q=tbn%3AANd9GcT0ypFsmjSPhbso3CVB0-g7ipp86sH1kgoFpw&usqp=CAU', 'https://discord.js.org');
coolEmbed.addField('CoolStuff', 'We makin a bot');
coolEmbed.setFooter('Cool bot', 'https://encrypted-tbn0.gstatic.com/images?q=tbn%3AANd9GcT0ypFsmjSPhbso3CVB0-g7ipp86sH1kgoFpw&usqp=CAU');
coolEmbed.setTimestamp();
Now to send our embed… — — — — — — — — — — — — — — — — — — —→
channel.message.send(coolEmbed)
Voila, our masterpiece is below…
Congratulations you learned how to make your first embed! Now there are many more attributes you can add to your embed to make it cater to whatever is needed so click here to see all the embed attributes you will need to dazzle any user in your server. :)
Conclusion
Today you have learned how to send a message, how to get a user's information, and last but not least create and customize your very own embed. If there are any questions or any recommendations for what the next part of this series should be leave a comment below. Thank you for your time and Happy Coding!!