Class: Discordrb::Webhooks::Builder

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

Overview

A class that acts as a builder for a webhook message object.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(content: '', username: nil, avatar_url: nil, tts: false, file: nil, embeds: []) ⇒ Builder

Returns a new instance of Builder.


6
7
8
9
10
11
12
13
# File 'lib/discordrb/webhooks/builder.rb', line 6

def initialize(content: '', username: nil, avatar_url: nil, tts: false, file: nil, embeds: [])
  @content = content
  @username = username
  @avatar_url = avatar_url
  @tts = tts
  @file = file
  @embeds = embeds
end

Instance Attribute Details

#avatar_urlString

The URL of an image file to be used as an avatar. If this is not set, the default avatar from the webhook's settings will be used instead.

Returns:

  • (String)

    the avatar URL.


27
28
29
# File 'lib/discordrb/webhooks/builder.rb', line 27

def avatar_url
  @avatar_url
end

#contentString

The content of the message. May be 2000 characters long at most.

Returns:

  • (String)

    the content of the message.


17
18
19
# File 'lib/discordrb/webhooks/builder.rb', line 17

def content
  @content
end

#embedsArray<Embed> (readonly)

Returns the embeds attached to this message.

Returns:

  • (Array<Embed>)

    the embeds attached to this message.


67
68
69
# File 'lib/discordrb/webhooks/builder.rb', line 67

def embeds
  @embeds
end

#fileFile?

Returns the file attached to this message.

Returns:

  • (File, nil)

    the file attached to this message.


64
65
66
# File 'lib/discordrb/webhooks/builder.rb', line 64

def file
  @file
end

#ttstrue, false

Whether this message should use TTS or not. By default, it doesn't.

Returns:

  • (true, false)

    the TTS status.


31
32
33
# File 'lib/discordrb/webhooks/builder.rb', line 31

def tts
  @tts
end

#usernameString

The username the webhook will display as. If this is not set, the default username set in the webhook's settings will be used instead.

Returns:


22
23
24
# File 'lib/discordrb/webhooks/builder.rb', line 22

def username
  @username
end

Instance Method Details

#<<(embed) ⇒ Object

Adds an embed to this message.

Parameters:

  • embed (Embed)

    The embed to add.

Raises:

  • (ArgumentError)

43
44
45
46
# File 'lib/discordrb/webhooks/builder.rb', line 43

def <<(embed)
  raise ArgumentError, 'Embeds and files are mutually exclusive!' if @file
  @embeds << embed
end

#add_embed(embed = nil) {|embed| ... } ⇒ Embed

Convenience method to add an embed using a block-style builder pattern

Examples:

Add an embed to a message

builder.add_embed do |embed|
  embed.title = 'Testing'
  embed.image = Discordrb::Webhooks::EmbedImage.new(url: 'https://i.imgur.com/PcMltU7.jpg')
end

Parameters:

  • embed (Embed, nil) (defaults to: nil)

    The embed to start the building process with, or nil if one should be created anew.

Yields:

  • (embed)

Returns:

  • (Embed)

    The created embed.


56
57
58
59
60
61
# File 'lib/discordrb/webhooks/builder.rb', line 56

def add_embed(embed = nil)
  embed ||= Embed.new
  yield(embed)
  self << embed
  embed
end

#to_json_hashHash

Returns a hash representation of the created message, for JSON format.

Returns:

  • (Hash)

    a hash representation of the created message, for JSON format.


70
71
72
73
74
75
76
77
78
# File 'lib/discordrb/webhooks/builder.rb', line 70

def to_json_hash
  {
    content: @content,
    username: @username,
    avatar_url: @avatar_url,
    tts: @tts,
    embeds: @embeds.map(&:to_hash)
  }
end

#to_multipart_hashHash

Returns a hash representation of the created message, for multipart format.

Returns:

  • (Hash)

    a hash representation of the created message, for multipart format.


81
82
83
84
85
86
87
88
89
# File 'lib/discordrb/webhooks/builder.rb', line 81

def to_multipart_hash
  {
    content: @content,
    username: @username,
    avatar_url: @avatar_url,
    tts: @tts,
    file: @file
  }
end