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.


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

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:


75
76
77
# File 'lib/discordrb/webhooks/embeds.rb', line 75

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


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

def colour
  @colour
end

#descriptionString?

Returns description for this embed.

Returns:

  • (String, nil)

    description for this embed


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

def description
  @description
end

#fieldsArray<EmbedField>

Returns the fields attached to this embed.

Returns:

  • (Array<EmbedField>)

    the fields attached to this embed.


95
96
97
# File 'lib/discordrb/webhooks/embeds.rb', line 95

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:


57
58
59
# File 'lib/discordrb/webhooks/embeds.rb', line 57

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:


63
64
65
# File 'lib/discordrb/webhooks/embeds.rb', line 63

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:


69
70
71
# File 'lib/discordrb/webhooks/embeds.rb', line 69

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.


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

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.


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

def title
  @title
end

#urlString?

Returns URL the title should point to.

Returns:

  • (String, nil)

    URL the title should point to


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

def url
  @url
end

Instance Method Details

#<<(field) ⇒ Object

Add a field object to this embed.

Parameters:


79
80
81
# File 'lib/discordrb/webhooks/embeds.rb', line 79

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 inlined

See Also:


90
91
92
# File 'lib/discordrb/webhooks/embeds.rb', line 90

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.


98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/discordrb/webhooks/embeds.rb', line 98

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