Class: Rubord::SelectMenu

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

Overview

Represents a Discord select menu component.

Select menus are dropdown components that allow users to select one or multiple options from a list.

Examples:

Creating a single-select menu

menu = Rubord::SelectMenu.new(
  custom_id: "game_selection",
  placeholder: "Choose a game..."
)
menu.add_option(label: "Minecraft", value: "minecraft")
menu.add_option(label: "Terraria", value: "terraria")

Creating a multi-select menu

menu = Rubord::SelectMenu.new(
  custom_id: "hobbies",
  placeholder: "Select your hobbies",
  min_values: 1,
  max_values: 3
)

See Also:

Since:

  • 1.0.0

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(custom_id:, placeholder: nil, min_values: 1, max_values: 1, disabled: false) ⇒ SelectMenu

Creates a new select menu component.

Examples:

Basic select menu

SelectMenu.new(
  custom_id: "color_picker",
  placeholder: "Choose a color",
  min_values: 1,
  max_values: 1
)

Multi-select menu

SelectMenu.new(
  custom_id: "tags",
  placeholder: "Select up to 3 tags",
  min_values: 0,
  max_values: 3
)

Parameters:

  • custom_id (String)

    Developer-defined identifier.

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

    Placeholder text.

  • min_values (Integer) (defaults to: 1)

    Minimum selectable options. Defaults to 1.

  • max_values (Integer) (defaults to: 1)

    Maximum selectable options. Defaults to 1.

  • disabled (Boolean) (defaults to: false)

    Whether the menu is disabled. Defaults to false.

Since:

  • 1.0.0



77
78
79
80
81
82
83
84
85
# File 'lib/rubord/components/select_menu.rb', line 77

def initialize(custom_id:, placeholder: nil, min_values: 1, max_values: 1, disabled: false)
  @type = 3
  @custom_id = custom_id
  @options = []
  @placeholder = placeholder
  @min_values = min_values
  @max_values = max_values
  @disabled = disabled
end

Instance Attribute Details

#custom_idString

Returns Developer-defined identifier.

Returns:

  • (String)

    Developer-defined identifier.

Since:

  • 1.0.0



32
33
34
# File 'lib/rubord/components/select_menu.rb', line 32

def custom_id
  @custom_id
end

#disabledBoolean

Returns Whether the select menu is disabled.

Returns:

  • (Boolean)

    Whether the select menu is disabled.

Since:

  • 1.0.0



49
50
51
# File 'lib/rubord/components/select_menu.rb', line 49

def disabled
  @disabled
end

#max_valuesInteger

Returns Maximum number of options that can be selected. Defaults to 1.

Returns:

  • (Integer)

    Maximum number of options that can be selected. Defaults to 1.

Since:

  • 1.0.0



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

def max_values
  @max_values
end

#min_valuesInteger

Returns Minimum number of options that must be selected. Defaults to 1.

Returns:

  • (Integer)

    Minimum number of options that must be selected. Defaults to 1.

Since:

  • 1.0.0



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

def min_values
  @min_values
end

#optionsArray<Hash>

Returns List of available options.

Returns:

  • (Array<Hash>)

    List of available options.

Since:

  • 1.0.0



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

def options
  @options
end

#placeholderString?

Returns Placeholder text displayed when no option is selected.

Returns:

  • (String, nil)

    Placeholder text displayed when no option is selected.

Since:

  • 1.0.0



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

def placeholder
  @placeholder
end

#typeInteger

Returns Component type (always 3 for select menus).

Returns:

  • (Integer)

    Component type (always 3 for select menus).

Since:

  • 1.0.0



29
30
31
# File 'lib/rubord/components/select_menu.rb', line 29

def type
  @type
end

Instance Method Details

#add_option(label:, value:, description: nil, emoji: nil, default: false) ⇒ Rubord::SelectMenu

Adds an option to the select menu.

Examples:

Adding an option with description

menu.add_option(
  label: "Ruby",
  value: "ruby_lang",
  description: "Dynamic, object-oriented programming language",
  emoji: { name: "💎" }
)

Parameters:

  • label (String)

    The user-facing name of the option (max 100 chars).

  • value (String)

    The developer-defined value of the option (max 100 chars).

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

    Additional description (max 100 chars).

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

    Emoji to display with the option.

  • default (Boolean) (defaults to: false)

    Whether this option is selected by default. Defaults to false.

Returns:

Since:

  • 1.0.0



105
106
107
108
109
110
111
112
113
114
# File 'lib/rubord/components/select_menu.rb', line 105

def add_option(label:, value:, description: nil, emoji: nil, default: false)
  @options << {
    label: label,
    value: value,
    description: description,
    emoji: emoji,
    default: default
  }.compact
  self
end

#to_hHash

Converts the select menu to a Discord API-compatible hash.

Examples:

menu = SelectMenu.new(custom_id: "test")
menu.to_h
# => {type: 3, custom_id: "test", options: [], min_values: 1, max_values: 1, disabled: false}

Returns:

  • (Hash)

    Select menu data in Discord API format.

Since:

  • 1.0.0



124
125
126
127
128
129
130
131
132
133
134
# File 'lib/rubord/components/select_menu.rb', line 124

def to_h
  {
    type: @type,
    custom_id: @custom_id,
    options: @options,
    placeholder: @placeholder,
    min_values: @min_values,
    max_values: @max_values,
    disabled: @disabled
  }.compact
end