character.rb 8.5 KB

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