embed.rb 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. # frozen_string_literal: true
  2. require_relative 'concerns/serializable'
  3. class Embed
  4. attr_accessor :title,
  5. :description,
  6. :url,
  7. :color,
  8. :timestamp,
  9. :footer,
  10. :thumbnail,
  11. :image,
  12. :author,
  13. :fields
  14. include Serializable
  15. class Footer
  16. attr_accessor :icon_url, :text
  17. include Serializable
  18. def to_hash
  19. {
  20. icon_url: icon_url,
  21. text: text
  22. }
  23. end
  24. end
  25. class Image
  26. attr_accessor :url
  27. include Serializable
  28. def to_hash
  29. {
  30. url: url
  31. }
  32. end
  33. end
  34. class Author
  35. attr_accessor :name, :url, :icon_url
  36. include Serializable
  37. def to_hash
  38. {
  39. name: name,
  40. url: url,
  41. icon_url: icon_url
  42. }
  43. end
  44. end
  45. class Field
  46. attr_accessor :name, :value, :inline
  47. include Serializable
  48. def to_hash
  49. {
  50. name: name,
  51. value: value,
  52. inline: inline
  53. }
  54. end
  55. end
  56. def to_hash
  57. hash = {
  58. title: title,
  59. description: description,
  60. url: url,
  61. color: int_color,
  62. timestamp: timestamp
  63. }
  64. hash[:footer] = footer if footer
  65. hash[:thumbnail] = thumbnail if thumbnail
  66. hash[:image] = image if image
  67. hash[:author] = author if author
  68. hash[:fields] = fields if fields
  69. hash
  70. end
  71. def int_color
  72. return nil unless color
  73. hex = color.to_s.sub(/\A#/, '0x')
  74. Integer(hex)
  75. end
  76. def self.convert(discord_embed)
  77. fields = []
  78. discord_embed.fields.each do |field|
  79. fields.push({ name: field.name, value: field.value })
  80. end
  81. embed = Embed.new(
  82. title: discord_embed.title,
  83. description: discord_embed.description,
  84. url: discord_embed.url,
  85. color: discord_embed.color&.combined,
  86. timestamp: discord_embed.timestamp
  87. )
  88. embed.footer = {
  89. icon_url: discord_embed.footer.icon_url,
  90. text: discord_embed.footer.text
  91. } if discord_embed.footer
  92. embed.thumbnail = { url: discord_embed.thumbnail.url } if discord_embed.thumbnail
  93. embed.image = { url: discord_embed.image.url } if discord_embed.image
  94. embed.author = {
  95. icon_url: discord_embed.author.icon_url,
  96. name: discord_embed.author.name,
  97. url: discord_embed.author.url,
  98. } if discord_embed.author
  99. embed.fields = fields if fields
  100. embed
  101. end
  102. end