Class: Rubord::Button

Inherits:
Object
  • Object
show all
Defined in:
lib/rubord/components/button.rb

Overview

Represents a Discord interactive button component.

Buttons are interactive elements that users can click to trigger actions. They can be used in messages, modals, and action rows.

Examples:

Creating a primary button

button = Rubord::Button.new(
  label: "Click Me",
  style: 1,
  custom_id: "my_button"
)

Creating a link button

link_button = Rubord::Button.new(
  label: "Visit Website",
  style: 5,
  url: "https://example.com"
)

See Also:

Since:

  • 1.0.0

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Creates a new button component.

Examples:

Primary button with emoji

Button.new(
  label: "Submit",
  style: 1,
  custom_id: "submit_btn",
  emoji: { name: "" }
)

Disabled danger button

Button.new(
  label: "Delete",
  style: 4,
  custom_id: "delete_btn",
  disabled: true
)

Parameters:

  • label (String) (defaults to: nil)

    Text label displayed on the button.

  • style (Integer) (defaults to: 1)

    Button style (1-5). Defaults to 1 (primary).

  • custom_id (String, nil) (defaults to: nil)

    Developer-defined identifier. Required for non-link buttons.

  • url (String, nil) (defaults to: nil)

    URL for link buttons (style 5).

  • disabled (Boolean) (defaults to: false)

    Whether the button is disabled. Defaults to false.

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

    Emoji to display on the button.

Raises:

  • (ArgumentError)

    If required parameters are missing.

Since:

  • 1.0.0



83
84
85
86
87
88
89
90
91
# File 'lib/rubord/components/button.rb', line 83

def initialize(label: nil, style: 1, custom_id: nil, url: nil, disabled: false, emoji: nil)
  @type = 2
  @style = style
  @label = label
  @custom_id = custom_id
  @url = url
  @disabled = disabled
  @emoji = emoji
end

Instance Attribute Details

#custom_idString?

Returns Developer-defined identifier for the button. Required for non-link buttons, must be unique per message.

Returns:

  • (String, nil)

    Developer-defined identifier for the button. Required for non-link buttons, must be unique per message.

Since:

  • 1.0.0



42
43
44
# File 'lib/rubord/components/button.rb', line 42

def custom_id
  @custom_id
end

#disabledBoolean

Returns Whether the button is disabled.

Returns:

  • (Boolean)

    Whether the button is disabled.

Since:

  • 1.0.0



48
49
50
# File 'lib/rubord/components/button.rb', line 48

def disabled
  @disabled
end

#emojiHash?

Returns Emoji to display on the button. Format: ‘“emoji_name”, id: “emoji_id”, animated: false`.

Returns:

  • (Hash, nil)

    Emoji to display on the button. Format: ‘“emoji_name”, id: “emoji_id”, animated: false`

Since:

  • 1.0.0



52
53
54
# File 'lib/rubord/components/button.rb', line 52

def emoji
  @emoji
end

#labelString

Returns Text displayed on the button (max 80 characters).

Returns:

  • (String)

    Text displayed on the button (max 80 characters).

Since:

  • 1.0.0



38
39
40
# File 'lib/rubord/components/button.rb', line 38

def label
  @label
end

#styleInteger

Returns Button style (1-5).

  • 1: Primary (blurple)

  • 2: Secondary (grey)

  • 3: Success (green)

  • 4: Danger (red)

  • 5: Link (grey, navigates to URL).

Returns:

  • (Integer)

    Button style (1-5).

    • 1: Primary (blurple)

    • 2: Secondary (grey)

    • 3: Success (green)

    • 4: Danger (red)

    • 5: Link (grey, navigates to URL)

Since:

  • 1.0.0



35
36
37
# File 'lib/rubord/components/button.rb', line 35

def style
  @style
end

#typeInteger

Returns Component type (always 2 for buttons).

Returns:

  • (Integer)

    Component type (always 2 for buttons).

Since:

  • 1.0.0



27
28
29
# File 'lib/rubord/components/button.rb', line 27

def type
  @type
end

#urlString?

Returns URL for link buttons (style 5).

Returns:

  • (String, nil)

    URL for link buttons (style 5).

Since:

  • 1.0.0



45
46
47
# File 'lib/rubord/components/button.rb', line 45

def url
  @url
end

Instance Method Details

#to_hHash

Converts the button to a Discord API-compatible hash.

Examples:

button = Button.new(label: "Test", custom_id: "test")
button.to_h
# => {type: 2, style: 1, label: "Test", custom_id: "test", disabled: false}

Returns:

  • (Hash)

    Button data in Discord API format.

Since:

  • 1.0.0



101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/rubord/components/button.rb', line 101

def to_h
  h = {
    type: @type,
    style: @style,
    label: @label,
    disabled: @disabled
  }
  h[:custom_id] = @custom_id if @custom_id
  h[:url] = @url if @url
  h[:emoji] = @emoji if @emoji
  h
end