class Discorb::Button
Represents a button component.
Attributes
@!visibility private
@return [String] The custom ID of the button.
Won't be used if the style is `:link`.
@return [Boolean] Whether the button is disabled.
@return [Boolean] Whether the button is disabled.
@return [Discorb::Emoji] The emoji of the button.
@return [String] The label of the button.
@return [:primary, :secondary, :success, :danger, :link] The style of the button.
@return [String] The URL of the button.
Only used when the style is `:link`.
Public Class Methods
Initialize a new button.
@param [String] label The label of the button. @param [:primary, :secondary, :success, :danger, :link] style The style of the button. @param [Discorb::Emoji] emoji The emoji of the button. @param [String] custom_id
The custom ID of the button. @param [String] url The URL of the button. @param [Boolean] disabled Whether the button is disabled.
# File lib/discorb/components.rb, line 80 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
Public Instance Methods
Converts the button to a hash.
@see discord.com/developers/docs/interactions/message-components#button-object-button-structure Official Discord API docs @return [Hash] A hash representation of the button.
# File lib/discorb/components.rb, line 95 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
Private Instance Methods
# File lib/discorb/components.rb, line 124 def hash_emoji(emoji) case emoji when UnicodeEmoji { id: nil, name: emoji.to_s, animated: false, } when CustomEmoji { id: emoji.id, name: emoji.name, animated: emoji.animated?, } end end