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.

egghead
egghead
~ 25 minutes ago

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

  • This was great!
  • This was horrible!
  • I didn't like this because it didn't match my skill level.
  • +1 It will likely be deleted as spam.

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!

Markdown supported.
Become a member to join the discussionEnroll Today