components.rst 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. ==================
  2. Message Components
  3. ==================
  4. Message components are new components you can add to messages, such as buttons and select menus. There are currently four different types of message components:
  5. ``ActionRow``
  6. =============
  7. Represents a row of buttons on a message. You can add up to 5 buttons to the row, which can then be added to the message. You can only add buttons to action rows.
  8. .. code:: php
  9. $row = ActionRow::new()
  10. ->addComponent(Button::new(Button::STYLE_SUCCESS));
  11. Functions
  12. ---------
  13. +----------------------------------+-------------------------------------------------------------+
  14. | name | description |
  15. +==================================+=============================================================+
  16. | ``addComponent($component)`` | adds a component to action row. must be a button component. |
  17. +----------------------------------+-------------------------------------------------------------+
  18. | ``removeComponent($component)`` | removes the given component from the action row. |
  19. +----------------------------------+-------------------------------------------------------------+
  20. | ``getComponents(): Component[]`` | returns all the action row components in an array. |
  21. +----------------------------------+-------------------------------------------------------------+
  22. ``Button``
  23. ==========
  24. Represents a button attached to a message. You cannot directly attach a button to a message, it must be contained inside an ``ActionRow``.
  25. .. code:: php
  26. $button = Button::new(Button::STYLE_SUCCESS)
  27. ->setLabel('push me!');
  28. There are 5 different button styles:
  29. ========= =========================== =======
  30. name constant colour
  31. ========= =========================== =======
  32. primary ``Button::STYLE_PRIMARY`` blurple
  33. secondary ``Button::STYLE_SECONDARY`` grey
  34. success ``Button::STYLE_SUCCESS`` green
  35. danger ``Button::STYLE_DANGER`` red
  36. link ``Button::STYLE_LINK`` grey
  37. ========= =========================== =======
  38. .. image:: https://discord.com/assets/7bb017ce52cfd6575e21c058feb3883b.png
  39. :alt: Discord button styles
  40. Discord button styles
  41. .. _functions-1:
  42. Functions
  43. ---------
  44. +--------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------+
  45. | name | description |
  46. +======================================+==========================================================================================================================================+
  47. | ``setStyle($style)`` | sets the style of the button. must be one of the above constants. |
  48. +--------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------+
  49. | ``setLabel($label)`` | sets the label of the button. maximum 80 characters. |
  50. +--------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------+
  51. | ``setEmoji($emoji)`` | sets the emoji for the button. must be an ``Emoji`` object. |
  52. +--------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------+
  53. | ``setCustomId($custom_id)`` | sets the custom ID of the button. maximum 100 characters. will be automatically generated if left null. not applicable for link buttons. |
  54. +--------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------+
  55. | ``setUrl($url)`` | sets the url of the button. only for buttons with the ``Button::STYLE_LINK`` style. |
  56. +--------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------+
  57. | ``setDisabled($disabled)`` | sets whether the button is disabled or not. |
  58. +--------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------+
  59. | ``setListener($listener, $discord)`` | sets the listener for the button. see below for more information. not applicable for link buttons. |
  60. +--------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------+
  61. | ``removeListener()`` | removes the listener from the button. |
  62. +--------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------+
  63. Adding a button listener
  64. ------------------------
  65. If you add a button you probably want to listen for when it is clicked. This is done through the ``setListener(callable $listener, Discord $discord)`` function.
  66. The ``callable $listener`` will be a function which is called with the ``Interaction`` object that triggered the button press. You must also pass the function the ``$discord`` client.
  67. .. code:: php
  68. $button->setListener(function (Interaction $interaction) {
  69. $interaction->respondWithMessage(MessageBuilder::new()
  70. ->setContent('why\'d u push me?'));
  71. }, $discord);
  72. If the interaction is not responded to after the function is called, the interaction will be automatically acknowledged with no response. If you are going to acknowledge the interaction after a delay (e.g. HTTP request, arbitrary timeout) you should return a promise from the listener to prevent the automatic acknowledgement:
  73. .. code:: php
  74. $button->setListener(function (Interaction $interaction) use ($discord) {
  75. return someFunctionWhichWillReturnAPromise()->then(function ($returnValueFromFunction) use ($interaction) {
  76. $interaction->respondWithMessage(MessageBuilder::new()
  77. ->setContent($returnValueFromFunction));
  78. });
  79. }, $discord);
  80. ``SelectMenu``
  81. ==============
  82. Select menus are a dropdown which can be attached to a message. They operate similar to buttons. They do not need to be attached to an ``ActionRow``. You may have up to 25 ``Option``\ s attached to a select menu.
  83. .. code:: php
  84. $select = SelectMenu::new()
  85. ->addOption(Option::new('me?'))
  86. ->addOption(Option::new('or me?'));
  87. .. _functions-2:
  88. Functions
  89. ---------
  90. +--------------------------------------+--------------------------------------------------------------------------------------------------------+
  91. | name | description |
  92. +======================================+========================================================================================================+
  93. | ``addOption($option)`` | adds an option to the select menu. maximum 25 options per menu. options must have unique values. |
  94. +--------------------------------------+--------------------------------------------------------------------------------------------------------+
  95. | ``removeOption($option)`` | removes an option from the select menu. |
  96. +--------------------------------------+--------------------------------------------------------------------------------------------------------+
  97. | ``setPlaceholder($placeholder)`` | sets a placeholder string to be displayed when nothing is selected. null to clear. max 150 characters. |
  98. +--------------------------------------+--------------------------------------------------------------------------------------------------------+
  99. | ``setMinValues($min_values)`` | the number of values which must be selected to submit the menu. between 0 and 25, default 1. |
  100. +--------------------------------------+--------------------------------------------------------------------------------------------------------+
  101. | ``setMaxValues($max_values)`` | the maximum number of values which can be selected. maximum 25, default 1. |
  102. +--------------------------------------+--------------------------------------------------------------------------------------------------------+
  103. | ``setDisabled($disabled)`` | sets whether the menu is disabled or not. |
  104. +--------------------------------------+--------------------------------------------------------------------------------------------------------+
  105. | ``setListener($listener, $discord)`` | sets the listener for the select menu. see below for more information. |
  106. +--------------------------------------+--------------------------------------------------------------------------------------------------------+
  107. | ``removeListener()`` | removes the listener from the select menu. |
  108. +--------------------------------------+--------------------------------------------------------------------------------------------------------+
  109. ``Option`` functions
  110. --------------------
  111. +----------------------------------+---------------------------------------------------------------------------------------------------------------------------+
  112. | name | description |
  113. +==================================+===========================================================================================================================+
  114. | ``new($label, ?$value)`` | creates a new option. requires a label to display, and optionally an internal value (leave as null to auto-generate one). |
  115. +----------------------------------+---------------------------------------------------------------------------------------------------------------------------+
  116. | ``setDescription($description)`` | sets the description of the option. null to clear. maximum 100 characters. |
  117. +----------------------------------+---------------------------------------------------------------------------------------------------------------------------+
  118. | ``setEmoji($emoji)`` | sets the emoji of the option. null to clear. must be an emoji object. |
  119. +----------------------------------+---------------------------------------------------------------------------------------------------------------------------+
  120. | ``setDefault($default)`` | sets whether the option is the default option. |
  121. +----------------------------------+---------------------------------------------------------------------------------------------------------------------------+
  122. | ``getValue()`` | gets the internal developer value of the option. |
  123. +----------------------------------+---------------------------------------------------------------------------------------------------------------------------+
  124. Adding a select menu listener
  125. -----------------------------
  126. Select menu listeners operate similar to the button listeners, so please read the above section first. The callback function will be called with the ``Interaction`` object as well as a collection of selected ``Option``\ s.
  127. .. code:: php
  128. $select->setListener(function (Interaction $interaction, Collection $options) {
  129. foreach ($options as $option) {
  130. echo $option->getValue().PHP_EOL;
  131. }
  132. $interaction->respondWithMessage(MessageBuilder::new()->setContent('thanks!'));
  133. }, $discord);
  134. ``TextInput``
  135. =============
  136. Text inputs are an interactive component that render on modals.
  137. .. code:: php
  138. $textInput = TextInput::new('Label', TextInput::TYPE_SHORT, 'custom id')
  139. ->setRequired(true);
  140. They can be used to collect short-form or long-form text:
  141. ====================== ==============================
  142. style constant
  143. ====================== ==============================
  144. Short (single line) ``TextInput::STYLE_SHORT``
  145. Paragraph (multi line) ``TextInput::STYLE_PARAGRAPH``
  146. ====================== ==============================
  147. .. _functions-3:
  148. Functions
  149. ---------
  150. +----------------------------------+-------------------------------------------------------------------------------------------------------------+
  151. | name | description |
  152. +==================================+=============================================================================================================+
  153. | ``setCustomId($custom_id)`` | sets the custom ID of the text input. maximum 100 characters. will be automatically generated if left null. |
  154. +----------------------------------+-------------------------------------------------------------------------------------------------------------+
  155. | ``setStyle($style)`` | sets the style of the text input. must be one of the above constants. |
  156. +----------------------------------+-------------------------------------------------------------------------------------------------------------+
  157. | ``setLabel($label)`` | sets the label of the button. maximum 80 characters. |
  158. +----------------------------------+-------------------------------------------------------------------------------------------------------------+
  159. | ``setMinLength($min_length)`` | the minimum length of value. between 0 and 4000, default 0. |
  160. +----------------------------------+-------------------------------------------------------------------------------------------------------------+
  161. | ``setMaxLength($max_length)`` | the maximum length of value. between 1 and 4000, default 4000. |
  162. +----------------------------------+-------------------------------------------------------------------------------------------------------------+
  163. | ``setValue($value)`` | sets a pre-filled value for the text input. maximum 4000 characters. |
  164. +----------------------------------+-------------------------------------------------------------------------------------------------------------+
  165. | ``setPlaceholder($placeholder)`` | sets a placeholder string to be displayed when text input is empty. max 100 characters. |
  166. +----------------------------------+-------------------------------------------------------------------------------------------------------------+
  167. | ``setRequired($required)`` | sets whether the text input is required or not. |
  168. +----------------------------------+-------------------------------------------------------------------------------------------------------------+