class Discorb::Embed

Represents an embed of discord.

Attributes

author[RW]

@return [Discorb::Embed::Author, nil] The author of embed.

color[RW]

@return [Discorb::Color, nil] The color of embed.

description[RW]

@return [String, nil] The description of embed.

fields[RW]

@return [Array<Discorb::Embed::Field>] The fields of embed.

image[R]
thumbnail[R]
timestamp[RW]

@return [Time, nil] The timestamp of embed.

title[RW]

@return [String, nil] The title of embed.

type[R]

@return [Symbol] The type of embed.

url[RW]

@return [String, nil] The url of embed.

Public Class Methods

new(title = nil, description = nil, color: nil, url: nil, timestamp: nil, author: nil, fields: nil, footer: nil, image: nil, thumbnail: nil, data: nil) click to toggle source

Initialize a new Embed object.

@param [String] title The title of embed. @param [String] description The description of embed. @param [Discorb::Color] color The color of embed. @param [String] url The url of embed. @param [Time] timestamp The timestamp of embed. @param [Discorb::Embed::Author] author The author field of embed. @param [Array<Discorb::Embed::Field>] fields The fields of embed. @param [Discorb::Embed::Footer] footer The footer of embed. @param [Discorb::Embed::Image, String] image The image of embed. @param [Discorb::Embed::Thumbnail, String] thumbnail The thumbnail of embed.

# File lib/discorb/embed.rb, line 47
def initialize(title = nil, description = nil, color: nil, url: nil, timestamp: nil, author: nil,
                                               fields: nil, footer: nil, image: nil, thumbnail: nil, data: nil)
  if data.nil?
    @title = title
    @description = description
    @url = url
    @timestamp = timestamp
    @color = color
    @author = author
    @fields = fields || []
    @footer = footer
    @image = image && (image.is_a?(String) ? Image.new(image) : image)
    @thumbnail = thumbnail && (thumbnail.is_a?(String) ? Thumbnail.new(thumbnail) : thumbnail)
    @type = "rich"
  else
    @title = data[:title]
    @description = data[:description]
    @url = data[:url]
    @timestamp = data[:timestamp] && Time.iso8601(data[:timestamp])
    @type = data[:type]
    @color = data[:color] && Color.new(data[:color])
    @footer = data[:footer] && Footer.new(data[:footer][:text], icon: data[:footer][:icon_url])
    @author = if data[:author]
        Author.new(data[:author][:name], icon: data[:author][:icon_url],
                                         url: data[:author][:url])
      end
    @thumbnail = data[:thumbnail] && Thumbnail.new(data[:thumbnail])
    @image = data[:image] && Image.new(data[:image])
    @video = data[:video] && Video.new(data[:video])
    @provider = data[:provider] && Provider.new(data[:provider])
    @fields = data[:fields] ? data[:fields].map { |f| Field.new(f[:name], f[:value], inline: f[:inline]) } : []
  end
end

Public Instance Methods

image=(value) click to toggle source
# File lib/discorb/embed.rb, line 81
def image=(value)
  @image = (value.is_a?(String)) ? Image.new(value) : value
end
thumbnail=(value) click to toggle source
# File lib/discorb/embed.rb, line 85
def thumbnail=(value)
  @thumbnail = (value.is_a?(String)) ? Thumbnail.new(value) : value
end
to_hash() click to toggle source

Convert embed to hash.

@see discord.com/developers/docs/resources/channel#embed-object-embed-structure Offical Discord API Docs @return [Hash] Converted embed.

# File lib/discorb/embed.rb, line 95
def to_hash
  {
    title: @title,
    description: @description,
    url: @url,
    timestamp: @timestamp&.iso8601,
    color: @color&.to_i,
    footer: @footer&.to_hash,
    image: @image&.to_hash,
    thumbnail: @thumbnail&.to_hash,
    author: @author&.to_hash,
    fields: @fields&.map { |f| f.to_hash },
  }
end