Class: RubyCord::Component::Button

Inherits:
RubyCord::Component show all
Defined in:
lib/rubycord/component/button.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from RubyCord::Component

to_payload

Constructor Details

#initialize(label, style = :primary, emoji: nil, custom_id: nil, url: nil, disabled: false) ⇒ Button

Initialize a new button.

Parameters:

  • label (String)

    The label of the button.

  • style (:primary, :secondary, :success, :danger, :link) (defaults to: :primary)

    The style of the button.

  • emoji (RubyCord::Emoji) (defaults to: nil)

    The emoji of the button.

  • custom_id (String) (defaults to: nil)

    The custom ID of the button.

  • url (String) (defaults to: nil)

    The URL of the button.

  • disabled (Boolean) (defaults to: false)

    Whether the button is disabled.



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/rubycord/component/button.rb', line 39

def initialize(
  label,
  style = :primary,
  emoji: nil,
  custom_id: nil,
  url: nil,
  disabled: false
)
  @label = label
  @style = style
  @emoji = emoji
  @custom_id = custom_id
  @url = url
  @disabled = disabled
end

Instance Attribute Details

#custom_idString

Returns The custom ID of the button. Won't be used if the style is :link.

Returns:

  • (String)

    The custom ID of the button. Won't be used if the style is :link.



17
18
19
# File 'lib/rubycord/component/button.rb', line 17

def custom_id
  @custom_id
end

#disabledBoolean Also known as: disabled?

Returns Whether the button is disabled.

Returns:

  • (Boolean)

    Whether the button is disabled.



22
23
24
# File 'lib/rubycord/component/button.rb', line 22

def disabled
  @disabled
end

#emojiRubyCord::Emoji

Returns The emoji of the button.

Returns:



14
15
16
# File 'lib/rubycord/component/button.rb', line 14

def emoji
  @emoji
end

#labelString

Returns The label of the button.

Returns:

  • (String)

    The label of the button.



10
11
12
# File 'lib/rubycord/component/button.rb', line 10

def label
  @label
end

#style:primary, ...

Returns The style of the button.

Returns:

  • (:primary, :secondary, :success, :danger, :link)

    The style of the button.



12
13
14
# File 'lib/rubycord/component/button.rb', line 12

def style
  @style
end

#urlString

Returns The URL of the button. Only used when the style is :link.

Returns:

  • (String)

    The URL of the button. Only used when the style is :link.



20
21
22
# File 'lib/rubycord/component/button.rb', line 20

def url
  @url
end

Class Method Details

.from_hash(data) ⇒ RubyCord::Component::Button

Creates a new button from a hash.

Parameters:

  • data (Hash)

    The hash to create the button from.

Returns:



97
98
99
100
101
102
103
104
105
106
# File 'lib/rubycord/component/button.rb', line 97

def from_hash(data)
  new(
    data[:label],
    data[:style],
    emoji: data[:emoji],
    custom_id: data[:custom_id],
    url: data[:url],
    disabled: data[:disabled]
  )
end

Instance Method Details

#inspectString

Returns Object class and attributes.

Returns:

  • (String)

    Object class and attributes.



85
86
87
# File 'lib/rubycord/component/button.rb', line 85

def inspect
  "#<#{self.class}: #{@custom_id || @url}>"
end

#to_hashHash

Converts the button to a hash.

Returns:

  • (Hash)

    A hash representation of the button.

See Also:



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/rubycord/component/button.rb', line 62

def to_hash
  if @style == :link
    {
      type: 2,
      label: @label,
      style: STYLES[@style],
      url: @url,
      emoji: @emoji&.to_hash,
      disabled: @disabled
    }
  else
    {
      type: 2,
      label: @label,
      style: STYLES[@style],
      custom_id: @custom_id,
      emoji: @emoji&.to_hash,
      disabled: @disabled
    }
  end
end