| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121 |
- # frozen_string_literal: true
- require_relative 'concerns/serializable'
- class Embed
- attr_accessor :title,
- :description,
- :url,
- :color,
- :timestamp,
- :footer,
- :thumbnail,
- :image,
- :author,
- :fields
- include Serializable
- class Footer
- attr_accessor :icon_url, :text
- include Serializable
- def to_hash
- {
- icon_url: icon_url,
- text: text
- }
- end
- end
- class Image
- attr_accessor :url
- include Serializable
- def to_hash
- {
- url: url
- }
- end
- end
- class Author
- attr_accessor :name, :url, :icon_url
- include Serializable
- def to_hash
- {
- name: name,
- url: url,
- icon_url: icon_url
- }
- end
- end
- class Field
- attr_accessor :name, :value, :inline
- include Serializable
- def to_hash
- {
- name: name,
- value: value,
- inline: inline
- }
- end
- end
- def to_hash
- hash = {
- title: title,
- description: description,
- url: url,
- color: int_color,
- timestamp: timestamp
- }
- hash[:footer] = footer if footer
- hash[:thumbnail] = thumbnail if thumbnail
- hash[:image] = image if image
- hash[:author] = author if author
- hash[:fields] = fields if fields
- hash
- end
- def int_color
- return nil unless color
- hex = color.to_s.sub(/\A#/, '0x')
- Integer(hex)
- end
- def self.convert(discord_embed)
- fields = []
- discord_embed.fields.each do |field|
- fields.push({ name: field.name, value: field.value })
- end
- embed = Embed.new(
- title: discord_embed.title,
- description: discord_embed.description,
- url: discord_embed.url,
- color: discord_embed.color&.combined,
- timestamp: discord_embed.timestamp
- )
- embed.footer = {
- icon_url: discord_embed.footer.icon_url,
- text: discord_embed.footer.text
- } if discord_embed.footer
- embed.thumbnail = { url: discord_embed.thumbnail.url } if discord_embed.thumbnail
- embed.image = { url: discord_embed.image.url } if discord_embed.image
- embed.author = {
- icon_url: discord_embed.author.icon_url,
- name: discord_embed.author.name,
- url: discord_embed.author.url,
- } if discord_embed.author
- embed.fields = fields if fields
- embed
- end
- end
|