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

Class Method Details

.attachment_payload(attachments) ⇒ Object



14
15
16
# File 'lib/onyxcord/internal/message_payload.rb', line 14

def attachment_payload(attachments)
  uploads(attachments).map.with_index { |upload, index| { id: index, filename: upload.filename } }
end

.components_v2?(components, flags) ⇒ Boolean

Returns:

  • (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, embeds)
  content_keep = content == KEEP
  embeds_keep = embeds == KEEP
  content_given = !content_keep && !content.nil?
  embeds_given = !embeds_keep && !embeds.nil?
  body = {}
  body[:content] = content if !content_keep && (content_given || embeds_given)
  body[:embeds] = embeds_given ? embeds : [] if !embeds_keep && (embeds_given || 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, attachments)
  parts = uploads(attachments).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

Returns:

  • (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(attachments)
  Array(attachments).map { |attachment| OnyxCord::Upload.wrap(attachment) }
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', attachments, MAX_ATTACHMENTS)
  validate_limit!('embeds', embeds, MAX_EMBEDS)
  validate_components_v2!(content, embeds, components, flags, poll)
end

.validate_components_v2!(content, embeds, components, flags, poll) ⇒ Object

Raises:

  • (ArgumentError)


58
59
60
61
62
63
# File 'lib/onyxcord/internal/message_payload.rb', line 58

def validate_components_v2!(content, embeds, components, flags, poll)
  return unless components_v2?(components, flags)
  return unless present?(content) || present?(embeds) || present?(poll)

  raise ArgumentError, 'Components V2 messages cannot include content, embeds, or poll'
end

.validate_limit!(name, values, max) ⇒ Object

Raises:

  • (ArgumentError)


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