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.

02-responding-to-buttons.md 959 B

4 years ago
4 years ago
4 years ago
4 years ago
12345678910111213141516171819202122232425262728293031323334353637
  1. ---
  2. uid: Guides.MessageComponents.Responding
  3. title: Responding to Components
  4. ---
  5. # Responding to button clicks
  6. Responding to buttons is pretty simple, there are a couple ways of doing it and we can cover both.
  7. ### Method 1: Hooking the InteractionCreated Event
  8. We can hook the `ButtonExecuted` event for button type interactions:
  9. ```cs
  10. client.ButtonExecuted += MyButtonHandler;
  11. ```
  12. Now, lets write our handler.
  13. ```cs
  14. public async Task MyButtonHandler(SocketMessageComponent component)
  15. {
  16. // We can now check for our custom id
  17. switch(component.Data.CustomId)
  18. {
  19. // Since we set our buttons custom id as 'custom-id', we can check for it like this:
  20. case "custom-id":
  21. // Lets respond by sending a message saying they clicked the button
  22. await component.RespondAsync($"{component.User.Mention} has clicked the button!");
  23. break;
  24. }
  25. }
  26. ```
  27. Running it and clicking the button:
  28. ![](Images/image2.png)