Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: izexi <43889168+izexi@users.noreply.github.com> Co-authored-by: Sugden <28943913+NotSugden@users.noreply.github.com> Co-authored-by: Advaith <advaithj1@gmail.com> Co-authored-by: Shiaupiau <stu43005@gmail.com> Co-authored-by: monbrey <rsm999@uowmail.edu.au> Co-authored-by: Tiemen <ThaTiemsz@users.noreply.github.com> Co-authored-by: Carter <carter@elhnet.net>
- Loading branch information
1 parent
af00ec8
commit f7643f7
Showing
24 changed files
with
1,340 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
# Slash Commands | ||
|
||
In this example, you'll get to know how to create commands and listen to incoming interactions. | ||
|
||
## Creating a Command | ||
|
||
First off, we need to create a command so that users can use it. We will create an `echo` command which simply returns what the user inputted. Note that global commands can take up to an hour to appear in the client, so if you want to test a new command, you should create it for one guild first. | ||
|
||
```js | ||
// The data for our command | ||
const commandData = { | ||
name: 'echo', | ||
description: 'Replies with your input!', | ||
options: [{ | ||
name: 'input', | ||
type: 'STRING', | ||
description: 'The input which should be echoed back', | ||
required: true, | ||
}], | ||
}; | ||
|
||
client.once('ready', () => { | ||
// Creating a global command | ||
client.application.commands.create(commandData); | ||
|
||
// Creating a guild-specific command | ||
client.guilds.cache.get('id').commands.create(commandData); | ||
}); | ||
``` | ||
|
||
And that's it! As soon as your client is ready, it will register the `echo` command. | ||
|
||
## Handling Commands | ||
|
||
Now let's implement a simple handler for it: | ||
|
||
```js | ||
client.on('interaction', interaction => { | ||
// If the interaction isn't a slash command, return | ||
if (!interaction.isCommand()) return; | ||
|
||
// Check if it is the correct command | ||
if (interaction.commandName === 'echo') { | ||
// Get the input of the user | ||
const input = interaction.options[0].value; | ||
// Reply to the command | ||
interaction.reply(input); | ||
} | ||
}); | ||
``` | ||
|
||
The `interaction` event will get emitted every time the client receives an interaction. Only our own slash commands trigger this event, so there is no need to implement a check for commands that belong to other bots. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,3 +28,5 @@ | |
path: moderation.md | ||
- name: Webhook | ||
path: webhook.js | ||
- name: Slash Commands | ||
path: commands.md |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
'use strict'; | ||
|
||
const { Events, InteractionTypes } = require('../../../util/Constants'); | ||
let Structures; | ||
|
||
module.exports = (client, { d: data }) => { | ||
if (data.type === InteractionTypes.APPLICATION_COMMAND) { | ||
if (!Structures) Structures = require('../../../util/Structures'); | ||
const CommandInteraction = Structures.get('CommandInteraction'); | ||
|
||
const interaction = new CommandInteraction(client, data); | ||
|
||
/** | ||
* Emitted when an interaction is created. | ||
* @event Client#interaction | ||
* @param {Interaction} interaction The interaction which was created | ||
*/ | ||
client.emit(Events.INTERACTION_CREATE, interaction); | ||
return; | ||
} | ||
|
||
client.emit(Events.DEBUG, `[INTERACTION] Received interaction with unknown type: ${data.type}`); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.