Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: interactions and slash commands #638

Merged
merged 105 commits into from
Jul 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
105 commits
Select commit Hold shift + click to select a range
2915b81
add interaction category to sidebar
castdrian Apr 10, 2021
5bf8fcd
add replying to slash commands page
castdrian Apr 10, 2021
b5852bd
add first four segments
castdrian Apr 10, 2021
4bc95c9
add editing response section
castdrian Apr 10, 2021
056cb70
add 2 warning blocks
castdrian Apr 10, 2021
d555a09
add deferred responses segment
castdrian Apr 10, 2021
c14431f
add ephemeral segment to deferred responses
castdrian Apr 10, 2021
5c84da1
casing
castdrian Apr 10, 2021
cb6666d
add followups section
castdrian Apr 10, 2021
89a710c
commit suggested changes
castdrian Apr 10, 2021
d4c0c34
commit suggested changes 2
castdrian Apr 10, 2021
ceddc9e
commit suggested changes 3
castdrian Apr 10, 2021
700857e
commit suggested changes 4
castdrian Apr 10, 2021
cd564d5
commit suggested changes 5
castdrian Apr 10, 2021
b3fc0fd
commit suggested changes 6
castdrian Apr 10, 2021
71d1d55
commit suggested changes 7
castdrian Apr 10, 2021
b720c73
commit suggested changes 8
castdrian Apr 10, 2021
2aa08c3
commit suggested changes 9
castdrian Apr 10, 2021
ec0f593
fix codeblock
castdrian Apr 10, 2021
9d2e11e
remove comments from all codeblocks
castdrian Apr 10, 2021
bfc63ba
indents: use spaces instead of tabs
castdrian Apr 10, 2021
b04d09f
type out numbers as words
castdrian Apr 10, 2021
8e03df0
sentence case
castdrian Apr 10, 2021
2a47862
line highlighting´and minor adjustments
castdrian Apr 10, 2021
859e7f3
use tabs as indentation, guess I should learn to read
castdrian Apr 11, 2021
4f1adb9
followups -> follow-ups
castdrian Apr 11, 2021
81d1d74
casing
castdrian Apr 11, 2021
0058ba2
casing again
castdrian Apr 11, 2021
59523d8
actually indent with tabs, blame vsc
castdrian Apr 11, 2021
3828574
remove unwanted spaces
castdrian Apr 11, 2021
7d0f402
add registering commands page
castdrian Apr 17, 2021
0b18de9
adjustment
castdrian Apr 17, 2021
62a6b68
review suggestion
castdrian Apr 27, 2021
ec41670
grammar
castdrian May 7, 2021
1f4b83a
method annotation
castdrian May 7, 2021
7d6cfd0
commanddata -> data
castdrian May 7, 2021
631cad4
trailing commas
castdrian May 7, 2021
576b25f
we will -> we'll
castdrian May 7, 2021
240273d
fix replace err
castdrian May 7, 2021
d7ac05e
commit suggestion
castdrian May 7, 2021
da8af8e
commit suggestion #2
castdrian May 7, 2021
9a66359
commit suggestion #3
castdrian May 7, 2021
8115a84
commit suggestion #4
castdrian May 7, 2021
822d6fc
commit suggestion #5
castdrian May 7, 2021
e97bc46
commit suggestion nr 6
castdrian May 7, 2021
16dc750
commit suggestion nr 7
castdrian May 7, 2021
4f0a901
commit suggestion nr 8
castdrian May 7, 2021
781a586
await all calls
castdrian May 7, 2021
555530c
forgot an await
castdrian May 7, 2021
5a6ce85
also forgot async keyword
castdrian May 7, 2021
ba89c0b
also update code highlight
castdrian May 7, 2021
7ccfc44
codeblock lint
castdrian May 7, 2021
c4d4d65
add page file
castdrian May 8, 2021
53c7713
bulk update permissions
castdrian May 8, 2021
da95040
tip that links to dapi docs
castdrian May 8, 2021
deffb73
add command handling tip
castdrian May 8, 2021
ee17413
use dummy ids for snowflakes
castdrian May 8, 2021
ea84936
add choices section
castdrian May 9, 2021
fcdecc4
Update guide/interactions/registering-slash-commands.md
castdrian May 8, 2021
7b9ba70
add missing dummy ids
castdrian May 10, 2021
eb88aa8
use the actual class name
castdrian May 10, 2021
3b26b3e
refactor events
castdrian May 10, 2021
03aed06
consistency
castdrian May 9, 2021
0f1b17c
spacing consistency
castdrian May 10, 2021
5233bd8
two additional reply sections
castdrian May 10, 2021
fd73308
fetch app if null
castdrian May 10, 2021
b205090
also fetch app if null
castdrian May 10, 2021
3e47f87
add warning about command name validation
castdrian May 10, 2021
6ace94c
that method goes there
castdrian May 11, 2021
811082f
minor adjustment
castdrian May 12, 2021
64a6163
add tip
castdrian May 13, 2021
4461a3a
follow-ups
castdrian May 15, 2021
7806e01
suggestion
castdrian May 13, 2021
f6a28ea
grammar
castdrian May 13, 2021
d4ee2b2
fix perms example
castdrian May 15, 2021
4e1c04e
mention other interaction types
castdrian May 22, 2021
64f2a71
fix typo
castdrian May 15, 2021
64477e2
commit suggested changes
castdrian May 22, 2021
0c1ba40
commit suggested changes
castdrian May 22, 2021
3b94b8b
commit suggested changes
castdrian May 22, 2021
620572d
commit suggested changes
castdrian May 22, 2021
254e1c6
adjutsments
castdrian May 28, 2021
93074b2
apply code suggestion
castdrian May 28, 2021
a368516
apply code suggestion
castdrian May 28, 2021
acfeb87
adjust to new lib changes
castdrian Jun 9, 2021
579e565
section about parsing options
castdrian Jun 21, 2021
2740fe8
more destructuring
castdrian Jun 24, 2021
addbde7
perm manager
castdrian Jul 1, 2021
5cea966
apply code suggestion
castdrian Jun 28, 2021
ee69dee
apply code suggestion
castdrian Jun 28, 2021
31502b7
apply code suggestion
castdrian Jun 28, 2021
5a56eb9
apply code suggestion
castdrian Jun 28, 2021
e8a7888
apply code suggestion
castdrian Jun 28, 2021
14945de
apply code suggestion
castdrian Jun 28, 2021
fdddcba
apply code suggestion
castdrian Jun 28, 2021
bef05c9
apply code suggestion
castdrian Jun 28, 2021
b972014
apply code suggestion
castdrian Jun 28, 2021
bfd80e9
apply code suggestion
castdrian Jun 28, 2021
9fc9602
update events
castdrian Jul 3, 2021
d0c2f4a
Apply suggestions from code review
castdrian Jul 5, 2021
dcbd7ae
sidebar order
castdrian Jul 5, 2021
60cd31a
smol fix
castdrian Jul 5, 2021
5cc5990
Apply suggestions from code review
castdrian Jul 6, 2021
2d11e8d
remove unnecessary tip
castdrian Jul 6, 2021
d30e598
Merge branch 'feat-interactions-slashcommands' of https://github.com/…
castdrian Jul 6, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 8 additions & 0 deletions guide/.vuepress/sidebar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,14 @@ export default {
'/keyv/',
],
},
{
text: 'Interactions',
children: [
'/interactions/registering-slash-commands.md',
'/interactions/replying-to-slash-commands.md',
'/interactions/slash-command-permissions.md',
],
},
{
text: 'Sharding',
children: [
Expand Down
167 changes: 167 additions & 0 deletions guide/interactions/registering-slash-commands.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
# Registering slash commands

Discord provides developers with the option to create client-integrated slash commands. In this section, we'll cover how to register these commands using discord.js!

::: tip
If you already have slash commands set-up for your application and want to learn how to respond to them, refer to [the following page](/interactions/replying-to-slash-commands/).
:::

castdrian marked this conversation as resolved.
Show resolved Hide resolved
## Global commands

First up, we'll introduce you to global application commands. These types of commands will be available in all guilds your application has the `applications.commands` scope authorized, as well as in DMs.

::: tip
Global commands are cached for one hour. New global commands will fan out slowly across all guilds and will only be guaranteed to be updated after an hour. Guild commands update instantly. As such, we recommend you use guild-based commands during development and publish them to global commands when they're ready for public use.
:::

To register a global command, pass an `ApplicationCommandData` object to the `ApplicationCommandManager#create()` method:

```js
client.on('messageCreate', async message => {
if (!client.application?.owner) await client.application?.fetch();

if (message.content.toLowerCase() === '!deploy' && message.author.id === client.application?.owner.id) {
const data = {
name: 'ping',
description: 'Replies with Pong!',
};

const command = await client.application?.commands.create(data);
console.log(command);
}
});
```
::: danger
Command names must be lowercase. You will receive an API error otherwise.
:::

That's it! You've successfully created your first global application command! Let's move on to guild commands.


## Guild commands

Guild-specific application commands are only available in the guild they were created in. You can use `GuildApplicationCommandManager#create()` to create them:

```js {10}
client.on('messageCreate', async message => {
if (!client.application?.owner) await client.application?.fetch();

if (message.content.toLowerCase() === '!deploy' && message.author.id === client.application?.owner.id) {
const data = {
name: 'ping',
description: 'Replies with Pong!',
};

const command = await client.guilds.cache.get('123456789012345678')?.commands.create(data);
console.log(command);
}
});
```



## Bulk-update commands

If, for example, you deploy your application commands when starting your application, you may want to update all commands and their changes at once. You can do this by passing an array of `ApplicationCommandData` objects to the `set()` method on either of the managers introduced above:

::: danger
This will overwrite all existing commands on the application or guild with the new data provided!
:::

```js {5-14,16-17}
client.on('messageCreate', async message => {
if (!client.application?.owner) await client.application?.fetch();

if (message.content.toLowerCase() === '!deploy' && message.author.id === client.application?.owner.id) {
const data = [
{
name: 'ping',
description: 'Replies with Pong!',
},
{
name: 'pong',
description: 'Replies with Ping!',
},
];

const commands = await client.application?.commands.set(data);
console.log(commands);
}
});
```


castdrian marked this conversation as resolved.
Show resolved Hide resolved
## Options

Application commands can have `options`. Think of these options as arguments to a function. You can specify them as shown below:

```js {4-9}
const data = {
name: 'echo',
description: 'Replies with your input!',
options: [{
name: 'input',
type: 'STRING',
description: 'The input to echo back',
required: true,
}],
};
```

Notice how `required: true` is specified within the options object. Setting this will prevent the user from sending the command without specifying a value for this option!


castdrian marked this conversation as resolved.
Show resolved Hide resolved
## Option types

As shown in the options example above, you can specify the `type` of an `ApplicationCommandOption`. Listed below are all the possible values you can pass as `ApplicationCommandOptionType`:

::: tip
Refer to the Discord API documentation for detailed explanations on the [`SUB_COMMAND` and `SUB_COMMAND_GROUP` option types](https://discord.com/developers/docs/interactions/slash-commands#subcommands-and-subcommand-groups).
:::

* `SUB_COMMAND` sets the option to be a sub-command
* `SUB_COMMAND_GROUP` sets the option to be a sub-command-group
castdrian marked this conversation as resolved.
Show resolved Hide resolved
* `STRING` sets the option to require a string value
* `INTEGER` sets the option to require an integer value
* `BOOLEAN` sets the option to require a boolean value
* `USER` sets the option to require a user or snowflake as value
* `CHANNEL` sets the option to require a channel or snowflake as value
* `ROLE` sets the option to require a role or snowflake as value
castdrian marked this conversation as resolved.
Show resolved Hide resolved
* `MENTIONABLE` sets the option to require a user, role or snowflake as value


## Choices

The `STRING` and `INTEGER` option types both can have `choices`. `choices` are a set of predetermined values users can pick from when selecting the option that contains them.

::: warning
If you specify `choices` for an option, they'll be the **only** valid values users can pick!
:::

Specify them by providing an array of `ApplicationCommandOptionChoice`'s to the option when creating a command:

```js {9-22}
const data = {
name: 'gif',
description: 'Sends a random gif!',
options: [{
name: 'category',
type: 'STRING',
description: 'The gif category',
required: true,
choices: [
{
name: 'Funny',
value: 'gif_funny',
},
{
name: 'Meme',
value: 'gif_meme',
},
{
name: 'Movie',
value: 'gif_movie',
},
],
}],
};