character.rb 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  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)
  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. fields.push(
  101. { name: 'Species', value: "#{char.species} #{Emoji::STAR}", inline: true }
  102. )if char.species
  103. fields.push(
  104. { name: 'Type', value: char.types, inline: true }
  105. )if char.types
  106. if char.attacks
  107. attacks = char.attacks
  108. attacks = attacks.gsub(/\s?\|\s?/, "\n")
  109. fields.push({ name: 'Attacks', value: attacks })
  110. end
  111. fields
  112. end
  113. def char_rumors(char, fields)
  114. fields.push(
  115. { name: 'Warnings', value: char.warnings }
  116. )if char.warnings
  117. if char.rumors
  118. rumors = char.rumors.split(/\s?\|\s?/)
  119. rumors = rumors.shuffle
  120. rumors = rumors.join("\n")
  121. fields.push({ name: 'Rumors', value: rumors })
  122. end
  123. fields
  124. end
  125. def char_status(char, fields, status_effects=nil)
  126. fields.push(
  127. { name: 'Age', value: char.age, inline: true }
  128. )if char.age
  129. fields.push(
  130. { name: 'Gender', value: char.gender, inline: true }
  131. )if char.gender
  132. fields.push(
  133. { name: 'Weight', value: char.weight, inline: true }
  134. )if char.weight
  135. fields.push(
  136. { name: 'Height', value: char.height, inline: true }
  137. )if char.height
  138. fields.push(
  139. { name: 'Sexual Orientation', value: char.orientation, inline: true }
  140. )if char.orientation
  141. fields.push(
  142. { name: 'Relationship Status', value: char.relationship, inline: true }
  143. )if char.relationship
  144. afs = []
  145. status_effects.each do |se|
  146. s = Status.find(se.status_id)
  147. if s.amount
  148. afs.push("#{se.amount}% #{s.effect.downcase}")
  149. else
  150. afs.push(s.effect.capitalize)
  151. end
  152. end
  153. fields.push(
  154. { name: "Current Afflictions", value: afs.join("\n") }
  155. )unless afs.empty?
  156. fields
  157. end
  158. def char_inv(bags, fields)
  159. inv = []
  160. bags.each do |line|
  161. item = Item.find(line.item_id)
  162. inv_line = line.amount > 1 ? "#{item.name} [#{line.amount}]" : item.name
  163. inv.push(inv_line)
  164. end
  165. fields.push({ name: "Bags", value: inv.join("\n"), inline: true })
  166. end
  167. def char_sections(fields)
  168. CharCarousel::REACTIONS.map do |emoji, message|
  169. fields.push({
  170. name: emoji,
  171. value: message,
  172. inline: true
  173. })
  174. end
  175. fields
  176. end
  177. def char_list_embed(chars, user = nil)
  178. fields = []
  179. active = []
  180. inactive= []
  181. owned_npcs = []
  182. unowned_npcs = []
  183. list = {
  184. "Active Guild Members" => active,
  185. "Former Guild Members" => inactive,
  186. "NPCs" => owned_npcs,
  187. "Public NPCs" => unowned_npcs
  188. }
  189. chars.each do |char|
  190. case char.active
  191. when 'Active'
  192. active.push char.name
  193. when 'Inactive'
  194. inactive.push char.name
  195. when 'NPC'
  196. owned_npcs.push char.name if char.user_id != 'Public'
  197. unowned_npcs.push char.name if char.user_id == 'Public'
  198. end
  199. end
  200. list.each do |name, array|
  201. unless array.empty?
  202. array = array.sort
  203. array.each_slice(100).each_with_index do |a, i|
  204. fields.push({
  205. name: "#{name} [#{i > 0 ? 'cont' : array.count}]",
  206. value: a.join(", ")
  207. })
  208. end
  209. end
  210. end
  211. #if active.count / 100 > 0
  212. #active = active.sort
  213. #active.each_slice(100) do |a|
  214. #fields.push({
  215. #name: "Active Guild Members [#{active.count}]",
  216. #value: a.join(", ")
  217. #})
  218. #end
  219. #else
  220. #fields.push({
  221. #name: "Active Guild Members [#{active.count}]",
  222. #value: active.sort.join(", ")
  223. #})if active.length > 0
  224. #end
  225. #fields.push({
  226. #name: "Former Guild Members [#{inactive.count}]",
  227. #value: inactive.sort.join(", ")
  228. #})if inactive.length > 0
  229. #fields.push({
  230. #name: "NPCs [#{owned_npcs.count}]",
  231. #value: owned_npcs.sort.join(", ")
  232. #})if owned_npcs.length > 0
  233. #fields.push({
  234. #name: "Public NPCs [#{unowned_npcs.count}]",
  235. #value: unowned_npcs.sort.join(", ")
  236. #})if unowned_npcs.length > 0
  237. embed = Embed.new(
  238. title: "Registered Pokemon [#{chars.count}]",
  239. fields: fields
  240. )
  241. if user
  242. user_name = user.nickname || user.name
  243. embed.color = user.color.combined
  244. embed.title = "#{user_name}'s Characters"
  245. end
  246. embed
  247. end
  248. def user_char_embed(chars, user)
  249. fields = []
  250. active = []
  251. inactive = []
  252. npcs = []
  253. user_name = user&.nickname || user&.name
  254. chars.each do |char|
  255. case char.active
  256. when 'Active'
  257. active.push char
  258. when 'Inactive'
  259. inactive.push char.name
  260. when 'NPC'
  261. npcs.push char.name
  262. end
  263. end
  264. active.each.with_index do |char, i|
  265. fields.push({
  266. name: "#{i+1} #{char.name}",
  267. value: "#{char.species} -- #{char.types}"
  268. })
  269. end
  270. unless inactive.empty?
  271. fields.push({
  272. name: "#{user_name}'s Inactive Characters",
  273. value: inactive.join(", ")
  274. })
  275. end
  276. unless npcs.empty?
  277. fields.push({ name: "#{user_name}'s NPCs", value: npcs.join(", ") })
  278. end
  279. allowed = User.find_by(id: user.id).level / 10 + 1
  280. allowed =
  281. user.roles.map(&:name).include?('Nitro Booster') ? allowed + 1 : allowed
  282. embed = Embed.new(
  283. title: "#{user_name}'s Characters [#{active.count}/#{allowed}]",
  284. description: "Click on the corresponding reaction to view the character",
  285. fields: fields
  286. )
  287. embed.color = user.color.combined if user&.color
  288. embed
  289. end
  290. def dup_char_embed(chars, name)
  291. fields = []
  292. chars.each.with_index do |char, i|
  293. fields.push({
  294. name: "#{Emoji::NUMBERS[i]}: #{char.species}",
  295. value: "Created by <@#{char.user_id}>"
  296. })
  297. end
  298. Embed.new(
  299. title: "Which #{name}?",
  300. description: "Click on the corresponding reaction to pick",
  301. fields: fields
  302. )
  303. end
  304. def char_image_embed(char, image, user, color)
  305. user_name = case user
  306. when String
  307. user.capitalize
  308. when nil
  309. 'Unknown User'
  310. else
  311. "#{user.name}##{user.tag}"
  312. end
  313. footer_text = "#{user_name} | #{char.active}"
  314. footer_text += " | #{char.rating}" if char.rating
  315. footer_text += " | #{image.category}"
  316. Embed.new(
  317. footer: {
  318. icon_url: user&.avatar_url,
  319. text: footer_text
  320. },
  321. title: "#{char.name} | #{image.keyword}",
  322. color: color,
  323. image: {
  324. url: image.url
  325. }
  326. )
  327. end
  328. def image_list_embed(char, images, user, color)
  329. desc = ""
  330. images.each do |img|
  331. desc += "[#{img.keyword}](#{img.url})\n" unless img.keyword == 'Default'
  332. end
  333. Embed.new(
  334. title: char.name,
  335. description: desc,
  336. color: color,
  337. footer: {
  338. icon_url: user.avatar_url,
  339. text: "#{user.name}##{user.tag} | #{char.active}"
  340. }
  341. )
  342. end