> For the complete documentation index, see [llms.txt](https://savvydev06.gitbook.io/quick-docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://savvydev06.gitbook.io/quick-docs/slash-commands/the-basics.md).

# The Basics

Clear and to-the-point tutorials with everything you'll need to integrate your bot with Discord.JS Slash commands, starting with the basics, and with many more advanced examples to use.

> This slash commands documentation is adapted from advaith1's 'Slash Commands in Discord.js' GitHub guide, but I have improved on them with better clarity and more examples.

discord.js doesn't have full support for slash commands yet ([there's a pr](https://github.com/discordjs/discord.js/pull/5448)) but you can still use the underlying api and websocket to use them.\
Note that discord.js doesn't officially support using client.api, this is basically just a workaround until they fully release support.

**Please read** [**Discord's Slash Command docs**](https://discord.com/developers/docs/interactions/slash-commands) **since they have actual docs and details for slash commands; the code examples below are just how you can implement it using discord.js.**

Note that slash commands won't show in a server unless that server has authorized it with the `applications.commands` oauth2 scope (not just the `bot` scope).

This does not require a discord.js update! It should work as long as you're using a modern version (anything v12 would probably work, obviously v12.5.1/latest is recommended)

> &#x20;Alternatively, you can manage commands and handle interactions with [slash-create](https://github.com/Snazzah/slash-create).

## Registering a command

Here's how to register a command. **You only need to register each command one time.**

It is recommended to use an [eval()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/eval) command for this, although this will work without the check.

{% hint style="success" %}
There are two ways of setting up slash commands: For testing/guild-specific (instant changes), and for deployment/global (can take up to an hour to update).
{% endhint %}

{% tabs %}
{% tab title="Guild-Specific (Testing)" %}
{% code title="bot.js" %}

```javascript
client.api.applications(client.user.id).guilds('guild-id-here').commands.post({
        data: {
            name: "help",
            description: "Get information about the bot."
            // possible options here e.g. options: [{...}]
        }
});
```

{% endcode %}
{% endtab %}

{% tab title="Global (Deployment)" %}
{% code title="bot.js" %}

```javascript
client.api.applications(client.user.id).commands.post({
        data: {
            name: "help",
            description: "Get information about the bot."
            // possible options here e.g. options: [{...}]
        }
});
```

{% endcode %}
{% endtab %}
{% endtabs %}

## Receiving & Handling the Event

Here is the bare-bones code for receiving the event, as well as an example where you can respond to the event by utilizing if statements to detect the command sent.

&#x20;Send an [Interaction Response](https://discord.com/developers/docs/interactions/slash-commands#interaction-interaction-response) object

{% hint style="info" %}
`interaction` is an [Interaction object](https://discord.com/developers/docs/interactions/slash-commands#interaction)
{% endhint %}

{% tabs %}
{% tab title="Receiving the Event (bare-bones)" %}
{% code title="bot.js" %}

```javascript
client.ws.on('INTERACTION_CREATE', async interaction => {
  // do stuff and respond here
})
```

{% endcode %}
{% endtab %}

{% tab title="Receiving and Responding to the Event" %}
{% code title="bot.js" %}

```javascript
client.ws.on('INTERACTION_CREATE', async interaction => {
        //Get our command name and convert it to lowercase
        const command = interaction.data.name.toLowerCase();
        //Get any arguments sent with our interaction
        const args = interaction.data.options;
        
        if (command === 'help') {
                // Here you could do anything: Respond, call functions, etc.
                client.api.interactions(interaction.id, interaction.token).callback.post({
                        data: {
                            type: 4,
                            data: {
                                //Replies to the message sent with the interaction
                                content: "Here to help, anytime."
                            }
                        }
                })
        }
}
```

{% endcode %}
{% endtab %}
{% endtabs %}

### Responding with Embeds

You can respond to the interaction with an embed rather than basic text. To do this, check [Embeds](/quick-docs/slash-commands/embeds.md).

## Sending a Follow-up Message

This goes inside the "Receiving the Event" block.\
see [Webhook#send docs](https://discord.js.org/#/docs/main/master/class/Webhook?scrollTo=send)

{% code title="bot.js" %}

```javascript
new Discord.WebhookClient(client.user.id, interaction.token).send('hello world')
```

{% endcode %}

##
