the previous page.
Pong!
Discord allows developers to register slash commands, which provide users a first-class way of interacting directly with your application. Before being able to reply to a command, you must first register it.
This section will cover only the bare minimum to get you started, but you can refer to our in-depth page on registering slash commands for further details. It covers guild commands, global commands, options, option types, and choices.
Create a deploy-commands.js
file in your project directory. This file will be used to register and update the slash commands for your bot application.
Since commands only need to be registered once, and updated when the definition (description, options etc) is changed, it’s not necessary to connect a whole client to the gateway or do this on every ready
event. As such, a standalone script using the lighter REST manager is preferred.
Below is a deployment script you can use. Focus on these variables:
clientId
: Your application’s client idguildId
: Your development server’s idcommands
: An array of commands to register. The slash command builder from discord.js
is used to build the data for your commandsIn order to get your application’s client id, go to Discord Developer Portal and choose your application. Find the id under “Application ID” in General Information subpage. To get guild id, open Discord and go to your settings. On the “Advanced” page, turn on “Developer Mode”. This will enable a “Copy ID” button in the context menu when you right-click on a server icon, a user’s profile, etc.
Once you fill in these values, run node deploy-commands.js
in your project directory to register your commands to a single guild. It’s also possible to register commands globally.
You only need to run node deploy-commands.js
once. You should only run it again if you add or edit existing commands.
Once you’ve registered your commands, you can listen for interactions via in your index.js
file.
You should first check if an interaction is a chat input command via , and then check the property to know which command it is. You can respond to interactions with .
_16client.once('ready', () => {_16console.log('Ready!');_16});_16_16client.on('interactionCreate', async (interaction) => {_16if (!interaction.isChatInputCommand()) return;_16const { commandName } = interaction;_16if (commandName === 'ping') {_16await interaction.reply('Pong!');_16} else if (commandName === 'server') {_16await interaction.reply('Server info.');_16} else if (commandName === 'user') {_16await interaction.reply('User info.');_16}_16});_16client.login(token);
_16client.once('ready', () => {_16console.log('Ready!');_16});_16_16client.on('interactionCreate', async (interaction) => {_16if (!interaction.isChatInputCommand()) return;_16const { commandName } = interaction;_16if (commandName === 'ping') {_16await interaction.reply('Pong!');_16} else if (commandName === 'server') {_16await interaction.reply('Server info.');_16} else if (commandName === 'user') {_16await interaction.reply('User info.');_16}_16});_16client.login(token);
Note that servers are referred to as “guilds” in the Discord API and discord.js library. interaction.guild
refers to the guild the interaction was sent in (a instance), which exposes properties such as .name
or .memberCount
.
_11client.on('interactionCreate', async (interaction) => {_11if (!interaction.isChatInputCommand()) return;_11const { commandName } = interaction;_11if (commandName === 'ping') {_11await interaction.reply('Pong!');_11} else if (commandName === 'server') {_11await interaction.reply(`Server name: ${interaction.guild.name}\nTotal members: ${interaction.guild.memberCount}`);_11} else if (commandName === 'user') {_11await interaction.reply('User info.');_11}_11});
_11client.on('interactionCreate', async (interaction) => {_11if (!interaction.isChatInputCommand()) return;_11const { commandName } = interaction;_11if (commandName === 'ping') {_11await interaction.reply('Pong!');_11} else if (commandName === 'server') {_11await interaction.reply(`Server name: ${interaction.guild.name}\nTotal members: ${interaction.guild.memberCount}`);_11} else if (commandName === 'user') {_11await interaction.reply('User info.');_11}_11});
Server name: discord.js Guide
Total members: 2
You could also display the date the server was created, or the server’s verification level. You would do those in the same manner – use interaction.guild.createdAt
or interaction.guild.verificationLevel
, respectively.
Refer to the documentation for a list of all the available properties and methods!
A “user” refers to a Discord user. interaction.user
refers to the user the interaction was sent by (a instance), which exposes properties such as .tag
or .id
.
_11client.on('interactionCreate', async (interaction) => {_11if (!interaction.isChatInputCommand()) return;_11const { commandName } = interaction;_11if (commandName === 'ping') {_11await interaction.reply('Pong!');_11} else if (commandName === 'server') {_11await interaction.reply(`Server name: ${interaction.guild.name}\nTotal members: ${interaction.guild.memberCount}`);_11} else if (commandName === 'user') {_11await interaction.reply(`Your tag: ${interaction.user.tag}\nYour id: ${interaction.user.id}`);_11}_11});
_11client.on('interactionCreate', async (interaction) => {_11if (!interaction.isChatInputCommand()) return;_11const { commandName } = interaction;_11if (commandName === 'ping') {_11await interaction.reply('Pong!');_11} else if (commandName === 'server') {_11await interaction.reply(`Server name: ${interaction.guild.name}\nTotal members: ${interaction.guild.memberCount}`);_11} else if (commandName === 'user') {_11await interaction.reply(`Your tag: ${interaction.user.tag}\nYour id: ${interaction.user.id}`);_11}_11});
Your tag: User#0001
Your id: 123456789012345678
Refer to the documentation for a list of all the available properties and methods!
And there you have it!
if
/else if
If you don’t plan on making more than a couple commands, then using an if
/else if
chain is fine; however, this isn’t always the case. Using a giant if
/else if
chain will only hinder your development process in the long run.
Here’s a small list of reasons why you shouldn’t do so:
Next, we’ll be diving into something called a “command handler” – code that makes handling commands easier and much more efficient. This allows you to move your commands into individual files.