Class: Discorb::Button
- Defined in:
- lib/discorb/components/button.rb
Overview
Represents a button component.
Instance Attribute Summary collapse
-
#custom_id -> String
The custom ID of the button.
-
#disabled -> Boolean
(also: #disabled?)
Whether the button is disabled.
-
#emoji -> Discorb::Emoji
The emoji of the button.
-
#label -> String
The label of the button.
-
#style -> :primary, ...
The style of the button.
-
#url -> String
The URL of the button.
Class Method Summary collapse
-
.from_hash(data) -> Discorb::Button
Creates a new button from a hash.
Instance Method Summary collapse
-
#initialize(label, style = :primary, emoji: nil, custom_id: nil, url: nil, disabled: false) -> Button
constructor
Initialize a new button.
- #inspect -> Object
-
#to_hash -> Hash
Converts the button to a hash.
Methods inherited from Component
Constructor Details
#initialize(label, style = :primary, emoji: nil, custom_id: nil, url: nil, disabled: false) -> Button
Initialize a new button.
44 45 46 47 48 49 50 51 |
# File 'lib/discorb/components/button.rb', line 44 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
.
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.
21 22 23 |
# File 'lib/discorb/components/button.rb', line 21 def disabled @disabled end |
#emoji -> Discorb::Emoji
Returns The emoji of the button.
13 14 15 |
# File 'lib/discorb/components/button.rb', line 13 def emoji @emoji end |
#label -> String
Returns 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.
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
.
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.
94 95 96 97 98 99 100 101 102 103 |
# File 'lib/discorb/components/button.rb', line 94 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
82 83 84 |
# File 'lib/discorb/components/button.rb', line 82 def inspect "#<#{self.class}: #{@custom_id || @url}>" end |
#to_hash -> Hash
Converts the button to a hash.
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/discorb/components/button.rb', line 60 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 |