components.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. package discordgo
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. )
  6. // ComponentType is type of component.
  7. type ComponentType uint
  8. // MessageComponent types.
  9. const (
  10. ActionsRowComponent ComponentType = 1
  11. ButtonComponent ComponentType = 2
  12. SelectMenuComponent ComponentType = 3
  13. TextInputComponent ComponentType = 4
  14. )
  15. // MessageComponent is a base interface for all message components.
  16. type MessageComponent interface {
  17. json.Marshaler
  18. Type() ComponentType
  19. }
  20. type unmarshalableMessageComponent struct {
  21. MessageComponent
  22. }
  23. // UnmarshalJSON is a helper function to unmarshal MessageComponent object.
  24. func (umc *unmarshalableMessageComponent) UnmarshalJSON(src []byte) error {
  25. var v struct {
  26. Type ComponentType `json:"type"`
  27. }
  28. err := json.Unmarshal(src, &v)
  29. if err != nil {
  30. return err
  31. }
  32. switch v.Type {
  33. case ActionsRowComponent:
  34. umc.MessageComponent = &ActionsRow{}
  35. case ButtonComponent:
  36. umc.MessageComponent = &Button{}
  37. case SelectMenuComponent:
  38. umc.MessageComponent = &SelectMenu{}
  39. case TextInputComponent:
  40. umc.MessageComponent = &TextInput{}
  41. default:
  42. return fmt.Errorf("unknown component type: %d", v.Type)
  43. }
  44. return json.Unmarshal(src, umc.MessageComponent)
  45. }
  46. // MessageComponentFromJSON is a helper function for unmarshaling message components
  47. func MessageComponentFromJSON(b []byte) (MessageComponent, error) {
  48. var u unmarshalableMessageComponent
  49. err := u.UnmarshalJSON(b)
  50. if err != nil {
  51. return nil, fmt.Errorf("failed to unmarshal into MessageComponent: %w", err)
  52. }
  53. return u.MessageComponent, nil
  54. }
  55. // ActionsRow is a container for components within one row.
  56. type ActionsRow struct {
  57. Components []MessageComponent `json:"components"`
  58. }
  59. // MarshalJSON is a method for marshaling ActionsRow to a JSON object.
  60. func (r ActionsRow) MarshalJSON() ([]byte, error) {
  61. type actionsRow ActionsRow
  62. return json.Marshal(struct {
  63. actionsRow
  64. Type ComponentType `json:"type"`
  65. }{
  66. actionsRow: actionsRow(r),
  67. Type: r.Type(),
  68. })
  69. }
  70. // UnmarshalJSON is a helper function to unmarshal Actions Row.
  71. func (r *ActionsRow) UnmarshalJSON(data []byte) error {
  72. var v struct {
  73. RawComponents []unmarshalableMessageComponent `json:"components"`
  74. }
  75. err := json.Unmarshal(data, &v)
  76. if err != nil {
  77. return err
  78. }
  79. r.Components = make([]MessageComponent, len(v.RawComponents))
  80. for i, v := range v.RawComponents {
  81. r.Components[i] = v.MessageComponent
  82. }
  83. return err
  84. }
  85. // Type is a method to get the type of a component.
  86. func (r ActionsRow) Type() ComponentType {
  87. return ActionsRowComponent
  88. }
  89. // ButtonStyle is style of button.
  90. type ButtonStyle uint
  91. // Button styles.
  92. const (
  93. // PrimaryButton is a button with blurple color.
  94. PrimaryButton ButtonStyle = 1
  95. // SecondaryButton is a button with grey color.
  96. SecondaryButton ButtonStyle = 2
  97. // SuccessButton is a button with green color.
  98. SuccessButton ButtonStyle = 3
  99. // DangerButton is a button with red color.
  100. DangerButton ButtonStyle = 4
  101. // LinkButton is a special type of button which navigates to a URL. Has grey color.
  102. LinkButton ButtonStyle = 5
  103. )
  104. // ComponentEmoji represents button emoji, if it does have one.
  105. type ComponentEmoji struct {
  106. Name string `json:"name,omitempty"`
  107. ID string `json:"id,omitempty"`
  108. Animated bool `json:"animated,omitempty"`
  109. }
  110. // Button represents button component.
  111. type Button struct {
  112. Label string `json:"label"`
  113. Style ButtonStyle `json:"style"`
  114. Disabled bool `json:"disabled"`
  115. Emoji ComponentEmoji `json:"emoji"`
  116. // NOTE: Only button with LinkButton style can have link. Also, URL is mutually exclusive with CustomID.
  117. URL string `json:"url,omitempty"`
  118. CustomID string `json:"custom_id,omitempty"`
  119. }
  120. // MarshalJSON is a method for marshaling Button to a JSON object.
  121. func (b Button) MarshalJSON() ([]byte, error) {
  122. type button Button
  123. if b.Style == 0 {
  124. b.Style = PrimaryButton
  125. }
  126. return json.Marshal(struct {
  127. button
  128. Type ComponentType `json:"type"`
  129. }{
  130. button: button(b),
  131. Type: b.Type(),
  132. })
  133. }
  134. // Type is a method to get the type of a component.
  135. func (Button) Type() ComponentType {
  136. return ButtonComponent
  137. }
  138. // SelectMenuOption represents an option for a select menu.
  139. type SelectMenuOption struct {
  140. Label string `json:"label,omitempty"`
  141. Value string `json:"value"`
  142. Description string `json:"description"`
  143. Emoji ComponentEmoji `json:"emoji"`
  144. // Determines whenever option is selected by default or not.
  145. Default bool `json:"default"`
  146. }
  147. // SelectMenu represents select menu component.
  148. type SelectMenu struct {
  149. CustomID string `json:"custom_id,omitempty"`
  150. // The text which will be shown in the menu if there's no default options or all options was deselected and component was closed.
  151. Placeholder string `json:"placeholder"`
  152. // This value determines the minimal amount of selected items in the menu.
  153. MinValues *int `json:"min_values,omitempty"`
  154. // This value determines the maximal amount of selected items in the menu.
  155. // If MaxValues or MinValues are greater than one then the user can select multiple items in the component.
  156. MaxValues int `json:"max_values,omitempty"`
  157. Options []SelectMenuOption `json:"options"`
  158. Disabled bool `json:"disabled"`
  159. }
  160. // Type is a method to get the type of a component.
  161. func (SelectMenu) Type() ComponentType {
  162. return SelectMenuComponent
  163. }
  164. // MarshalJSON is a method for marshaling SelectMenu to a JSON object.
  165. func (m SelectMenu) MarshalJSON() ([]byte, error) {
  166. type selectMenu SelectMenu
  167. return json.Marshal(struct {
  168. selectMenu
  169. Type ComponentType `json:"type"`
  170. }{
  171. selectMenu: selectMenu(m),
  172. Type: m.Type(),
  173. })
  174. }
  175. // TextInput represents text input component.
  176. type TextInput struct {
  177. CustomID string `json:"custom_id"`
  178. Label string `json:"label"`
  179. Style TextInputStyle `json:"style"`
  180. Placeholder string `json:"placeholder,omitempty"`
  181. Value string `json:"value,omitempty"`
  182. Required bool `json:"required,omitempty"`
  183. MinLength int `json:"min_length,omitempty"`
  184. MaxLength int `json:"max_length,omitempty"`
  185. }
  186. // Type is a method to get the type of a component.
  187. func (TextInput) Type() ComponentType {
  188. return TextInputComponent
  189. }
  190. // MarshalJSON is a method for marshaling TextInput to a JSON object.
  191. func (m TextInput) MarshalJSON() ([]byte, error) {
  192. type inputText TextInput
  193. return json.Marshal(struct {
  194. inputText
  195. Type ComponentType `json:"type"`
  196. }{
  197. inputText: inputText(m),
  198. Type: m.Type(),
  199. })
  200. }
  201. // TextInputStyle is style of text in TextInput component.
  202. type TextInputStyle uint
  203. // Text styles
  204. const (
  205. TextInputShort TextInputStyle = 1
  206. TextInputParagraph TextInputStyle = 2
  207. )