Class: Discorb::Button

Inherits:
Component show all
Defined in:
lib/discorb/components/button.rb

Overview

Represents a button component.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from 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 (Discorb::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.



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

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_id -> String

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.



16
17
18
# File 'lib/discorb/components/button.rb', line 16

def custom_id
  @custom_id
end

#disabled -> Boolean Also known as: disabled?

Returns Whether the button is disabled.

Returns:

  • (Boolean)

    Whether the button is disabled.



21
22
23
# File 'lib/discorb/components/button.rb', line 21

def disabled
  @disabled
end

#emoji -> Discorb::Emoji

Returns The emoji of the button.

Returns:



13
14
15
# File 'lib/discorb/components/button.rb', line 13

def emoji
  @emoji
end

#label -> String

Returns The label of the button.

Returns:

  • (String)

    The label of the button.



9
10
11
# File 'lib/discorb/components/button.rb', line 9

def label
  @label
end

#style -> :primary, ...

Returns The style of the button.

Returns:

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

    The style of the button.



11
12
13
# File 'lib/discorb/components/button.rb', line 11

def style
  @style
end

#url -> String

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.



19
20
21
# File 'lib/discorb/components/button.rb', line 19

def url
  @url
end

Class Method Details

.from_hash(data) -> Discorb::Button

Creates a new button from a hash.

Parameters:

  • data (Hash)

    The hash to create the button from.

Returns:



95
96
97
98
99
100
101
102
103
104
# File 'lib/discorb/components/button.rb', line 95

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

#inspect -> Object



83
84
85
# File 'lib/discorb/components/button.rb', line 83

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

#to_hash -> Hash

Converts the button to a hash.

Returns:

  • (Hash)

    A hash representation of the button.

See Also:



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

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