character.rb 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. def character_embed(char:, img: nil, user: nil, color:, section: nil)
  2. fields = []
  3. user_name = case user
  4. when String
  5. user.capitalize
  6. when nil
  7. 'Unknown User'
  8. else
  9. "#{user.name}##{user.tag}"
  10. end
  11. footer_text = "#{user_name} | #{char.active}"
  12. footer_text += " | #{char.rating}" if char.rating
  13. footer_text += " | #{img.category} " if section == :image
  14. navigate = "React to Navigate"
  15. footer_text += " | #{navigate}" unless section.nil?
  16. status_effects = CharStatus.where(char_id: char.id)
  17. embed = Embed.new(
  18. footer: {
  19. text: footer_text
  20. },
  21. title: char.name,
  22. color: color,
  23. )
  24. case section
  25. when :all, nil
  26. embed.description = char.personality if char.personality
  27. fields = char_type(char, fields)
  28. fields = char_status(char, fields, status_effects)
  29. fields = char_bio(char, fields)
  30. fields = char_rumors(char, fields)
  31. when :default
  32. embed.description = navigate
  33. fields = char_sections(fields)
  34. when :bio
  35. embed.description = char.personality if char.personality
  36. fields = char_bio(char, fields)
  37. when :type
  38. fields = char_type(char, fields)
  39. when :status
  40. fields = char_status(char, fields, status_effects)
  41. when :rumors
  42. fields = char_rumors(char, fields)
  43. when :image
  44. if img
  45. embed.title =
  46. "#{char.name} | #{img.keyword}" unless img.keyword == 'Default'
  47. embed.image = { url: img.url }
  48. else
  49. embed.description = "No character images found!"
  50. end
  51. when :bags
  52. bags = Inventory.where(char_id: char.id)
  53. fields = char_inv(bags, fields)
  54. end
  55. embed.thumbnail = { url: img.url } if img && section != :image
  56. embed.fields = fields
  57. embed.footer.icon_url = user.avatar_url if user && user != 'Public'
  58. embed
  59. end
  60. def char_bio(char, fields)
  61. fields.push(
  62. { name: 'Hometown', value: char.hometown, inline: true }
  63. )if char.hometown
  64. fields.push(
  65. { name: 'Location', value: char.location, inline: true }
  66. )if char.location
  67. fields.push(
  68. { name: 'Likes', value: char.likes }
  69. )if char.likes
  70. fields.push(
  71. { name: 'Dislikes', value: char.dislikes }
  72. )if char.dislikes
  73. fields.push(
  74. { name: 'Backstory', value: char.backstory }
  75. )if char.backstory
  76. fields.push(
  77. { name: 'Other', value: char.other }
  78. )if char.other
  79. fields.push(
  80. { name: 'DM Notes', value: char.dm_notes }
  81. )if char.dm_notes
  82. fields
  83. end
  84. def char_type(char, fields)
  85. fields.push(
  86. { name: 'Species', value: char.species, inline: true }
  87. )if char.species
  88. fields.push(
  89. { name: 'Type', value: char.types, inline: true }
  90. )if char.types
  91. if char.attacks
  92. attacks = char.attacks
  93. attacks = attacks.gsub(/\s?\|\s?/, "\n")
  94. fields.push({ name: 'Attacks', value: attacks })
  95. end
  96. fields
  97. end
  98. def char_rumors(char, fields)
  99. fields.push(
  100. { name: 'Warnings', value: char.warnings }
  101. )if char.warnings
  102. if char.rumors
  103. rumors = char.rumors.split(/\s?\|\s?/)
  104. rumors = rumors.shuffle
  105. rumors = rumors.join("\n")
  106. fields.push({ name: 'Rumors', value: rumors })
  107. end
  108. fields
  109. end
  110. def char_status(char, fields, status_effects=nil)
  111. fields.push(
  112. { name: 'Age', value: char.age, inline: true }
  113. )if char.age
  114. fields.push(
  115. { name: 'Gender', value: char.gender, inline: true }
  116. )if char.gender
  117. fields.push(
  118. { name: 'Weight', value: char.weight, inline: true }
  119. )if char.weight
  120. fields.push(
  121. { name: 'Height', value: char.height, inline: true }
  122. )if char.height
  123. fields.push(
  124. { name: 'Sexual Orientation', value: char.orientation, inline: true }
  125. )if char.orientation
  126. fields.push(
  127. { name: 'Relationship Status', value: char.relationship, inline: true }
  128. )if char.relationship
  129. afs = []
  130. status_effects.each do |se|
  131. s = Status.find(se.status_id)
  132. afs.push("#{se.amount}% #{s.effect.downcase}")
  133. end
  134. fields.push(
  135. { name: "Current Afflictions", value: afs.join("\n") }
  136. )unless afs.empty?
  137. fields
  138. end
  139. def char_inv(bags, fields)
  140. inv = []
  141. bags.each do |line|
  142. item = Item.find(line.item_id)
  143. inv_line = line.amount > 1 ? "#{item.name} [#{line.amount}]" : item.name
  144. inv.push(inv_line)
  145. end
  146. fields.push({ name: "Bags", value: inv.join("\n"), inline: true })
  147. end
  148. def char_sections(fields)
  149. CharCarousel::REACTIONS.map do |emoji, message|
  150. fields.push({
  151. name: emoji,
  152. value: message,
  153. inline: true
  154. })
  155. end
  156. fields
  157. end
  158. def char_list_embed(chars, user = nil)
  159. fields = []
  160. active = []
  161. npcs = []
  162. chars.each do |char|
  163. case char.active
  164. when 'Active'
  165. active.push char.name
  166. when 'NPC'
  167. npcs.push char.name
  168. end
  169. end
  170. fields.push({
  171. name: 'Active Characters',
  172. value: active.join(", ")
  173. })if active.length > 0
  174. fields.push({
  175. name: 'NPCs',
  176. value: npcs.join(", ")
  177. })if npcs.length > 0
  178. embed = Embed.new(
  179. title: 'Registered Characters',
  180. fields: fields
  181. )
  182. if user
  183. user_name = user.nickname || user.name
  184. embed.color = user.color.combined
  185. embed.title = "#{user_name}'s Characters"
  186. end
  187. embed
  188. end
  189. def user_char_embed(chars, user)
  190. fields = []
  191. active = []
  192. npcs = []
  193. user_name = user.nickname || user.name
  194. chars.each do |char|
  195. case char.active
  196. when 'Active'
  197. active.push char
  198. when 'NPC'
  199. npcs.push char.name
  200. end
  201. end
  202. active.each.with_index do |char, i|
  203. fields.push({
  204. name: "#{i+1} #{char.name}",
  205. value: "#{char.species} -- #{char.types}"
  206. })
  207. end
  208. unless npcs.empty?
  209. fields.push({ name: "#{user_name}'s NPCs", value: npcs.join(", ") })
  210. end
  211. embed = Embed.new(
  212. title: "#{user_name}'s Characters",
  213. description: "Click on the corresponding reaction to view the character",
  214. fields: fields
  215. )
  216. embed.color = user.color.combined if user.color
  217. embed
  218. end
  219. def dup_char_embed(chars, name)
  220. fields = []
  221. chars.each.with_index do |char, i|
  222. fields.push({
  223. name: "#{Emoji::NUMBERS[i]}: #{char.species}",
  224. value: "Created by <@#{char.user_id}>"
  225. })
  226. end
  227. Embed.new(
  228. title: "Which #{name}?",
  229. description: "Click on the corresponding reaction to pick",
  230. fields: fields
  231. )
  232. end
  233. def char_image_embed(char, image, user, color)
  234. footer = "#{user.name}##{user.tag} | #{char.active}" +
  235. " | #{image.category}"
  236. Embed.new(
  237. footer: {
  238. icon_url: user.avatar_url,
  239. text: footer
  240. },
  241. title: "#{char.name} | #{image.keyword}",
  242. color: color,
  243. image: {
  244. url: image.url
  245. }
  246. )
  247. end
  248. def image_list_embed(char, images, user, color)
  249. desc = ""
  250. images.each do |img|
  251. desc += "[#{img.keyword}](#{img.url})\n" unless img.keyword == 'Default'
  252. end
  253. Embed.new(
  254. title: char.name,
  255. description: desc,
  256. color: color,
  257. footer: {
  258. icon_url: user.avatar_url,
  259. text: "#{user.name}##{user.tag} | #{char.active}"
  260. }
  261. )
  262. end