Class: Rubord::Modal

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

Overview

Represents a Discord modal dialog component.

Modals are pop-up dialog windows that can contain text inputs. They’re triggered by interactions and allow for more complex user input.

Examples:

Creating a simple modal

modal = Rubord::Modal.new(
  custom_id: "feedback_form",
  title: "Submit Feedback"
)
modal.add_text_input(
  custom_id: "feedback",
  label: "Your feedback",
  style: 2,
  placeholder: "Type your feedback here..."
)

See Also:

Since:

  • 1.0.0

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(custom_id:, title:) ⇒ Modal

Creates a new modal dialog.

Examples:

Modal.new(
  custom_id: "user_registration",
  title: "Register Account"
)

Parameters:

  • custom_id (String)

    Developer-defined identifier.

  • title (String)

    Modal title (max 45 characters).

Since:

  • 1.0.0



46
47
48
49
50
51
# File 'lib/rubord/components/modal.rb', line 46

def initialize(custom_id:, title:)
  @type = 1
  @custom_id = custom_id
  @title = title
  @components = []
end

Instance Attribute Details

#componentsArray<Hash>

Returns List of text input components.

Returns:

  • (Array<Hash>)

    List of text input components.

Since:

  • 1.0.0



34
35
36
# File 'lib/rubord/components/modal.rb', line 34

def components
  @components
end

#custom_idString

Returns Developer-defined identifier.

Returns:

  • (String)

    Developer-defined identifier.

Since:

  • 1.0.0



28
29
30
# File 'lib/rubord/components/modal.rb', line 28

def custom_id
  @custom_id
end

#titleString

Returns Modal title (max 45 characters).

Returns:

  • (String)

    Modal title (max 45 characters).

Since:

  • 1.0.0



31
32
33
# File 'lib/rubord/components/modal.rb', line 31

def title
  @title
end

#typeInteger

Returns Component type (always 1 for modals).

Returns:

  • (Integer)

    Component type (always 1 for modals).

Since:

  • 1.0.0



25
26
27
# File 'lib/rubord/components/modal.rb', line 25

def type
  @type
end

Instance Method Details

#add_text_input(custom_id:, label:, style: 1, min_length: nil, max_length: nil, required: true, value: nil, placeholder: nil) ⇒ Rubord::Modal

Adds a text input field to the modal.

Examples:

Single-line required input

modal.add_text_input(
  custom_id: "username",
  label: "Username",
  required: true,
  min_length: 3,
  max_length: 20
)

Multi-line optional input

modal.add_text_input(
  custom_id: "bio",
  label: "Biography",
  style: 2,
  required: false,
  placeholder: "Tell us about yourself...",
  max_length: 500
)

Parameters:

  • custom_id (String)

    Developer-defined identifier for the input.

  • label (String)

    Label displayed above the input (max 45 characters).

  • style (Integer) (defaults to: 1)

    Input style (1=single-line, 2=multi-line). Defaults to 1.

  • min_length (Integer, nil) (defaults to: nil)

    Minimum input length.

  • max_length (Integer, nil) (defaults to: nil)

    Maximum input length.

  • required (Boolean) (defaults to: true)

    Whether the input is required. Defaults to true.

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

    Pre-filled value.

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

    Placeholder text.

Returns:

Since:

  • 1.0.0



86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/rubord/components/modal.rb', line 86

def add_text_input(custom_id:, label:, style: 1, min_length: nil, max_length: nil, required: true, value: nil, placeholder: nil)
  @components << {
    type: 4,
    custom_id: custom_id,
    style: style,
    label: label,
    min_length: min_length,
    max_length: max_length,
    required: required,
    value: value,
    placeholder: placeholder
  }.compact
  self
end

#to_hHash

Converts the modal to a Discord API-compatible hash.

Examples:

modal = Modal.new(custom_id: "test", title: "Test Modal")
modal.to_h
# => {type: 1, custom_id: "test", title: "Test Modal", components: [{type: 1, components: []}]}

Returns:

  • (Hash)

    Modal data in Discord API format.

Since:

  • 1.0.0



109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/rubord/components/modal.rb', line 109

def to_h
  {
    type: @type,
    custom_id: @custom_id,
    title: @title,
    components: [
      {
        type: 1,
        components: @components
      }
    ]
  }
end