Class: Discordrb::Webhooks::Embed

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

Overview

An embed is a multipart-style attachment to a webhook message that can have a variety of different purposes and appearances.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(title: nil, description: nil, url: nil, timestamp: nil, colour: nil, color: nil, footer: nil, image: nil, thumbnail: nil, video: nil, provider: nil, author: nil, fields: []) ⇒ Embed

Returns a new instance of Embed.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/discordrb/webhooks/embeds.rb', line 7

def initialize(title: nil, description: nil, url: nil, timestamp: nil, colour: nil, color: nil, footer: nil,
               image: nil, thumbnail: nil, video: nil, provider: nil, author: nil, fields: [])
  @title = title
  @description = description
  @url = url
  @timestamp = timestamp
  self.colour = colour || color
  @footer = footer
  @image = image
  @thumbnail = thumbnail
  @video = video
  @provider = provider
  @author = author
  @fields = fields
end

Instance Attribute Details

#authorEmbedAuthor?

Returns author for this embed.

Examples:

Add a author to an embed

embed.author = Discordrb::Webhooks::EmbedAuthor.new(name: 'meew0', url: 'https://github.com/meew0', icon_url: 'https://avatars2.githubusercontent.com/u/3662915?v=3&s=466')

Returns:

See Also:



83
84
85
# File 'lib/discordrb/webhooks/embeds.rb', line 83

def author
  @author
end

#colourInteger? Also known as: color

Returns the colour of the bar to the side, in decimal form.

Returns:

  • (Integer, nil)

    the colour of the bar to the side, in decimal form



36
37
38
# File 'lib/discordrb/webhooks/embeds.rb', line 36

def colour
  @colour
end

#descriptionString?

Returns description for this embed.

Returns:

  • (String, nil)

    description for this embed



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

def description
  @description
end

#fieldsArray<EmbedField>

Returns the fields attached to this embed.

Returns:

  • (Array<EmbedField>)

    the fields attached to this embed.



103
104
105
# File 'lib/discordrb/webhooks/embeds.rb', line 103

def fields
  @fields
end

Returns footer for this embed.

Examples:

Add a footer to an embed

embed.footer = Discordrb::Webhooks::EmbedFooter.new(text: 'Hello', icon_url: 'https://i.imgur.com/j69wMDu.jpg')

Returns:



65
66
67
# File 'lib/discordrb/webhooks/embeds.rb', line 65

def footer
  @footer
end

#imageEmbedImage?

Returns image for this embed.

Examples:

Add a image to an embed

embed.image = Discordrb::Webhooks::EmbedImage.new(url: 'https://i.imgur.com/PcMltU7.jpg')

Returns:

See Also:



71
72
73
# File 'lib/discordrb/webhooks/embeds.rb', line 71

def image
  @image
end

#thumbnailEmbedThumbnail?

Returns thumbnail for this embed.

Examples:

Add a thumbnail to an embed

embed.thumbnail = Discordrb::Webhooks::EmbedThumbnail.new(url: 'https://i.imgur.com/xTG3a1I.jpg')

Returns:

See Also:



77
78
79
# File 'lib/discordrb/webhooks/embeds.rb', line 77

def thumbnail
  @thumbnail
end

#timestampTime?

Returns timestamp for this embed. Will be displayed just below the title.

Returns:

  • (Time, nil)

    timestamp for this embed. Will be displayed just below the title.



33
34
35
# File 'lib/discordrb/webhooks/embeds.rb', line 33

def timestamp
  @timestamp
end

#titleString?

Returns title of the embed that will be displayed above everything else.

Returns:

  • (String, nil)

    title of the embed that will be displayed above everything else.



24
25
26
# File 'lib/discordrb/webhooks/embeds.rb', line 24

def title
  @title
end

#urlString?

Returns URL the title should point to.

Returns:

  • (String, nil)

    URL the title should point to



30
31
32
# File 'lib/discordrb/webhooks/embeds.rb', line 30

def url
  @url
end

Instance Method Details

#<<(field) ⇒ Object

Add a field object to this embed.

Parameters:



87
88
89
# File 'lib/discordrb/webhooks/embeds.rb', line 87

def <<(field)
  @fields << field
end

#add_field(name: nil, value: nil, inline: nil) ⇒ Object

Convenience method to add a field to the embed without having to create one manually.

Examples:

Add a field to an embed, conveniently

embed.add_field(name: 'A field', value: "The field's content")

Parameters:

  • name (String) (defaults to: nil)

    The field’s name

  • value (String) (defaults to: nil)

    The field’s value

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

    Whether the field should be inline

See Also:



98
99
100
# File 'lib/discordrb/webhooks/embeds.rb', line 98

def add_field(name: nil, value: nil, inline: nil)
  self << EmbedField.new(name: name, value: value, inline: inline)
end

#to_hashHash

Returns a hash representation of this embed, to be converted to JSON.

Returns:

  • (Hash)

    a hash representation of this embed, to be converted to JSON.



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/discordrb/webhooks/embeds.rb', line 106

def to_hash
  {
    title: @title,
    description: @description,
    url: @url,
    timestamp: @timestamp&.utc&.iso8601,
    color: @colour,
    footer: @footer&.to_hash,
    image: @image&.to_hash,
    thumbnail: @thumbnail&.to_hash,
    video: @video&.to_hash,
    provider: @provider&.to_hash,
    author: @author&.to_hash,
    fields: @fields.map(&:to_hash)
  }
end