Class: Discorb::Button

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

Overview

Represents a button component.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Component

from_hash, 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.



112
113
114
115
116
117
118
119
# File 'lib/discorb/components.rb', line 112

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.



86
87
88
# File 'lib/discorb/components.rb', line 86

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.



91
92
93
# File 'lib/discorb/components.rb', line 91

def disabled
  @disabled
end

#emoji -> Discorb::Emoji

Returns The emoji of the button.

Returns:



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

def emoji
  @emoji
end

#label -> String

Returns The label of the button.

Returns:

  • (String)

    The label of the button.



79
80
81
# File 'lib/discorb/components.rb', line 79

def label
  @label
end

#style -> :primary, ...

Returns The style of the button.

Returns:

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

    The style of the button.



81
82
83
# File 'lib/discorb/components.rb', line 81

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.



89
90
91
# File 'lib/discorb/components.rb', line 89

def url
  @url
end

Instance Method Details

#inspect -> Object



149
150
151
# File 'lib/discorb/components.rb', line 149

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:



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/discorb/components.rb', line 127

def to_hash
  if @style == :link
    {
      type: 2,
      label: @label,
      style: self.class.styles[@style],
      url: @url,
      emoji: hash_emoji(@emoji),
      disabled: @disabled,
    }
  else
    {
      type: 2,
      label: @label,
      style: self.class.styles[@style],
      custom_id: @custom_id,
      emoji: hash_emoji(@emoji),
      disabled: @disabled,
    }
  end
end