class Discordrb::Webhooks::Embed

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

Attributes

author[RW]

@see EmbedAuthor @example 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')

@return [EmbedAuthor, nil] author for this embed

color[R]

@return [Integer, nil] the colour of the bar to the side, in decimal form

colour[R]

@return [Integer, nil] the colour of the bar to the side, in decimal form

description[RW]

@return [String, nil] description for this embed

fields[RW]

@return [Array<EmbedField>] the fields attached to this embed.

image[RW]

@see EmbedImage @example Add a image to an embed

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

@return [EmbedImage, nil] image for this embed

thumbnail[RW]

@see EmbedThumbnail @example Add a thumbnail to an embed

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

@return [EmbedThumbnail, nil] thumbnail for this embed

timestamp[RW]

@return [Time, nil] timestamp for this embed. Will be displayed just below the title.

title[RW]

@return [String, nil] title of the embed that will be displayed above everything else.

url[RW]

@return [String, nil] URL the title should point to

Public Class Methods

new(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: []) click to toggle source
# 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

Public Instance Methods

<<(field) click to toggle source

Add a field object to this embed. @param field [EmbedField] The field to add.

# File lib/discordrb/webhooks/embeds.rb, line 87
def <<(field)
  @fields << field
end
add_field(name: nil, value: nil, inline: nil) click to toggle source

Convenience method to add a field to the embed without having to create one manually. @see EmbedField @example Add a field to an embed, conveniently

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

@param name [String] The field's name @param value [String] The field's value @param inline [true, false] Whether the field should be inlined

# 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
color=(value)
Alias for: colour=
colour=(value) click to toggle source

Sets the colour of the bar to the side of the embed to something new. @param value [Integer, String, {Integer, Integer, Integer}, to_i, nil] The colour in decimal, hexadecimal, R/G/B decimal, or nil to clear the embeds colour

form.
# File lib/discordrb/webhooks/embeds.rb, line 42
def colour=(value)
  if value.nil?
    @colour = nil
  elsif value.is_a? Integer
    raise ArgumentError, 'Embed colour must be 24-bit!' if value >= 16_777_216

    @colour = value
  elsif value.is_a? String
    self.colour = value.delete('#').to_i(16)
  elsif value.is_a? Array
    raise ArgumentError, 'Colour tuple must have three values!' if value.length != 3

    self.colour = value[0] << 16 | value[1] << 8 | value[2]
  else
    self.colour = value.to_i
  end
end
Also aliased as: color=
to_hash() click to toggle source

@return [Hash] a hash representation of this embed, to be converted to JSON.

# 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