Class: DiscordRDA::Components::Button

Inherits:
Base
  • Object
show all
Defined in:
lib/discord_rda/interactions/components.rb

Overview

Button component

Instance Attribute Summary

Attributes inherited from Base

#data, #type

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#to_h

Constructor Details

#initialize(style:, label: nil, custom_id: nil, url: nil, emoji: nil, sku_id: nil, disabled: false) ⇒ Button

Returns a new instance of Button.

Parameters:

  • style (Symbol, Integer)

    Button style (:primary, :secondary, :success, :danger, :link)

  • label (String) (defaults to: nil)

    Button text

  • custom_id (String) (defaults to: nil)

    Custom ID for interaction (not for link buttons)

  • url (String) (defaults to: nil)

    URL for link buttons

  • emoji (Hash, String) (defaults to: nil)

    Emoji hash or unicode string

  • sku_id (String) (defaults to: nil)

    SKU ID for premium buttons

  • disabled (Boolean) (defaults to: false)

    Whether disabled



129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/discord_rda/interactions/components.rb', line 129

def initialize(style:, label: nil, custom_id: nil, url: nil, emoji: nil, sku_id: nil, disabled: false)
  style_val = BUTTON_STYLES[style] || style
  data = { style: style_val, disabled: disabled }

  data[:label] = label if label
  data[:custom_id] = custom_id if custom_id
  data[:url] = url if url && style_val == 5
  data[:emoji] = emoji.is_a?(String) ? { name: emoji } : emoji if emoji
  data[:sku_id] = sku_id if sku_id && style_val == 6

  super(:button, data)
end

Class Method Details

.danger(label:, custom_id:, **options) ⇒ Object

Create a danger (red) button

Parameters:

  • label (String)

    Button text

  • custom_id (String)

    Custom ID



166
167
168
# File 'lib/discord_rda/interactions/components.rb', line 166

def self.danger(label:, custom_id:, **options)
  new(style: :danger, label: label, custom_id: custom_id, **options)
end

Create a link button

Parameters:

  • label (String)

    Button text

  • url (String)

    URL to open



173
174
175
# File 'lib/discord_rda/interactions/components.rb', line 173

def self.link(label:, url:, **options)
  new(style: :link, label: label, url: url, **options)
end

.primary(label:, custom_id:, **options) ⇒ Object

Create a primary (blurple) button

Parameters:

  • label (String)

    Button text

  • custom_id (String)

    Custom ID



145
146
147
# File 'lib/discord_rda/interactions/components.rb', line 145

def self.primary(label:, custom_id:, **options)
  new(style: :primary, label: label, custom_id: custom_id, **options)
end

.secondary(label:, custom_id:, **options) ⇒ Object

Create a secondary (grey) button

Parameters:

  • label (String)

    Button text

  • custom_id (String)

    Custom ID



152
153
154
# File 'lib/discord_rda/interactions/components.rb', line 152

def self.secondary(label:, custom_id:, **options)
  new(style: :secondary, label: label, custom_id: custom_id, **options)
end

.success(label:, custom_id:, **options) ⇒ Object

Create a success (green) button

Parameters:

  • label (String)

    Button text

  • custom_id (String)

    Custom ID



159
160
161
# File 'lib/discord_rda/interactions/components.rb', line 159

def self.success(label:, custom_id:, **options)
  new(style: :success, label: label, custom_id: custom_id, **options)
end