character.rb 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. def character_embed(char:, img: nil, user: nil, color:, section: nil)
  2. fields = []
  3. user_name = case user
  4. when String
  5. 'Adopt Me!'
  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. if s.amount
  133. afs.push("#{se.amount}% #{s.effect.downcase}")
  134. else
  135. afs.push(s.effect.capitalize)
  136. end
  137. end
  138. fields.push(
  139. { name: "Current Afflictions", value: afs.join("\n") }
  140. )unless afs.empty?
  141. fields
  142. end
  143. def char_inv(bags, fields)
  144. inv = []
  145. bags.each do |line|
  146. item = Item.find(line.item_id)
  147. inv_line = line.amount > 1 ? "#{item.name} [#{line.amount}]" : item.name
  148. inv.push(inv_line)
  149. end
  150. fields.push({ name: "Bags", value: inv.join("\n"), inline: true })
  151. end
  152. def char_sections(fields)
  153. CharCarousel::REACTIONS.map do |emoji, message|
  154. fields.push({
  155. name: emoji,
  156. value: message,
  157. inline: true
  158. })
  159. end
  160. fields
  161. end
  162. def char_list_embed(chars, user = nil)
  163. fields = []
  164. active = []
  165. inactive= []
  166. owned_npcs = []
  167. unowned_npcs = []
  168. chars.each do |char|
  169. case char.active
  170. when 'Active'
  171. active.push char.name
  172. when 'Inactive'
  173. inactive.push char.name
  174. when 'NPC'
  175. owned_npcs.push char.name if char.user_id != 'Public'
  176. unowned_npcs.push char.name if char.user_id == 'Public'
  177. end
  178. end
  179. fields.push({
  180. name: "Active Guild Members [#{active.count}]",
  181. value: active.sort.join(", ")
  182. })if active.length > 0
  183. fields.push({
  184. name: "Former Guild Members [#{inactive.count}]",
  185. value: inactive.sort.join(", ")
  186. })if inactive.length > 0
  187. fields.push({
  188. name: "NPCs [#{owned_npcs.count}]",
  189. value: owned_npcs.sort.join(", ")
  190. })if owned_npcs.length > 0
  191. fields.push({
  192. name: "Public NPCs [#{unowned_npcs.count}]",
  193. value: unowned_npcs.sort.join(", ")
  194. })if unowned_npcs.length > 0
  195. embed = Embed.new(
  196. title: "Registered Pokemon [#{chars.count}]",
  197. fields: fields
  198. )
  199. if user
  200. user_name = user.nickname || user.name
  201. embed.color = user.color.combined
  202. embed.title = "#{user_name}'s Characters"
  203. end
  204. embed
  205. end
  206. def user_char_embed(chars, user)
  207. fields = []
  208. active = []
  209. inactive = []
  210. npcs = []
  211. user_name = user&.nickname || user&.name
  212. chars.each do |char|
  213. case char.active
  214. when 'Active'
  215. active.push char
  216. when 'Inactive'
  217. inactive.push char.name
  218. when 'NPC'
  219. npcs.push char.name
  220. end
  221. end
  222. active.each.with_index do |char, i|
  223. fields.push({
  224. name: "#{i+1} #{char.name}",
  225. value: "#{char.species} -- #{char.types}"
  226. })
  227. end
  228. unless inactive.empty?
  229. fields.push({
  230. name: "#{user_name}'s Inactive Characters",
  231. value: inactive.join(", ")
  232. })
  233. end
  234. unless npcs.empty?
  235. fields.push({ name: "#{user_name}'s NPCs", value: npcs.join(", ") })
  236. end
  237. allowed = User.find_by(id: chars.first.user_id).level / 10 + 1
  238. embed = Embed.new(
  239. title: "#{user_name}'s Characters [#{active.count}/#{allowed}]",
  240. description: "Click on the corresponding reaction to view the character",
  241. fields: fields
  242. )
  243. embed.color = user.color.combined if user&.color
  244. embed
  245. end
  246. def dup_char_embed(chars, name)
  247. fields = []
  248. chars.each.with_index do |char, i|
  249. fields.push({
  250. name: "#{Emoji::NUMBERS[i]}: #{char.species}",
  251. value: "Created by <@#{char.user_id}>"
  252. })
  253. end
  254. Embed.new(
  255. title: "Which #{name}?",
  256. description: "Click on the corresponding reaction to pick",
  257. fields: fields
  258. )
  259. end
  260. def char_image_embed(char, image, user, color)
  261. user_name = case user
  262. when String
  263. user.capitalize
  264. when nil
  265. 'Unknown User'
  266. else
  267. "#{user.name}##{user.tag}"
  268. end
  269. footer_text = "#{user_name} | #{char.active}"
  270. footer_text += " | #{char.rating}" if char.rating
  271. footer_text += " | #{image.category}"
  272. Embed.new(
  273. footer: {
  274. icon_url: user&.avatar_url,
  275. text: footer_text
  276. },
  277. title: "#{char.name} | #{image.keyword}",
  278. color: color,
  279. image: {
  280. url: image.url
  281. }
  282. )
  283. end
  284. def image_list_embed(char, images, user, color)
  285. desc = ""
  286. images.each do |img|
  287. desc += "[#{img.keyword}](#{img.url})\n" unless img.keyword == 'Default'
  288. end
  289. Embed.new(
  290. title: char.name,
  291. description: desc,
  292. color: color,
  293. footer: {
  294. icon_url: user.avatar_url,
  295. text: "#{user.name}##{user.tag} | #{char.active}"
  296. }
  297. )
  298. end