Class: Rubycord::Webhooks::Embed

Inherits:
Object
  • Object
show all
Defined in:
lib/rubycord/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/rubycord/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 = Rubycord::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:



81
82
83
# File 'lib/rubycord/webhooks/embeds.rb', line 81

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/rubycord/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/rubycord/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.



101
102
103
# File 'lib/rubycord/webhooks/embeds.rb', line 101

def fields
  @fields
end

Returns footer for this embed.

Examples:

Add a footer to an embed

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

Returns:



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

def footer
  @footer
end

#imageEmbedImage?

Returns image for this embed.

Examples:

Add a image to an embed

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

Returns:

See Also:



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

def image
  @image
end

#thumbnailEmbedThumbnail?

Returns thumbnail for this embed.

Examples:

Add a thumbnail to an embed

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

Returns:

See Also:



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

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/rubycord/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/rubycord/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/rubycord/webhooks/embeds.rb', line 28

def url
  @url
end

Instance Method Details

#<<(field) ⇒ Object

Add a field object to this embed.

Parameters:



85
86
87
# File 'lib/rubycord/webhooks/embeds.rb', line 85

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:



96
97
98
# File 'lib/rubycord/webhooks/embeds.rb', line 96

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.



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

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