Setup for Discord.js Slash Commands

Lucas Minter
InstructorLucas Minter
Share this video with your friends

Social Share Links

Send Tweet
Published a year ago
Updated a year ago

In this lesson, you will be setting up the necessary files for your first Discord.js slash command. The goal of this lesson is to give you a solid foundation to build upon as you create your own custom commands for Discord.

We will be importing and using fs and path to read from the command and events directories, using Collection from Discord to store and retrieve your commands, and creating the Client using the `GatewayIntentBits.

Also, we are going to deploy your commands and set up events to execute them properly once they're written.

Instructor: [0:00] This lesson is going to be a bit of a jump from our previous lesson, mainly because I want this course to be focused solely on Discord.js and not all the node that we're throwing in here.

[0:13] We've added in a couple more files and folders, a commands folder, an events folder with a couple of files. We've edited our main JS file. We've added in a deploy commands file.

[0:28] These files are given to us courtesy of the Discord.js guide. I will have that link down below. You'll be able to go there to get even more information. They go really in-depth to do a great job of describing what's going on here, but I'm just going to quickly run through it for this course.

[0:45] If you don't want to have to add all these files yourself, I have a GitHub repo template that will be linked down below that you can just clone and it'll get you right up to speed.

[0:55] We're going to import FS and PATH for reading our command and events directory, and allowing us access to those files and directories. We're going to import collection from Discord, which will be used for storing and retrieving our commands. For creating our client, we are just using the guilds, GatewayIntentBits.

[1:15] Down here, we've attached a commands property to our client to be able to access our commands and other files. This next section here is strictly just for our commands. We're using path to get access to our commands directory, and then we're using FS to filter through each file in our commands directory for every file that ends with JS.

[1:41] Then, we're throwing that through a for loop. For each file in that commands directory, we're going to path to that file, require that path. Then, if there is a data property and execute method in that command, we're going to go ahead and set that command name for each of the commands.

[2:01] Otherwise, we're going to throw this error. Down here, this is going to be for each of our events, like our ready event in the previous lesson. This is almost an exact duplicate of what we did for commands, but I'm, again, going to quickly run through it.

[2:16] We're going to get our events directory, filter through each file that ends with JavaScript for our events directory. Then, each of those files in our events directory, we're going to path to it, require that path.

[2:30] If it is an event that is ran once, we're going to take the name of that event and the rest of our arguments, call execute on our event, while passing in the spread of our arguments.

[2:44] Then, we're going to do the exact same thing for events that are constantly running, not just ran once. Then, lastly, log in as our bot. In our env file, we're going to add in a couple more environmental variables, our client ID, and our guild ID.

[3:02] Our client ID is going to be this application ID right here, offering general information. You can click copy, and paste that in. Then, we have our guild ID, which will be our server ID.

[3:13] If you have the developer mode toggled on in your Discord application, right click on your server, copy ID, and then paste that in here. Now, let's move to the deploy commands file. Again, another file given to us by the Discord.js Guide.

[3:29] We're going to import rest under routes from Discord, require and configure our .env file. We're going to use FS for reading our commands directory, create a commands array.

[3:41] We're going to grab all the command files in the commands directory, filter through that for each file that ends with JS. Then, we're going to grab the slash command builder, the JSON output of that, for each of our commands data for deployment.

[3:56] Then, we're going to construct and prepare an instance of the rest module, and then deploy our commands. In this async function, we're going to add in a try catch, quickly just log out. How many commands we're going to be refreshing?

[4:12] Then, use a put method to fully refresh all the commands in the Guild with the current set. Then, if successful, log that out. Otherwise, console.log your error. Then, over in our events folder, we're going to look at the ready.js file. Import events from Discord.js.

[4:38] We're going to set the name to be ClientReady. This is only going to be ran once. Then, we're going to execute that event by just console logging that we are logged in.

[4:51] Then, we're going to head over to InteractionCreate. This is what is going to help us create our commands. We're going to import events from Discord. The name is going to be Events.InteractionCreate.

[5:05] Then, in our execute method, if the interaction is not a chat input command, just go ahead and return. If it is, get the name of that command.

[5:17] In this if there is no command, go ahead and console.log that, and then return. Otherwise, if there was a command, go ahead and execute that command or console.log out an error. Now, in the next lesson, let's go ahead and build our first command.

egghead
egghead
~ 33 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