Class: Discordrb::Webhooks::View::SectionBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/discordrb/webhooks/view.rb

Overview

A section allows you to group together an accessory with text display components.

Instance Method Summary collapse

Constructor Details

#initialize(id: nil) {|builder| ... } ⇒ SectionBuilder

Create a section component.

Parameters:

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

    The unique 32-bit ID of the section component.

Yield Parameters:

  • builder (SectionBuilder)

    Yields the initialized section component.



291
292
293
294
295
296
297
# File 'lib/discordrb/webhooks/view.rb', line 291

def initialize(id: nil)
  @id = id
  @accessory = nil
  @components = []

  yield self if block_given?
end

Instance Method Details

#button(style:, id: nil, label: nil, emoji: nil, custom_id: nil, disabled: nil, url: nil) ⇒ Object

Set the button for the section. This is mutually exclusive with #thumbnail. that responds to #to_h which returns a hash in the format of { id: Integer, name: string }. There is a limit of 100 characters to each custom_id.

Parameters:

  • style (Symbol, Integer)

    The button's style type. See BUTTON_STYLES

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

    The unique 32-bit ID of the button component. This is not to be confused with the custom_id.

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

    The text label for the button. Either a label or emoji must be provided.

  • emoji (#to_h, String, Integer) (defaults to: nil)

    An emoji ID, or unicode emoji to attach to the button. Can also be an object

  • custom_id (String) (defaults to: nil)

    Custom IDs are used to pass state to the events that are raised from interactions.

  • disabled (true, false) (defaults to: nil)

    Whether this button is disabled and shown as greyed out.

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

    The URL, when using a link style button.



324
325
326
327
328
329
330
331
332
333
334
335
# File 'lib/discordrb/webhooks/view.rb', line 324

def button(style:, id: nil, label: nil, emoji: nil, custom_id: nil, disabled: nil, url: nil)
  style = BUTTON_STYLES[style] || style

  emoji = case emoji
          when Integer, String
            emoji.to_i.positive? ? { id: emoji } : { name: emoji }
          else
            emoji&.to_h
          end

  @accessory = { type: COMPONENT_TYPES[:button], id: id, label: label, emoji: emoji, style: style, custom_id: custom_id, disabled: disabled, url: url }.compact
end

#text_displayObject

Add a text display component to this section.



301
302
303
# File 'lib/discordrb/webhooks/view.rb', line 301

def text_display(...)
  @components << TextDisplayBuilder.new(...)
end

#thumbnail(url:, id: nil, description: nil, spoiler: false) ⇒ Object

Set the thumbnail for the section. This is mutually exclusive with #button.

Parameters:

  • url (String)

    The URL to the thumbnail image.

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

    The unique 32-bit ID of the thumbnail component.

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

    The description of the thumbnail.

  • spoiler (true, false) (defaults to: false)

    Whether or not to apply a spoiler label to the thumbnail.



310
311
312
# File 'lib/discordrb/webhooks/view.rb', line 310

def thumbnail(url:, id: nil, description: nil, spoiler: false)
  @accessory = { type: COMPONENT_TYPES[:thumbnail], id: id, media: { url: }, description: description, spoiler: spoiler }.compact
end