class Discorb::Button

Represents a button component.

Attributes

styles[R]

@!visibility private

custom_id[RW]

@return [String] The custom ID of the button.

Won't be used if the style is `:link`.
disabled[RW]

@return [Boolean] Whether the button is disabled.

disabled?[RW]

@return [Boolean] Whether the button is disabled.

emoji[RW]

@return [Discorb::Emoji] The emoji of the button.

label[RW]

@return [String] The label of the button.

style[RW]

@return [:primary, :secondary, :success, :danger, :link] The style of the button.

url[RW]

@return [String] The URL of the button.

Only used when the style is `:link`.

Public Class Methods

new(label, style = :primary, emoji: nil, custom_id: nil, url: nil, disabled: false) click to toggle source

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

to_hash() click to toggle source

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

hash_emoji(emoji) click to toggle source
# 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