You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

03-responding-to-slash-commands.md 1.7 KiB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
12345678910111213141516171819202122232425262728293031323334353637383940
  1. ---
  2. uid: Guides.SlashCommands.Receiving
  3. title: Receiving and Responding to Slash Commands
  4. ---
  5. # Responding to interactions.
  6. Interactions are the base thing sent over by Discord. Slash commands are one of the interaction types. We can listen to the `SlashCommandExecuted` event to respond to them. Lets add this to our code:
  7. ```cs
  8. client.SlashCommandExecuted += SlashCommandHandler;
  9. ...
  10. private async Task SlashCommandHandler(SocketSlashCommand command)
  11. {
  12. }
  13. ```
  14. With every type of interaction there is a `Data` field. This is where the relevant information lives about our command that was executed. In our case, `Data` is a `SocketSlashCommandData` instance. In the data class, we can access the name of the command triggered as well as the options if there were any. For this example, we're just going to respond with the name of the command executed.
  15. ```cs
  16. private async Task SlashCommandHandler(SocketSlashCommand command)
  17. {
  18. await command.RespondAsync($"You executed {command.Data.Name}");
  19. }
  20. ```
  21. Let's try this out!
  22. ![slash command picker](images/slashcommand1.png)
  23. ![slash command result](images/slashcommand2.png)
  24. > [!NOTE]
  25. > After receiving an interaction, you must respond to acknowledge it. You can choose to respond with a message immediately using `RespondAsync()` or you can choose to send a deferred response with `DeferAsync()`.
  26. > If choosing a deferred response, the user will see a loading state for the interaction, and you'll have up to 15 minutes to edit the original deferred response using `ModifyOriginalResponseAsync()`. You can read more about response types [here](https://discord.com/developers/docs/interactions/slash-commands#interaction-response)
  27. This seems to be working! Next, we will look at parameters for slash commands.