Create Your First Discord.js Slash Command

Lucas Minter
InstructorLucas Minter
Share this video with your friends

Social Share Links

Send Tweet
Published 2 years ago
Updated 2 years ago

Now that our files are set up, we can now write our first Discord.js slash command.

By create custom commands, we extend the functionality of our Discord bot and can make it much more fun and interesting.

We'll start by creating a new file in the commands folder, importing the slash command builder, and defining the data and execute sections of the command. We'll then register the command and test it in the Discord application.

By the end of this lesson, you'll have a solid understanding of how to create custom slash commands in Discord.js. So get ready to dive in and start building!

[0:00] Instructor: To create our first slash command, go up into the commands folder. Create a new file called ping.js, and we're going to import slash command builder. Export that. Then our slash command needs a minimum of two things, data and execute.

[0:23] Inside of data, we're going to create a new slash command builder, and our data needs a minimum of two things. A name, which we will name it "ping," and a description, which we'll just say, replies with "pong."

[0:48] Add our comma. Then, inside of our execute, we're going to pass an interaction and then we're going to reply to that interaction with pong.

[1:05] Go and save that. Open up our terminal. Now, we need to register that command. We're going to run node deploy commands.js, hit enter, and we should see successfully reloaded one application/commands. Start our bot in development, yarn dev.

[1:27] Head over to your Discord application, type /ping. Here is a really interesting thing about slash commands. You can have multiple bots with the same name for a command.

[1:45] On the right here, you can see which bot will run the command. We want to choose the second one since that's the bot that we're running through in this course. Click on that and hit enter. Replies with pong.