Class: DiscordRDA::MessageBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/discord_rda/entity/message_builder.rb

Overview

Builder for creating message payloads with a DSL. Supports content, embeds, components, and other message options.

Instance Method Summary collapse

Constructor Details

#initialize(base = {}) ⇒ MessageBuilder

Initialize with base payload

Parameters:

  • base (Hash) (defaults to: {})

    Base payload hash



10
11
12
# File 'lib/discord_rda/entity/message_builder.rb', line 10

def initialize(base = {})
  @payload = base
end

Instance Method Details

#allowed_mentions(parse: nil, roles: nil, users: nil, replied_user: nil) ⇒ self

Set allowed mentions for the message

Parameters:

  • parse (Array<String>) (defaults to: nil)

    Parse types (roles, users, everyone)

  • roles (Array<String>) (defaults to: nil)

    Specific role IDs to mention

  • users (Array<String>) (defaults to: nil)

    Specific user IDs to mention

  • replied_user (Boolean) (defaults to: nil)

    Whether to mention the replied user

Returns:

  • (self)


78
79
80
81
82
83
84
85
# File 'lib/discord_rda/entity/message_builder.rb', line 78

def allowed_mentions(parse: nil, roles: nil, users: nil, replied_user: nil)
  @payload[:allowed_mentions] = {}
  @payload[:allowed_mentions][:parse] = parse if parse
  @payload[:allowed_mentions][:roles] = roles if roles
  @payload[:allowed_mentions][:users] = users if users
  @payload[:allowed_mentions][:replied_user] = replied_user unless replied_user.nil?
  self
end

#attachment(filename:, description: nil) ⇒ self

Add attachments (requires multipart/form-data, not supported in basic builder)

Parameters:

  • filename (String)

    Attachment filename

  • description (String) (defaults to: nil)

    Attachment description

Returns:

  • (self)


91
92
93
94
95
# File 'lib/discord_rda/entity/message_builder.rb', line 91

def attachment(filename:, description: nil)
  @payload[:attachments] ||= []
  @payload[:attachments] << { filename: filename, description: description }.compact
  self
end

#components(type: 1) {|ComponentBuilder| ... } ⇒ self

Add components (buttons, select menus) to the message

Parameters:

  • type (Integer) (defaults to: 1)

    Component type (1 for action row)

Yields:

Returns:

  • (self)


50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/discord_rda/entity/message_builder.rb', line 50

def components(type: 1, &block)
  @payload[:components] ||= []

  row = { type: type, components: [] }

  if block
    builder = ComponentBuilder.new(row[:components])
    block.call(builder)
  end

  @payload[:components] << row unless row[:components].empty?
  self
end

#content(text) ⇒ self

Set message content

Parameters:

  • text (String)

    Message text

Returns:

  • (self)


17
18
19
20
# File 'lib/discord_rda/entity/message_builder.rb', line 17

def content(text)
  @payload[:content] = text
  self
end

#embed(title: nil, description: nil, color: nil) {|EmbedBuilder| ... } ⇒ self

Add an embed to the message

Parameters:

  • title (String) (defaults to: nil)

    Embed title

  • description (String) (defaults to: nil)

    Embed description

  • color (Integer, Color) (defaults to: nil)

    Embed color

Yields:

Returns:

  • (self)


28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/discord_rda/entity/message_builder.rb', line 28

def embed(title: nil, description: nil, color: nil, &block)
  @payload[:embeds] ||= []

  embed_hash = {}
  embed_hash[:title] = title if title
  embed_hash[:description] = description if description
  embed_hash[:color] = color.is_a?(Color) ? color.to_i : color if color

  if block
    builder = EmbedBuilder.new(embed_hash)
    block.call(builder)
    embed_hash = builder.to_h
  end

  @payload[:embeds] << embed_hash
  self
end

#flags(flags) ⇒ self

Set message flags

Parameters:

  • flags (Integer)

    Message flags

Returns:

  • (self)


100
101
102
103
# File 'lib/discord_rda/entity/message_builder.rb', line 100

def flags(flags)
  @payload[:flags] = flags
  self
end

#to_hHash

Convert builder to hash payload

Returns:

  • (Hash)

    Message payload



107
108
109
# File 'lib/discord_rda/entity/message_builder.rb', line 107

def to_h
  @payload
end

#tts(enabled = true) ⇒ self

Set whether this is a TTS message

Parameters:

  • enabled (Boolean) (defaults to: true)

    TTS enabled

Returns:

  • (self)


67
68
69
70
# File 'lib/discord_rda/entity/message_builder.rb', line 67

def tts(enabled = true)
  @payload[:tts] = enabled
  self
end