Module: OnyxCord::MessagePayload
- Defined in:
- lib/onyxcord/internal/message_payload.rb
Constant Summary collapse
- MAX_ATTACHMENTS =
10- MAX_EMBEDS =
10- KEEP =
:keep
Class Method Summary collapse
- .attachment_payload(attachments) ⇒ Object
- .components_v2?(components, flags) ⇒ Boolean
- .edit_body(content, embeds) ⇒ Object
- .multipart_body(body, attachments) ⇒ Object
- .present?(value) ⇒ Boolean
- .uploads(attachments) ⇒ Object
- .validate!(content: nil, embeds: nil, components: nil, flags: nil, attachments: nil, poll: nil) ⇒ Object
- .validate_components_v2!(content, embeds, components, flags, poll) ⇒ Object
- .validate_limit!(name, values, max) ⇒ Object
Class Method Details
.attachment_payload(attachments) ⇒ Object
14 15 16 |
# File 'lib/onyxcord/internal/message_payload.rb', line 14 def () uploads().map.with_index { |upload, index| { id: index, filename: upload.filename } } end |
.components_v2?(components, flags) ⇒ Boolean
65 66 67 68 |
# File 'lib/onyxcord/internal/message_payload.rb', line 65 def components_v2?(components, flags) OnyxCord::MessageComponents.components_v2?(components) || OnyxCord::MessageComponents.flag_value(flags).anybits?(OnyxCord::MessageComponents::IS_COMPONENTS_V2) end |
.edit_body(content, embeds) ⇒ Object
37 38 39 40 41 42 43 44 45 46 |
# File 'lib/onyxcord/internal/message_payload.rb', line 37 def edit_body(content, ) content_keep = content == KEEP = == KEEP content_given = !content_keep && !content.nil? = ! && !.nil? body = {} body[:content] = content if !content_keep && (content_given || ) body[:embeds] = ? : [] if ! && ( || content_given) body end |
.multipart_body(body, attachments) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/onyxcord/internal/message_payload.rb', line 18 def multipart_body(body, ) parts = uploads().map.with_index do |upload, index| { name: "files[#{index}]", value: upload, filename: upload.filename, content_type: upload.content_type || 'application/octet-stream' } end parts << { name: 'payload_json', value: body.to_json } end |
.present?(value) ⇒ Boolean
70 71 72 73 74 75 76 |
# File 'lib/onyxcord/internal/message_payload.rb', line 70 def present?(value) return false if value.nil? return false if value == KEEP return !value.empty? if value.respond_to?(:empty?) true end |
.uploads(attachments) ⇒ Object
48 49 50 |
# File 'lib/onyxcord/internal/message_payload.rb', line 48 def uploads() Array().map { || OnyxCord::Upload.wrap() } end |
.validate!(content: nil, embeds: nil, components: nil, flags: nil, attachments: nil, poll: nil) ⇒ Object
31 32 33 34 35 |
# File 'lib/onyxcord/internal/message_payload.rb', line 31 def validate!(content: nil, embeds: nil, components: nil, flags: nil, attachments: nil, poll: nil) validate_limit!('attachments', , MAX_ATTACHMENTS) validate_limit!('embeds', , MAX_EMBEDS) validate_components_v2!(content, , components, flags, poll) end |
.validate_components_v2!(content, embeds, components, flags, poll) ⇒ Object
58 59 60 61 62 63 |
# File 'lib/onyxcord/internal/message_payload.rb', line 58 def validate_components_v2!(content, , components, flags, poll) return unless components_v2?(components, flags) return unless present?(content) || present?() || present?(poll) raise ArgumentError, 'Components V2 messages cannot include content, embeds, or poll' end |
.validate_limit!(name, values, max) ⇒ Object
52 53 54 55 56 |
# File 'lib/onyxcord/internal/message_payload.rb', line 52 def validate_limit!(name, values, max) return if values.nil? || Array(values).length <= max raise ArgumentError, "#{name} cannot exceed #{max} elements" end |