onyxcord-webhooks 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,284 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Modal component builder.
4
+ class OnyxCord::Webhooks::Modal
5
+ # A mapping of names to types of components usable in a modal.
6
+ COMPONENT_TYPES = {
7
+ action_row: 1,
8
+ string_select: 3,
9
+ text_input: 4,
10
+ user_select: 5,
11
+ role_select: 6,
12
+ mentionable_select: 7,
13
+ channel_select: 8,
14
+ text_display: 10,
15
+ label: 18,
16
+ file_upload: 19,
17
+ radio_group: 21,
18
+ checkbox_group: 22,
19
+ checkbox: 23
20
+ }.freeze
21
+
22
+ # Builder for radio and checkbox groups.
23
+ class GroupBuilder
24
+ # @!visibility private
25
+ def initialize(type, custom_id, id, options = [], required = nil, min_values = nil, max_values = nil)
26
+ @id = id
27
+ @type = type
28
+ @custom_id = custom_id
29
+ @options = options
30
+ @required = required
31
+ @min_values = min_values
32
+ @max_values = max_values
33
+ end
34
+
35
+ # Add a checkbox component to the group.
36
+ # @param value [String] The value that the checkbox represents.
37
+ # @param label [String] The primary text of the checkbox.
38
+ # @param description [String, nil] The description of the checkbox.
39
+ # @param default [true, false, nil] Whether or not the checkbox should be checked by default.
40
+ def checkbox(value:, label:, description: nil, default: nil)
41
+ raise "Cannot add a checkbox to a #{@type}" unless @type == :checkbox_group
42
+
43
+ @options << { value: value, label: label, description: description, default: default }.compact
44
+ end
45
+
46
+ # Add a radio button component to the group.
47
+ # @param value [String] The value that the radio button represents.
48
+ # @param label [String] The primary text of the radio button.
49
+ # @param description [String, nil] The description of the radio button.
50
+ # @param default [true, false, nil] Whether or not the radio button should be selected by default.
51
+ def radio_button(value:, label:, description: nil, default: nil)
52
+ raise "Cannot add a radio button to a #{@type}" unless @type == :radio_group
53
+
54
+ @options << { value: value, label: label, description: description, default: default }.compact
55
+ end
56
+
57
+ alias_method :button, :radio_button
58
+
59
+ # @!visibility private
60
+ def to_h
61
+ {
62
+ id: @id,
63
+ type: COMPONENT_TYPES[@type],
64
+ custom_id: @custom_id,
65
+ options: @options.map(&:to_h),
66
+ required: @required,
67
+ min_values: @min_values,
68
+ max_values: @max_values
69
+ }.compact
70
+ end
71
+ end
72
+
73
+ # This builder is used when adding a label component to a modal.
74
+ class LabelBuilder
75
+ # A mapping of text input styles to symbol names. `short` is a single line where `paragraph` is a block.
76
+ TEXT_INPUT_STYLES = {
77
+ short: 1,
78
+ paragraph: 2
79
+ }.freeze
80
+
81
+ # Create a label component.
82
+ # @param label [String, nil] The label of the label component. This
83
+ # field should always be passed, and will be required in 4.0.
84
+ # @param id [Integer, nil] The unique 32-bit ID of the label component.
85
+ # @param description [String, nil] The description of the label component.
86
+ # @yieldparam builder [LabelBuilder] Yields the initialized label component.
87
+ def initialize(label: nil, id: nil, description: nil)
88
+ @id = id
89
+ @label = label
90
+ @description = description
91
+
92
+ yield self if block_given?
93
+ end
94
+
95
+ # Add a text input to the label component.
96
+ # @param style [Symbol, Integer] The text input's style type. See {TEXT_INPUT_STYLES}
97
+ # @param custom_id [String] Custom IDs are used to pass state to the events that are raised from interactions.
98
+ # There is a limit of 100 characters to each custom_id.
99
+ # @param id [Integer] The integer ID for this component. This is not to be confused with custom_id.
100
+ # @param min_length [Integer, nil] The minimum input length for a text input, min 0, max 4000.
101
+ # @param max_length [Integer, nil] The maximum input length for a text input, min 1, max 4000.
102
+ # @param required [true, false, nil] Whether this component is required to be filled, default true.
103
+ # @param value [String, nil] A pre-filled value for this component, max 4000 characters.
104
+ # @param placeholder [String, nil] Custom placeholder text if the input is empty, max 100 characters
105
+ # @param label [String, nil] This parameter is deprecated and will be removed soon. Please pass this argument to {LabelBuilder#initialize} instead.
106
+ def text_input(style:, custom_id:, id: nil, min_length: nil, max_length: nil, required: nil, value: nil, placeholder: nil, label: nil)
107
+ @label = label unless label.nil?
108
+
109
+ @component = {
110
+ id: id,
111
+ style: TEXT_INPUT_STYLES[style] || style,
112
+ custom_id: custom_id,
113
+ type: COMPONENT_TYPES[:text_input],
114
+ min_length: min_length,
115
+ max_length: max_length,
116
+ required: required,
117
+ value: value,
118
+ placeholder: placeholder
119
+ }.compact
120
+ end
121
+
122
+ # Add a string select menu to the label component.
123
+ # @param custom_id [String] Custom IDs are used to pass state to the events that are raised from interactions.
124
+ # There is a limit of 100 characters to each custom_id.
125
+ # @param id [Integer, nil] The unique 32-bit ID of the string select. This is not to be confused with the `custom_id`.
126
+ # @param options [Array<Hash>] Options that can be selected in this menu. Can also be provided via the yielded builder.
127
+ # @param placeholder [String, nil] Default text to show when no entries are selected.
128
+ # @param min_values [Integer, nil] The minimum amount of values a user must select.
129
+ # @param max_values [Integer, nil] The maximum amount of values a user can select.
130
+ # @param required [true, false, nil] Whether a value must be selected for the component.
131
+ # @yieldparam builder [SelectMenuBuilder] The select menu builder is yielded to allow for the modification of attributes.
132
+ def string_select(custom_id:, id: nil, options: [], placeholder: nil, min_values: nil, max_values: nil, required: nil)
133
+ builder = OnyxCord::Webhooks::View::SelectMenuBuilder.new(custom_id, options, placeholder, min_values, max_values, nil, select_type: :string_select, id: id, required: required)
134
+
135
+ yield builder if block_given?
136
+
137
+ @component = builder.to_h
138
+ end
139
+
140
+ alias_method :select_menu, :string_select
141
+
142
+ # Add a user select to the label component.
143
+ # @param custom_id [String] Custom IDs are used to pass state to the events that are raised from interactions.
144
+ # There is a limit of 100 characters to each custom_id.
145
+ # @param id [Integer, nil] The unique 32-bit ID of the user select. This is not to be confused with the `custom_id`.
146
+ # @param placeholder [String, nil] Default text to show when no entries are selected.
147
+ # @param min_values [Integer, nil] The minimum amount of values a user must select.
148
+ # @param max_values [Integer, nil] The maximum amount of values a user can select.
149
+ # @param required [true, false, nil] Whether a value must be selected for the component.
150
+ # @param default_values [Array<User, Member, Recipient, Integer, String, Hash>, nil] The users to populate in the select menu by default.
151
+ def user_select(custom_id:, id: nil, placeholder: nil, min_values: nil, max_values: nil, required: nil, default_values: nil)
152
+ @component = OnyxCord::Webhooks::View::SelectMenuBuilder.new(custom_id, [], placeholder, min_values, max_values, nil, select_type: :user_select, id: id, required: required, default_values: default_values).to_h
153
+ end
154
+
155
+ # Add a role select to the label component.
156
+ # @param custom_id [String] Custom IDs are used to pass state to the events that are raised from interactions.
157
+ # There is a limit of 100 characters to each custom_id.
158
+ # @param id [Integer, nil] The unique 32-bit ID of the role select. This is not to be confused with the `custom_id`.
159
+ # @param placeholder [String, nil] Default text to show when no entries are selected.
160
+ # @param min_values [Integer, nil] The minimum amount of values a user must select.
161
+ # @param max_values [Integer, nil] The maximum amount of values a user can select.
162
+ # @param required [true, false, nil] Whether a value must be selected for the component.
163
+ # @param default_values [Array<Role, Integer, String, Hash>, nil] The roles to populate in the select menu by default.
164
+ def role_select(custom_id:, id: nil, placeholder: nil, min_values: nil, max_values: nil, required: nil, default_values: nil)
165
+ @component = OnyxCord::Webhooks::View::SelectMenuBuilder.new(custom_id, [], placeholder, min_values, max_values, nil, select_type: :role_select, id: id, required: required, default_values: default_values).to_h
166
+ end
167
+
168
+ # Add a mentionable select to the label component.
169
+ # @param custom_id [String] Custom IDs are used to pass state to the events that are raised from interactions.
170
+ # There is a limit of 100 characters to each custom_id.
171
+ # @param id [Integer, nil] The unique 32-bit ID of the mentionable select. This is not to be confused with the `custom_id`.
172
+ # @param placeholder [String, nil] Default text to show when no entries are selected.
173
+ # @param min_values [Integer, nil] The minimum amount of values a user must select.
174
+ # @param max_values [Integer, nil] The maximum amount of values a user can select.
175
+ # @param required [true, false, nil] Whether a value must be selected for the component.
176
+ # @param default_values [Array<User, Role, Member, Recipient, Hash>, nil] The mentionable entities to populate in the select menu by default.
177
+ def mentionable_select(custom_id:, id: nil, placeholder: nil, min_values: nil, max_values: nil, required: nil, default_values: nil)
178
+ @component = OnyxCord::Webhooks::View::SelectMenuBuilder.new(custom_id, [], placeholder, min_values, max_values, nil, select_type: :mentionable_select, id: id, required: required, default_values: default_values).to_h
179
+ end
180
+
181
+ # Add a channel select to the label component.
182
+ # @param custom_id [String] Custom IDs are used to pass state to the events that are raised from interactions.
183
+ # There is a limit of 100 characters to each custom_id.
184
+ # @param id [Integer, nil] The unique 32-bit ID of the channel select. This is not to be confused with the `custom_id`.
185
+ # @param placeholder [String, nil] Default text to show when no entries are selected.
186
+ # @param min_values [Integer, nil] The minimum amount of values a user must select.
187
+ # @param max_values [Integer, nil] The maximum amount of values a user can select.
188
+ # @param required [true, false, nil] Whether a value must be selected for the component.
189
+ # @param types [Array<Symbol, Integer>, nil] The channel types to include in the select menu.
190
+ # @param default_values [Array<Channel, Integer, String, Hash>, nil] The channels to populate in the select menu by default.
191
+ def channel_select(custom_id:, id: nil, placeholder: nil, min_values: nil, max_values: nil, required: nil, types: nil, default_values: nil)
192
+ builder = OnyxCord::Webhooks::View::SelectMenuBuilder.new(custom_id, [], placeholder, min_values, max_values, nil, select_type: :channel_select, id: id, required: required, default_values: default_values).to_h
193
+
194
+ builder[:channel_types] = types.map { |type| OnyxCord::Channel::TYPES[type] || type } if types
195
+
196
+ @component = builder
197
+ end
198
+
199
+ # Add a file upload component to the label component.
200
+ # @param custom_id [String] Custom IDs are used to pass state to the events that are raised from interactions.
201
+ # There is a limit of 100 characters to each custom_id.
202
+ # @param id [Integer, nil] The unique 32-bit ID of the file upload component. This is not to be confused with the `custom_id`.
203
+ # @param min_values [Integer, nil] The minimum amount of files a user must upload.
204
+ # @param max_values [Integer, nil] The maximum amount of files a user has to upload.
205
+ # @param required [true, false, nil] Whether or not a file must be uploaded to the component.
206
+ def file_upload(custom_id:, id: nil, min_values: nil, max_values: nil, required: nil)
207
+ @component = { type: COMPONENT_TYPES[:file_upload], custom_id: custom_id, id: id, min_values: min_values, max_values: max_values, required: required }.compact
208
+ end
209
+
210
+ # Add a standalone checkbox component to the label component.
211
+ # @param custom_id [String] Custom IDs are used to pass state to the events that are raised from interactions.
212
+ # There is a limit of 100 characters to each custom_id.
213
+ # @param id [Integer, nil] The unique 32-bit ID of the checkbox component. This is not to be confused with the `custom_id`.
214
+ # @param default [true, false, nil] Whether or not the checkbox is checked by default.
215
+ def checkbox(custom_id:, id: nil, default: false)
216
+ @component = { type: COMPONENT_TYPES[:checkbox], custom_id: custom_id, id: id, default: default }.compact
217
+ end
218
+
219
+ # Add a group of radio buttons to the label component.
220
+ # @param custom_id [String] Custom IDs are used to pass state to the events that are raised from interactions.
221
+ # There is a limit of 100 characters to each custom_id.
222
+ # @param id [Integer, nil] The unique 32-bit ID of the radio group component. This is not to be confused with the `custom_id`.
223
+ # @param buttons [Array<Hash>] Radio buttons for the group. Can also be provided via the yielded builder.
224
+ # @param required [true, false, nil] Whether or not a radio button in the group must be selected.
225
+ def radio_group(custom_id:, id: nil, buttons: [], required: nil)
226
+ builder = GroupBuilder.new(:radio_group, custom_id, id, buttons, required)
227
+
228
+ yield builder if block_given?
229
+
230
+ @component = builder.to_h
231
+ end
232
+
233
+ # Add a group of checkboxes to the label component.
234
+ # @param custom_id [String] Custom IDs are used to pass state to the events that are raised from interactions.
235
+ # There is a limit of 100 characters to each custom_id.
236
+ # @param id [Integer, nil] The unique 32-bit ID of the checkbox group component. This is not to be confused with the `custom_id`.
237
+ # @param checkboxes [Array<Hash>] Checkboxes for the group. Can also be provided via the yielded builder.
238
+ # @param min_values [Integer, nil] The minimum number of checkboxes a user must check.
239
+ # @param max_values [Integer, nil] The maximum number of checkboxes a user is allowed to check.
240
+ # @param required [true, false, nil] Whether or not a checkbox must be checked.
241
+ def checkbox_group(custom_id:, id: nil, checkboxes: [], min_values: nil, max_values: nil, required: nil)
242
+ builder = GroupBuilder.new(:checkbox_group, custom_id, id, checkboxes, required, min_values, max_values)
243
+
244
+ yield builder if block_given?
245
+
246
+ @component = builder.to_h
247
+ end
248
+
249
+ # @!visibility private
250
+ def to_h
251
+ { type: COMPONENT_TYPES[:label], label: @label, description: @description, component: @component }.compact
252
+ end
253
+ end
254
+
255
+ # @!visibility private
256
+ def initialize
257
+ @components = []
258
+
259
+ yield self if block_given?
260
+ end
261
+
262
+ # @!visibility private
263
+ def to_a
264
+ @components.map(&:to_h)
265
+ end
266
+
267
+ # Add a label component to the view.
268
+ # @see LabelBuilder#initialize
269
+ def label(...)
270
+ @components << LabelBuilder.new(...)
271
+ end
272
+
273
+ # Add a text display component to the view.
274
+ # @see Webhooks::View::TextDisplayBuilder#initialize
275
+ def text_display(...)
276
+ @components << OnyxCord::Webhooks::View::TextDisplayBuilder.new(...)
277
+ end
278
+
279
+ # @deprecated Please use {#label} instead.
280
+ alias_method :row, :label
281
+
282
+ # @deprecated This alias will be removed in future releases.
283
+ RowBuilder = LabelBuilder
284
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Webhook support for onyxcord
4
+ module OnyxCord
5
+ module Webhooks
6
+ # The current version of onyxcord-webhooks.
7
+ VERSION = '1.1.0'
8
+ end
9
+ end