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.
- checksums.yaml +7 -0
- data/lib/onyxcord/webhooks/builder.rb +150 -0
- data/lib/onyxcord/webhooks/client.rb +172 -0
- data/lib/onyxcord/webhooks/embeds.rb +250 -0
- data/lib/onyxcord/webhooks/modal.rb +284 -0
- data/lib/onyxcord/webhooks/version.rb +9 -0
- data/lib/onyxcord/webhooks/view.rb +538 -0
- data/lib/onyxcord/webhooks.rb +14 -0
- metadata +66 -0
|
@@ -0,0 +1,538 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'onyxcord/message_components'
|
|
4
|
+
|
|
5
|
+
# A reusable view representing a component collection, with builder methods.
|
|
6
|
+
class OnyxCord::Webhooks::View
|
|
7
|
+
# Possible button style names and values.
|
|
8
|
+
BUTTON_STYLES = {
|
|
9
|
+
primary: 1,
|
|
10
|
+
secondary: 2,
|
|
11
|
+
success: 3,
|
|
12
|
+
danger: 4,
|
|
13
|
+
link: 5,
|
|
14
|
+
premium: 6
|
|
15
|
+
}.freeze
|
|
16
|
+
|
|
17
|
+
# Possible separator size names and values.
|
|
18
|
+
SEPARATOR_SIZES = {
|
|
19
|
+
small: 1,
|
|
20
|
+
large: 2
|
|
21
|
+
}.freeze
|
|
22
|
+
|
|
23
|
+
# Component types.
|
|
24
|
+
# @see https://discord.com/developers/docs/components/reference#component-object-component-types
|
|
25
|
+
COMPONENT_TYPES = {
|
|
26
|
+
action_row: 1,
|
|
27
|
+
button: 2,
|
|
28
|
+
string_select: 3,
|
|
29
|
+
# text_input: 4, # (defined in modal.rb)
|
|
30
|
+
user_select: 5,
|
|
31
|
+
role_select: 6,
|
|
32
|
+
mentionable_select: 7,
|
|
33
|
+
channel_select: 8,
|
|
34
|
+
section: 9,
|
|
35
|
+
text_display: 10,
|
|
36
|
+
thumbnail: 11,
|
|
37
|
+
media_gallery: 12,
|
|
38
|
+
file: 13,
|
|
39
|
+
separator: 14,
|
|
40
|
+
container: 17
|
|
41
|
+
# label: 18, # (defined in modal.rb)
|
|
42
|
+
# file_upload: 19, (defined in modal.rb)
|
|
43
|
+
# radio_group: 21, (defined in modal.rb)
|
|
44
|
+
# checkbox_group: 22, (defined in modal.rb)
|
|
45
|
+
# checkbox: 23 (defined in modal.rb)
|
|
46
|
+
}.freeze
|
|
47
|
+
|
|
48
|
+
IS_COMPONENTS_V2 = OnyxCord::MessageComponents::IS_COMPONENTS_V2
|
|
49
|
+
V2_COMPONENT_TYPES = OnyxCord::MessageComponents::V2_COMPONENT_TYPES
|
|
50
|
+
|
|
51
|
+
# This builder is used when constructing an ActionRow. Button and select menu components must be within an action row, but this can
|
|
52
|
+
# change in the future. A message can have 10 action rows, each action row can hold a weight of 5. Buttons have a weight of 1,
|
|
53
|
+
# and dropdowns have a weight of 5.
|
|
54
|
+
class RowBuilder
|
|
55
|
+
# @!visibility private
|
|
56
|
+
def initialize(id: nil)
|
|
57
|
+
@id = id
|
|
58
|
+
@components = []
|
|
59
|
+
|
|
60
|
+
yield self if block_given?
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
# Add a button to this action row.
|
|
64
|
+
# @param style [Symbol, Integer] The button's style type. See {BUTTON_STYLES}
|
|
65
|
+
# @param id [Integer, nil] The unique 32-bit ID of the button component. This is not to be confused with the `custom_id`.
|
|
66
|
+
# @param label [String, nil] The text label for the button. Either a label or emoji must be provided.
|
|
67
|
+
# @param emoji [#to_h, String, Integer] An emoji ID, or unicode emoji to attach to the button. Can also be an object
|
|
68
|
+
# that responds to `#to_h` which returns a hash in the format of `{ id: Integer, name: string }`.
|
|
69
|
+
# @param custom_id [String] Custom IDs are used to pass state to the events that are raised from interactions.
|
|
70
|
+
# There is a limit of 100 characters to each custom_id.
|
|
71
|
+
# @param disabled [true, false] Whether this button is disabled and shown as greyed out.
|
|
72
|
+
# @param url [String, nil] The URL, when using a link style button.
|
|
73
|
+
def button(style:, id: nil, label: nil, emoji: nil, custom_id: nil, disabled: nil, url: nil, sku_id: nil)
|
|
74
|
+
style = BUTTON_STYLES[style] || style
|
|
75
|
+
|
|
76
|
+
emoji = case emoji
|
|
77
|
+
when Integer, String
|
|
78
|
+
emoji.to_i.positive? ? { id: emoji } : { name: emoji }
|
|
79
|
+
else
|
|
80
|
+
emoji&.to_h
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
@components << { type: COMPONENT_TYPES[:button], id: id, label: label, emoji: emoji, style: style, custom_id: custom_id, disabled: disabled, url: url, sku_id: sku_id }.compact
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
# Add a string select to this action row.
|
|
87
|
+
# @param custom_id [String] Custom IDs are used to pass state to the events that are raised from interactions.
|
|
88
|
+
# There is a limit of 100 characters to each custom_id.
|
|
89
|
+
# @param id [Integer, nil] The unique 32-bit ID of the string select. This is not to be confused with the `custom_id`.
|
|
90
|
+
# @param options [Array<Hash>] Options that can be selected in this menu. Can also be provided via the yielded builder.
|
|
91
|
+
# @param placeholder [String, nil] Default text to show when no entries are selected.
|
|
92
|
+
# @param min_values [Integer, nil] The minimum amount of values a user must select.
|
|
93
|
+
# @param max_values [Integer, nil] The maximum amount of values a user can select.
|
|
94
|
+
# @param disabled [true, false, nil] Grey out the component to make it unusable.
|
|
95
|
+
# @yieldparam builder [SelectMenuBuilder]
|
|
96
|
+
def string_select(custom_id:, options: [], id: nil, placeholder: nil, min_values: nil, max_values: nil, disabled: nil)
|
|
97
|
+
builder = SelectMenuBuilder.new(custom_id, options, placeholder, min_values, max_values, disabled, select_type: :string_select, id: id)
|
|
98
|
+
|
|
99
|
+
yield builder if block_given?
|
|
100
|
+
|
|
101
|
+
@components << builder.to_h
|
|
102
|
+
end
|
|
103
|
+
|
|
104
|
+
alias_method :select_menu, :string_select
|
|
105
|
+
|
|
106
|
+
# Add a user select to this action row.
|
|
107
|
+
# @param custom_id [String] Custom IDs are used to pass state to the events that are raised from interactions.
|
|
108
|
+
# There is a limit of 100 characters to each custom_id.
|
|
109
|
+
# @param id [Integer, nil] The unique 32-bit ID of the user select. This is not to be confused with the `custom_id`.
|
|
110
|
+
# @param placeholder [String, nil] Default text to show when no entries are selected.
|
|
111
|
+
# @param min_values [Integer, nil] The minimum amount of values a user must select.
|
|
112
|
+
# @param max_values [Integer, nil] The maximum amount of values a user can select.
|
|
113
|
+
# @param disabled [true, false, nil] Grey out the component to make it unusable.
|
|
114
|
+
# @param default_values [Array<User, Member, Recipient, Integer, String, Hash>, nil] The users to populate in the select menu by default.
|
|
115
|
+
def user_select(custom_id:, id: nil, placeholder: nil, min_values: nil, max_values: nil, disabled: nil, default_values: nil)
|
|
116
|
+
@components << SelectMenuBuilder.new(custom_id, [], placeholder, min_values, max_values, disabled, select_type: :user_select, id: id, default_values: default_values).to_h
|
|
117
|
+
end
|
|
118
|
+
|
|
119
|
+
# Add a role select to this action row.
|
|
120
|
+
# @param custom_id [String] Custom IDs are used to pass state to the events that are raised from interactions.
|
|
121
|
+
# There is a limit of 100 characters to each custom_id.
|
|
122
|
+
# @param id [Integer, nil] The unique 32-bit ID of the role select. This is not to be confused with the `custom_id`.
|
|
123
|
+
# @param placeholder [String, nil] Default text to show when no entries are selected.
|
|
124
|
+
# @param min_values [Integer, nil] The minimum amount of values a user must select.
|
|
125
|
+
# @param max_values [Integer, nil] The maximum amount of values a user can select.
|
|
126
|
+
# @param disabled [true, false, nil] Grey out the component to make it unusable.
|
|
127
|
+
# @param default_values [Array<Role, Integer, String, Hash>, nil] The roles to populate in the select menu by default.
|
|
128
|
+
def role_select(custom_id:, id: nil, placeholder: nil, min_values: nil, max_values: nil, disabled: nil, default_values: nil)
|
|
129
|
+
@components << SelectMenuBuilder.new(custom_id, [], placeholder, min_values, max_values, disabled, select_type: :role_select, id: id, default_values: default_values).to_h
|
|
130
|
+
end
|
|
131
|
+
|
|
132
|
+
# Add a mentionable select to this action row.
|
|
133
|
+
# @param custom_id [String] Custom IDs are used to pass state to the events that are raised from interactions.
|
|
134
|
+
# There is a limit of 100 characters to each custom_id.
|
|
135
|
+
# @param id [Integer, nil] The unique 32-bit ID of the mentionable select. This is not to be confused with the `custom_id`.
|
|
136
|
+
# @param placeholder [String, nil] Default text to show when no entries are selected.
|
|
137
|
+
# @param min_values [Integer, nil] The minimum amount of values a user must select.
|
|
138
|
+
# @param max_values [Integer, nil] The maximum amount of values a user can select.
|
|
139
|
+
# @param disabled [true, false, nil] Grey out the component to make it unusable.
|
|
140
|
+
# @param default_values [Array<User, Role, Member, Recipient, Hash>, nil] The mentionable entities to populate in the select menu by default.
|
|
141
|
+
def mentionable_select(custom_id:, id: nil, placeholder: nil, min_values: nil, max_values: nil, disabled: nil, default_values: nil)
|
|
142
|
+
@components << SelectMenuBuilder.new(custom_id, [], placeholder, min_values, max_values, disabled, select_type: :mentionable_select, id: id, default_values: default_values).to_h
|
|
143
|
+
end
|
|
144
|
+
|
|
145
|
+
# Add a channel select to this action row.
|
|
146
|
+
# @param custom_id [String] Custom IDs are used to pass state to the events that are raised from interactions.
|
|
147
|
+
# There is a limit of 100 characters to each custom_id.
|
|
148
|
+
# @param id [Integer, nil] The unique 32-bit ID of the channel select. This is not to be confused with the `custom_id`.
|
|
149
|
+
# @param placeholder [String, nil] Default text to show when no entries are selected.
|
|
150
|
+
# @param min_values [Integer, nil] The minimum amount of values a user must select.
|
|
151
|
+
# @param max_values [Integer, nil] The maximum amount of values a user can select.
|
|
152
|
+
# @param disabled [true, false, nil] Grey out the component to make it unusable.
|
|
153
|
+
# @param types [Array<Symbol, Integer>, nil] The channel types to include in the select menu.
|
|
154
|
+
# @param default_values [Array<Channel, Integer, String, Hash>, nil] The channels to populate in the select menu by default.
|
|
155
|
+
def channel_select(custom_id:, id: nil, placeholder: nil, min_values: nil, max_values: nil, disabled: nil, types: nil, default_values: nil)
|
|
156
|
+
builder = SelectMenuBuilder.new(custom_id, [], placeholder, min_values, max_values, disabled, select_type: :channel_select, id: id, default_values: default_values).to_h
|
|
157
|
+
|
|
158
|
+
builder[:channel_types] = types.map { |type| OnyxCord::Channel::TYPES[type] || type } if types
|
|
159
|
+
|
|
160
|
+
@components << builder
|
|
161
|
+
end
|
|
162
|
+
|
|
163
|
+
# @!visibility private
|
|
164
|
+
def to_h
|
|
165
|
+
{ type: COMPONENT_TYPES[:action_row], id: @id, components: @components }.compact
|
|
166
|
+
end
|
|
167
|
+
end
|
|
168
|
+
|
|
169
|
+
# A builder to assist in adding options to select menus.
|
|
170
|
+
class SelectMenuBuilder
|
|
171
|
+
# @!visibility private
|
|
172
|
+
def initialize(custom_id, options = [], placeholder = nil, min_values = nil, max_values = nil, disabled = nil, select_type: :string_select, id: nil, required: nil, default_values: nil)
|
|
173
|
+
@id = id
|
|
174
|
+
@custom_id = custom_id
|
|
175
|
+
@options = options
|
|
176
|
+
@placeholder = placeholder
|
|
177
|
+
@min_values = min_values
|
|
178
|
+
@max_values = max_values
|
|
179
|
+
@disabled = disabled
|
|
180
|
+
@select_type = select_type
|
|
181
|
+
@required = required
|
|
182
|
+
@default_values = process_defaults(default_values)
|
|
183
|
+
end
|
|
184
|
+
|
|
185
|
+
# Add an option to this select menu.
|
|
186
|
+
# @param label [String] The title of this option.
|
|
187
|
+
# @param value [String] The value that this option represents.
|
|
188
|
+
# @param description [String, nil] An optional description of the option.
|
|
189
|
+
# @param emoji [#to_h, String, Integer] An emoji ID, or unicode emoji to attach to the button. Can also be an object
|
|
190
|
+
# that responds to `#to_h` which returns a hash in the format of `{ id: Integer, name: string }`.
|
|
191
|
+
# @param default [true, false, nil] Whether this is the default selected option.
|
|
192
|
+
def option(label:, value:, description: nil, emoji: nil, default: nil)
|
|
193
|
+
emoji = case emoji
|
|
194
|
+
when Integer, String
|
|
195
|
+
emoji.to_i.positive? ? { id: emoji } : { name: emoji }
|
|
196
|
+
else
|
|
197
|
+
emoji&.to_h
|
|
198
|
+
end
|
|
199
|
+
|
|
200
|
+
@options << { label: label, value: value, description: description, emoji: emoji, default: default }
|
|
201
|
+
end
|
|
202
|
+
|
|
203
|
+
# @!visibility private
|
|
204
|
+
def to_h
|
|
205
|
+
{
|
|
206
|
+
type: COMPONENT_TYPES[@select_type],
|
|
207
|
+
id: @id,
|
|
208
|
+
options: @options,
|
|
209
|
+
placeholder: @placeholder,
|
|
210
|
+
min_values: @min_values,
|
|
211
|
+
max_values: @max_values,
|
|
212
|
+
custom_id: @custom_id,
|
|
213
|
+
disabled: @disabled,
|
|
214
|
+
required: @required,
|
|
215
|
+
default_values: @default_values
|
|
216
|
+
}.compact
|
|
217
|
+
end
|
|
218
|
+
|
|
219
|
+
private
|
|
220
|
+
|
|
221
|
+
# @!visibility private
|
|
222
|
+
def process_defaults(default_values)
|
|
223
|
+
if @select_type == :mentionable_select
|
|
224
|
+
default_values&.map do |default_value|
|
|
225
|
+
case default_value
|
|
226
|
+
when OnyxCord::Recipient, OnyxCord::User, OnyxCord::Member
|
|
227
|
+
{ id: default_value.id, type: :user }
|
|
228
|
+
when OnyxCord::Role
|
|
229
|
+
{ id: default_value.id, type: :role }
|
|
230
|
+
when Hash
|
|
231
|
+
default_value
|
|
232
|
+
else
|
|
233
|
+
raise TypeError, "Unsupported type: #{default_value.class}"
|
|
234
|
+
end
|
|
235
|
+
end
|
|
236
|
+
else
|
|
237
|
+
default_values&.map { |id| id.is_a?(Hash) ? id : { id: id.resolve_id, type: @select_type[..-8] } }
|
|
238
|
+
end
|
|
239
|
+
end
|
|
240
|
+
end
|
|
241
|
+
|
|
242
|
+
# A text display component allows you to send message content.
|
|
243
|
+
class TextDisplayBuilder
|
|
244
|
+
# Create a text display component.
|
|
245
|
+
# @param content [String] The content of the text display component.
|
|
246
|
+
# @param id [Integer, nil] The unique 32-bit ID of the text display component.
|
|
247
|
+
def initialize(content:, id: nil)
|
|
248
|
+
@id = id
|
|
249
|
+
@content = content
|
|
250
|
+
end
|
|
251
|
+
|
|
252
|
+
# @!visibility private
|
|
253
|
+
def to_h
|
|
254
|
+
{ type: COMPONENT_TYPES[:text_display], id: @id, content: @content }.compact
|
|
255
|
+
end
|
|
256
|
+
end
|
|
257
|
+
|
|
258
|
+
# A separator allows you to add a barrier between components.
|
|
259
|
+
class SeparatorBuilder
|
|
260
|
+
# Create a separator component.
|
|
261
|
+
# @param divider [true, false] Whether or not the separator should act as a visible barrier.
|
|
262
|
+
# @param id [Integer, nil] The unique 32-bit ID of the separator component.
|
|
263
|
+
# @param spacing [Symbol, Integer] The size of the separator component's padding. See {SEPARATOR_SIZES}.
|
|
264
|
+
def initialize(divider:, id: nil, spacing: nil)
|
|
265
|
+
@id = id
|
|
266
|
+
@divider = divider
|
|
267
|
+
@spacing = SEPARATOR_SIZES[spacing] || spacing
|
|
268
|
+
end
|
|
269
|
+
|
|
270
|
+
# @!visibility private
|
|
271
|
+
def to_h
|
|
272
|
+
{ type: COMPONENT_TYPES[:separator], id: @id, divider: @divider, spacing: @spacing }.compact
|
|
273
|
+
end
|
|
274
|
+
end
|
|
275
|
+
|
|
276
|
+
# A file component lets you send a file via an attachment://<filename> reference.
|
|
277
|
+
class FileBuilder
|
|
278
|
+
# Create a file component.
|
|
279
|
+
# @param url [String] An `attachment://<filename>` reference to the attached file.
|
|
280
|
+
# @param id [Integer, nil] The unique 32-bit ID of the file component.
|
|
281
|
+
# @param spoiler [true, false] Whether or not to apply a spoiler label to the file.
|
|
282
|
+
def initialize(url:, id: nil, spoiler: false)
|
|
283
|
+
@id = id
|
|
284
|
+
@file = { url: }
|
|
285
|
+
@spoiler = spoiler
|
|
286
|
+
end
|
|
287
|
+
|
|
288
|
+
# @!visibility private
|
|
289
|
+
def to_h
|
|
290
|
+
{ type: COMPONENT_TYPES[:file], id: @id, spoiler: @spoiler, file: @file }.compact
|
|
291
|
+
end
|
|
292
|
+
end
|
|
293
|
+
|
|
294
|
+
# A media gallery component is a gallery grid.
|
|
295
|
+
class MediaGalleryBuilder
|
|
296
|
+
# Create a media gallery component.
|
|
297
|
+
# @param id [Integer, nil] The unique 32-bit ID of the media gallery component.
|
|
298
|
+
# @yieldparam builder [MediaGalleryBuilder] Yields the initialized media gallery component.
|
|
299
|
+
def initialize(id: nil)
|
|
300
|
+
@id = id
|
|
301
|
+
@items = []
|
|
302
|
+
|
|
303
|
+
yield self if block_given?
|
|
304
|
+
end
|
|
305
|
+
|
|
306
|
+
# Add a gallery item to the media gallery component.
|
|
307
|
+
# @param url [String] The URL to the gallery item's media.
|
|
308
|
+
# @param description [String, nil] The description of the gallery item.
|
|
309
|
+
# @param spoiler [true, false] Whether or not to apply a spoiler label to the gallery item.
|
|
310
|
+
def item(url:, description: nil, spoiler: false)
|
|
311
|
+
@items << { media: { url: }, description: description, spoiler: spoiler }.compact
|
|
312
|
+
end
|
|
313
|
+
|
|
314
|
+
# @!visibility private
|
|
315
|
+
def to_h
|
|
316
|
+
{ type: COMPONENT_TYPES[:media_gallery], id: @id, items: @items }.compact
|
|
317
|
+
end
|
|
318
|
+
end
|
|
319
|
+
|
|
320
|
+
# A section allows you to group together an accessory with text display components.
|
|
321
|
+
class SectionBuilder
|
|
322
|
+
# Create a section component.
|
|
323
|
+
# @param id [Integer, nil] The unique 32-bit ID of the section component.
|
|
324
|
+
# @yieldparam builder [SectionBuilder] Yields the initialized section component.
|
|
325
|
+
def initialize(id: nil)
|
|
326
|
+
@id = id
|
|
327
|
+
@accessory = nil
|
|
328
|
+
@components = []
|
|
329
|
+
|
|
330
|
+
yield self if block_given?
|
|
331
|
+
end
|
|
332
|
+
|
|
333
|
+
# Add a text display component to this section.
|
|
334
|
+
# @see TextDisplayBuilder#initialize
|
|
335
|
+
def text_display(...)
|
|
336
|
+
@components << TextDisplayBuilder.new(...)
|
|
337
|
+
end
|
|
338
|
+
|
|
339
|
+
# Set the thumbnail for the section. This is mutually exclusive with {#button}.
|
|
340
|
+
# @param url [String] The URL to the thumbnail image.
|
|
341
|
+
# @param id [Integer, nil] The unique 32-bit ID of the thumbnail component.
|
|
342
|
+
# @param description [String, nil] The description of the thumbnail.
|
|
343
|
+
# @param spoiler [true, false] Whether or not to apply a spoiler label to the thumbnail.
|
|
344
|
+
def thumbnail(url:, id: nil, description: nil, spoiler: false)
|
|
345
|
+
@accessory = { type: COMPONENT_TYPES[:thumbnail], id: id, media: { url: }, description: description, spoiler: spoiler }.compact
|
|
346
|
+
end
|
|
347
|
+
|
|
348
|
+
# Set the button for the section. This is mutually exclusive with {#thumbnail}.
|
|
349
|
+
# @param style [Symbol, Integer] The button's style type. See {BUTTON_STYLES}
|
|
350
|
+
# @param id [Integer, nil] The unique 32-bit ID of the button component. This is not to be confused with the `custom_id`.
|
|
351
|
+
# @param label [String, nil] The text label for the button. Either a label or emoji must be provided.
|
|
352
|
+
# @param emoji [#to_h, String, Integer] An emoji ID, or unicode emoji to attach to the button. Can also be an object
|
|
353
|
+
# that responds to `#to_h` which returns a hash in the format of `{ id: Integer, name: string }`.
|
|
354
|
+
# @param custom_id [String] Custom IDs are used to pass state to the events that are raised from interactions.
|
|
355
|
+
# There is a limit of 100 characters to each custom_id.
|
|
356
|
+
# @param disabled [true, false] Whether this button is disabled and shown as greyed out.
|
|
357
|
+
# @param url [String, nil] The URL, when using a link style button.
|
|
358
|
+
def button(style:, id: nil, label: nil, emoji: nil, custom_id: nil, disabled: nil, url: nil, sku_id: nil)
|
|
359
|
+
style = BUTTON_STYLES[style] || style
|
|
360
|
+
|
|
361
|
+
emoji = case emoji
|
|
362
|
+
when Integer, String
|
|
363
|
+
emoji.to_i.positive? ? { id: emoji } : { name: emoji }
|
|
364
|
+
else
|
|
365
|
+
emoji&.to_h
|
|
366
|
+
end
|
|
367
|
+
|
|
368
|
+
@accessory = { type: COMPONENT_TYPES[:button], id: id, label: label, emoji: emoji, style: style, custom_id: custom_id, disabled: disabled, url: url, sku_id: sku_id }.compact
|
|
369
|
+
end
|
|
370
|
+
|
|
371
|
+
# @!visibility private
|
|
372
|
+
def to_h
|
|
373
|
+
{ type: COMPONENT_TYPES[:section], id: @id, components: @components.map(&:to_h), accessory: @accessory }.compact
|
|
374
|
+
end
|
|
375
|
+
end
|
|
376
|
+
|
|
377
|
+
# This builder can be used to construct a container. These are similar to embeds.
|
|
378
|
+
class ContainerBuilder
|
|
379
|
+
# Create a container component.
|
|
380
|
+
# @param id [Integer, nil] The unique 32-bit ID of the container component.
|
|
381
|
+
# @param colour [Array, Integer, String, ColourRGB, nil] The accent colour of the container
|
|
382
|
+
# component. This argument can be passed via the American spelling (`color:`) as well.
|
|
383
|
+
# @param spoiler [true, false] Whether or not to apply a spoiler label to the container component.
|
|
384
|
+
# @yieldparam builder [ContainerBuilder] Yields the initialized container component.
|
|
385
|
+
def initialize(id: nil, color: nil, colour: nil, spoiler: false)
|
|
386
|
+
@id = id
|
|
387
|
+
@spoiler = spoiler
|
|
388
|
+
@components = []
|
|
389
|
+
self.colour = (colour || color)
|
|
390
|
+
|
|
391
|
+
yield self if block_given?
|
|
392
|
+
end
|
|
393
|
+
|
|
394
|
+
# Add a row component to the container.
|
|
395
|
+
# @see RowBuilder#initialize
|
|
396
|
+
def row(...)
|
|
397
|
+
@components << RowBuilder.new(...)
|
|
398
|
+
end
|
|
399
|
+
|
|
400
|
+
# Add a file component to the container.
|
|
401
|
+
# @see FileBuilder#initialize
|
|
402
|
+
def file(...)
|
|
403
|
+
@components << FileBuilder.new(...)
|
|
404
|
+
end
|
|
405
|
+
|
|
406
|
+
alias_method :file_display, :file
|
|
407
|
+
|
|
408
|
+
# Add a section component to the container.
|
|
409
|
+
# @see SectionBuilder#initialize
|
|
410
|
+
def section(...)
|
|
411
|
+
@components << SectionBuilder.new(...)
|
|
412
|
+
end
|
|
413
|
+
|
|
414
|
+
# Add a separator component to the container.
|
|
415
|
+
# @see SeparatorBuilder#initialize
|
|
416
|
+
def separator(...)
|
|
417
|
+
@components << SeparatorBuilder.new(...)
|
|
418
|
+
end
|
|
419
|
+
|
|
420
|
+
# Add a text display component to the container.
|
|
421
|
+
# @see TextDisplayBuilder#initialize
|
|
422
|
+
def text_display(...)
|
|
423
|
+
@components << TextDisplayBuilder.new(...)
|
|
424
|
+
end
|
|
425
|
+
|
|
426
|
+
# Add a media gallery component to the container.
|
|
427
|
+
# @see MediaGalleryBuilder#initialize
|
|
428
|
+
def media_gallery(...)
|
|
429
|
+
@components << MediaGalleryBuilder.new(...)
|
|
430
|
+
end
|
|
431
|
+
|
|
432
|
+
# Set the color of the container.
|
|
433
|
+
# @param colour [Array, Integer, String, ColourRGB, nil] The accent colour of the container component, or `nil` to clear the accent colour.
|
|
434
|
+
def colour=(colour)
|
|
435
|
+
@colour = case colour
|
|
436
|
+
when Array
|
|
437
|
+
(colour[0] << 16) | (colour[1] << 8) | colour[2]
|
|
438
|
+
when String
|
|
439
|
+
colour.delete('#').to_i(16)
|
|
440
|
+
else
|
|
441
|
+
colour&.to_i
|
|
442
|
+
end
|
|
443
|
+
end
|
|
444
|
+
|
|
445
|
+
alias_method :color=, :colour=
|
|
446
|
+
|
|
447
|
+
# @!visibility private
|
|
448
|
+
def to_h
|
|
449
|
+
{ type: COMPONENT_TYPES[:container], id: @id, accent_color: @colour, spoiler: @spoiler, components: @components.map(&:to_h) }.compact
|
|
450
|
+
end
|
|
451
|
+
end
|
|
452
|
+
|
|
453
|
+
# @!visibility private
|
|
454
|
+
def initialize
|
|
455
|
+
@components = []
|
|
456
|
+
|
|
457
|
+
yield self if block_given?
|
|
458
|
+
end
|
|
459
|
+
|
|
460
|
+
# @!visibility private
|
|
461
|
+
def to_a
|
|
462
|
+
@components.map(&:to_h)
|
|
463
|
+
end
|
|
464
|
+
|
|
465
|
+
def empty?
|
|
466
|
+
@components.empty?
|
|
467
|
+
end
|
|
468
|
+
|
|
469
|
+
def any?
|
|
470
|
+
@components.any?
|
|
471
|
+
end
|
|
472
|
+
|
|
473
|
+
def components_v2?
|
|
474
|
+
self.class.components_v2?(to_a)
|
|
475
|
+
end
|
|
476
|
+
|
|
477
|
+
alias v2? components_v2?
|
|
478
|
+
|
|
479
|
+
def flags(flags = 0)
|
|
480
|
+
self.class.apply_v2_flag(flags, to_a)
|
|
481
|
+
end
|
|
482
|
+
|
|
483
|
+
def self.components_v2?(components)
|
|
484
|
+
OnyxCord::MessageComponents.components_v2?(components)
|
|
485
|
+
end
|
|
486
|
+
|
|
487
|
+
def self.component_payload(components)
|
|
488
|
+
OnyxCord::MessageComponents.payload(components)
|
|
489
|
+
end
|
|
490
|
+
|
|
491
|
+
def self.apply_v2_flag(flags, components, force: false)
|
|
492
|
+
OnyxCord::MessageComponents.apply_v2_flag(flags, components, force: force)
|
|
493
|
+
end
|
|
494
|
+
|
|
495
|
+
# Add a row component to the view.
|
|
496
|
+
# @see RowBuilder#initialize
|
|
497
|
+
def row(...)
|
|
498
|
+
@components << RowBuilder.new(...)
|
|
499
|
+
end
|
|
500
|
+
|
|
501
|
+
# Add a file component to the view.
|
|
502
|
+
# @see FileBuilder#initialize
|
|
503
|
+
def file(...)
|
|
504
|
+
@components << FileBuilder.new(...)
|
|
505
|
+
end
|
|
506
|
+
|
|
507
|
+
alias_method :file_display, :file
|
|
508
|
+
|
|
509
|
+
# Add a section component to the view.
|
|
510
|
+
# @see SectionBuilder#initialize
|
|
511
|
+
def section(...)
|
|
512
|
+
@components << SectionBuilder.new(...)
|
|
513
|
+
end
|
|
514
|
+
|
|
515
|
+
# Add a separator component to the view.
|
|
516
|
+
# @see SeparatorBuilder#initialize
|
|
517
|
+
def separator(...)
|
|
518
|
+
@components << SeparatorBuilder.new(...)
|
|
519
|
+
end
|
|
520
|
+
|
|
521
|
+
# Add a container component to the view.
|
|
522
|
+
# @see ContainerBuilder#initialize
|
|
523
|
+
def container(...)
|
|
524
|
+
@components << ContainerBuilder.new(...)
|
|
525
|
+
end
|
|
526
|
+
|
|
527
|
+
# Add a text display component to the view.
|
|
528
|
+
# @see TextDisplayBuilder#initialize
|
|
529
|
+
def text_display(...)
|
|
530
|
+
@components << TextDisplayBuilder.new(...)
|
|
531
|
+
end
|
|
532
|
+
|
|
533
|
+
# Add a media gallery component to the view.
|
|
534
|
+
# @see MediaGalleryBuilder#initialize
|
|
535
|
+
def media_gallery(...)
|
|
536
|
+
@components << MediaGalleryBuilder.new(...)
|
|
537
|
+
end
|
|
538
|
+
end
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'onyxcord/webhooks/version'
|
|
4
|
+
require 'onyxcord/webhooks/embeds'
|
|
5
|
+
require 'onyxcord/webhooks/client'
|
|
6
|
+
require 'onyxcord/webhooks/builder'
|
|
7
|
+
require 'onyxcord/webhooks/view'
|
|
8
|
+
require 'onyxcord/webhooks/modal'
|
|
9
|
+
|
|
10
|
+
module OnyxCord
|
|
11
|
+
# Webhook client
|
|
12
|
+
module Webhooks
|
|
13
|
+
end
|
|
14
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: onyxcord-webhooks
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 1.1.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Gustavo Silva
|
|
8
|
+
bindir: exe
|
|
9
|
+
cert_chain: []
|
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
|
11
|
+
dependencies:
|
|
12
|
+
- !ruby/object:Gem::Dependency
|
|
13
|
+
name: rest-client
|
|
14
|
+
requirement: !ruby/object:Gem::Requirement
|
|
15
|
+
requirements:
|
|
16
|
+
- - ">="
|
|
17
|
+
- !ruby/object:Gem::Version
|
|
18
|
+
version: 2.0.0
|
|
19
|
+
type: :runtime
|
|
20
|
+
prerelease: false
|
|
21
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
22
|
+
requirements:
|
|
23
|
+
- - ">="
|
|
24
|
+
- !ruby/object:Gem::Version
|
|
25
|
+
version: 2.0.0
|
|
26
|
+
description: A webhook client for OnyxCord, a Ruby Discord library based on discordrb
|
|
27
|
+
and updated with Components V2 support.
|
|
28
|
+
email:
|
|
29
|
+
- gustavosilva8kt@gmail.com
|
|
30
|
+
executables: []
|
|
31
|
+
extensions: []
|
|
32
|
+
extra_rdoc_files: []
|
|
33
|
+
files:
|
|
34
|
+
- lib/onyxcord/webhooks.rb
|
|
35
|
+
- lib/onyxcord/webhooks/builder.rb
|
|
36
|
+
- lib/onyxcord/webhooks/client.rb
|
|
37
|
+
- lib/onyxcord/webhooks/embeds.rb
|
|
38
|
+
- lib/onyxcord/webhooks/modal.rb
|
|
39
|
+
- lib/onyxcord/webhooks/version.rb
|
|
40
|
+
- lib/onyxcord/webhooks/view.rb
|
|
41
|
+
homepage: https://github.com/kruldevb/OnyxCord
|
|
42
|
+
licenses:
|
|
43
|
+
- MIT
|
|
44
|
+
metadata:
|
|
45
|
+
bug_tracker_uri: https://github.com/kruldevb/OnyxCord/issues
|
|
46
|
+
documentation_uri: https://github.com/kruldevb/OnyxCord#readme
|
|
47
|
+
source_code_uri: https://github.com/kruldevb/OnyxCord
|
|
48
|
+
rubygems_mfa_required: 'true'
|
|
49
|
+
rdoc_options: []
|
|
50
|
+
require_paths:
|
|
51
|
+
- lib
|
|
52
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
53
|
+
requirements:
|
|
54
|
+
- - ">="
|
|
55
|
+
- !ruby/object:Gem::Version
|
|
56
|
+
version: '3.3'
|
|
57
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
58
|
+
requirements:
|
|
59
|
+
- - ">="
|
|
60
|
+
- !ruby/object:Gem::Version
|
|
61
|
+
version: '0'
|
|
62
|
+
requirements: []
|
|
63
|
+
rubygems_version: 3.6.9
|
|
64
|
+
specification_version: 4
|
|
65
|
+
summary: Webhook client for onyxcord
|
|
66
|
+
test_files: []
|