Class: Discorb::Component Abstract

Inherits:
Object
  • Object
show all
Defined in:
lib/discorb/components.rb

Overview

This class is abstract.

Represents a Discord component.

Direct Known Subclasses

Button, SelectMenu

Class Method Summary collapse

Class Method Details

.from_hash(data) -> Component

Create a new component from hash data.

Parameters:

  • data (Hash)

    Hash data.

Returns:

See Also:



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/discorb/components.rb', line 18

def from_hash(data)
  case data[:type]
  when 2
    Button.new(
      data[:label],
      data[:style],
      emoji: data[:emoji],
      custom_id: data[:custom_id],
      url: data[:url],
      disabled: data[:disabled],
    )
  when 3
    SelectMenu.new(
      data[:custom_id],
      data[:options].map { |o| SelectMenu::Option.from_hash(o) },
      placeholder: data[:placeholder],
      min_values: data[:min_values],
      max_values: data[:max_values],
    )
  end
end

.to_payload(components) -> Array<Hash>

Convert components to a hash.

Parameters:

Returns:

  • (Array<Hash>)

    Hash data.



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/discorb/components.rb', line 47

def to_payload(components)
  tmp_components = []
  tmp_row = []
  components.each do |c|
    case c
    when Array
      tmp_components << tmp_row
      tmp_row = []
      tmp_components << c
    when SelectMenu
      tmp_components << tmp_row
      tmp_row = []
      tmp_components << [c]
    else
      tmp_row << c
    end
  end
  tmp_components << tmp_row
  return tmp_components.filter { |c| c.length.positive? }.map { |c| { type: 1, components: c.map(&:to_hash) } }
end