In this lesson, you will learn how to build custom commands that can receive user input.
As a Discord user, you may have noticed that many servers use custom commands to provide information, moderate the chat, or enhance the overall user experience. By learning how to build these custom commands, you'll be able to create a more interactive and dynamic Discord server for your community!
We'll start by introducing the basics of slash commands and how they can be used in a Discord app. You'll then learn how to create a simple command that sends a response back to the user who triggered the command.
We'll also cover how to make your commands more dynamic by adding arguments.
By the end of the lesson, you'll understand how to receive and respond to input from users and be able to build more sophisticated bots that can interact with your users in meaningful ways.
Instructor: [0:00] Let's get started on our Book-Club Command. In commands, create a new file called bookclub.js, and I'm going to go ahead and paste in the bare bones of our slash command. We're going to set the name to be, Book Club. The description will be, creates a new book club.
[0:20] We want our users to be able to give us input, and for us to be able to do something with that input, such as the title of a book. We're going to give our users the option to input a string. We're going to add string option. This will be a function where we are going to set the name of this option to be, Book.
[0:46] Set the description to be the book, the book club is on. Then down in our Execute, we're going to create a variable called Const Book.
[1:00] Set that to interaction.options.getString. Now this getString, the value, will be what we set the name of our string option up here. It's going to be, Book. Then with that, let's go ahead and reply to that. So interaction.reply.
[1:21] The title of the book is Book. We'll save that, open up our terminal, and now that we've created a new slash command, we're going to need to re-register all of our commands. Node deploy-commands.js, and then let's start our bot and development, yarn dev.
[1:42] Head over to our Discord application, type out book, and then you can see we have our new Book-Club Command. Hit enter, select book, testing.
[1:58] Enter, the title of the book is testing. Let's add in a couple more fields. One for how many people we want, and then another one for when we want the submissions to close. We'll add integer option. This will be another function.
[2:25] Here, we're going to set the name to be, People. Set the description to be how many people you want for the club. Then, our other one will also be an integer option, add integer option, create another function, set the name to be hours, set the description to be, when in hours, you want submissions closed.
[3:12] Down in our Execute, we're going to create two more variables. "Num of people", which will be set to interaction.options.getInteger this time, and that will be people. Then, Const hours=interaction.options.getInteger, and this one will be hours.
[3:47] Last thing we'll add in is author so people know who is wanting to run the book club. We'll create a variable called author, set that to interaction.member.user.username. This is the person who launched the Command.
[4:10] In our interaction reply, I'm just going to paste in a string here. The author is looking for these many people to run a book club on the book, "Book - Submissions end in blank hours."
[4:24] Whenever we are editing our data field, we're going to need to re-register our commands. We'll stop the bot, run our node deploy-commands.js, successfully reloaded, start up our bot development again, head over to our Discord/Application/Book-club, and you can see we have three options available to us.
[4:58] Book, testing, I'm going to hit tab, select people, I want three people, select hours in three hours, hit enter. Mentor hero is looking for three people to run a book club on the book, testing submissions end in three hours.
[5:16] The last thing we want to add in is we don't want people to accidentally skip something. Discord gives us one other field we can add, which is set required true. This way, this option cannot be skipped. We'll go and add that to our other options as well.
[5:41] Re-register our Command, restart our server, restart our bot in development, head back over, type slash, and now you can see instead of it just saying three options, it gives us the three options that we need.
[6:03] If I go ahead and hit enter without filling anything in, it pops up with this option is required, specify a value, testing, and people, it's also expecting an integer. If I type out a string and hit enter, it's going to ask for a valid integer.
[6:22] Three people in three hours, hit enter. Our Command is working as expected.
Member comments are a way for members to communicate, interact, and ask questions about a lesson.
The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io
Be on-Topic
Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.
Avoid meta-discussion
Code Problems?
Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context
Details and Context
Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!