Class: Rubord::ActionRow

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

Overview

Represents a Discord action row container.

Action rows are containers for message components (buttons, select menus). They can hold up to 5 buttons or 1 select menu per row.

Examples:

Creating an action row with buttons

row = Rubord::ActionRow.new(
  Rubord::Button.new(label: "Yes", custom_id: "yes"),
  Rubord::Button.new(label: "No", custom_id: "no")
)

Creating an empty row and adding components

row = Rubord::ActionRow.new
row.add(Rubord::Button.new(label: "Click", custom_id: "click"))

See Also:

Since:

  • 1.0.0

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*components) ⇒ ActionRow

Note:

Action rows can contain either:

  • Up to 5 buttons

  • OR 1 select menu

Not a mixture of both.

Creates a new action row.

Examples:

Empty action row

ActionRow.new

With initial buttons

ActionRow.new(
  Button.new(label: "Save", custom_id: "save"),
  Button.new(label: "Cancel", custom_id: "cancel")
)

Parameters:

  • components (Array<Button, SelectMenu>)

    Components to include in the row. Can be empty.

Since:

  • 1.0.0



46
47
48
49
# File 'lib/rubord/components/actionRow.rb', line 46

def initialize(*components)
  @type = 1
  @components = components
end

Instance Attribute Details

#componentsArray<Button, SelectMenu>

Returns Components in this action row.

Returns:

Since:

  • 1.0.0



26
27
28
# File 'lib/rubord/components/actionRow.rb', line 26

def components
  @components
end

#typeInteger

Returns Component type (always 1 for action rows).

Returns:

  • (Integer)

    Component type (always 1 for action rows).

Since:

  • 1.0.0



23
24
25
# File 'lib/rubord/components/actionRow.rb', line 23

def type
  @type
end

Instance Method Details

#add(component) ⇒ Rubord::ActionRow

Adds a component to the action row.

Examples:

row = ActionRow.new
row.add(Button.new(label: "Test", custom_id: "test"))

Parameters:

Returns:

Raises:

  • (ArgumentError)

    If adding component would violate Discord limits.

Since:

  • 1.0.0



62
63
64
65
# File 'lib/rubord/components/actionRow.rb', line 62

def add(component)
  @components << component
  self
end

#to_hHash

Converts the action row to a Discord API-compatible hash.

Examples:

row = ActionRow.new(Button.new(label: "Btn", custom_id: "btn"))
row.to_h
# => {type: 1, components: [{type: 2, style: 1, label: "Btn", custom_id: "btn", disabled: false}]}

Returns:

  • (Hash)

    Action row data in Discord API format.

Since:

  • 1.0.0



75
76
77
78
79
80
# File 'lib/rubord/components/actionRow.rb', line 75

def to_h
  {
    type: @type,
    components: @components.map(&:to_h)
  }
end