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.

01-getting-started.md 2.5 KiB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. # Message Components
  2. Message components are a framework for adding interactive elements to a message your app or bot sends. They;re accessible, customizable, and easy to use.
  3. ## What is a Component
  4. Components are a new parameter you can use when sending messages with your bot. There are currently 2 different types of components you can use: Buttons and Select Menus.
  5. ## Creating components
  6. Lets create a simple component that has a button. First thing we need is a way to trigger the message, this can be done via commands or simply a ready event. Lets make a command that triggers our button message.
  7. ```cs
  8. [Command("spawner")]
  9. public async Task Spawn()
  10. {
  11. // Reply with some components
  12. }
  13. ```
  14. We now have our command, but we need to actually send the buttons with the command. To do that, lets look at the `ComponentBuilder` class:
  15. | Name | Description |
  16. | ---------------- | --------------------------------------------------------------------------- |
  17. | `FromMessage` | Creates a new builder from a message. |
  18. | `FromComponents` | Creates a new builder from the provided list of components. |
  19. | `WithSelectMenu` | Adds a `SelectMenuBuilder` to the `ComponentBuilder` at the specific row. |
  20. | `WithButton` | Adds a `ButtonBuilder` to the `ComponentBuilder` at the specific row. |
  21. | `Build` | Builds this builder into a `MessageComponent` used to send your components. |
  22. We see that we can use the `WithButton` function so lets do that. looking at its parameters it takes:
  23. - `label` - The display text of the button.
  24. - `customId` - The custom id of the button, this is whats sent by discord when your button is clicked.
  25. - `style` - The discord defined style of the button.
  26. - `emote` - An emote to be displayed with the button.
  27. - `url` - The url of the button if its a link button.
  28. - `disabled` - Whether or not the button is disabled.
  29. - `row` - The row the button will occupy.
  30. Since were just making a busic button, we dont have to specify anything else besides the label and custom id.
  31. ```cs
  32. var builder = new ComponentBuilder()
  33. .WithButton("label", "custom-id");
  34. ```
  35. Lets add this to our command:
  36. ```cs
  37. [Command("spawner")]
  38. public async Task Spawn()
  39. {
  40. var builder = new ComponentBuilder()
  41. .WithButton("label", "custom-id");
  42. await ReplyAsync("Here is a button!", components: builder.Build());
  43. }
  44. ```
  45. ![](images\image1.png)