character.rb 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  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. 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)
  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. fields.push({ name: "Bags", value: inv.join("\n"), inline: true })
  167. end
  168. def char_sections(fields)
  169. CharCarousel::REACTIONS.map do |emoji, message|
  170. fields.push({
  171. name: emoji,
  172. value: message,
  173. inline: true
  174. })
  175. end
  176. fields
  177. end
  178. def char_list_embed(chars, user = nil)
  179. fields = []
  180. active = []
  181. inactive= []
  182. owned_npcs = []
  183. unowned_npcs = []
  184. list = {
  185. "Active Guild Members" => active,
  186. "Former Guild Members" => inactive,
  187. "NPCs" => owned_npcs,
  188. "Public NPCs" => unowned_npcs
  189. }
  190. chars.each do |char|
  191. case char.active
  192. when 'Active'
  193. active.push char.name
  194. when 'Inactive'
  195. inactive.push char.name
  196. when 'NPC'
  197. owned_npcs.push char.name if char.user_id != 'Public'
  198. unowned_npcs.push char.name if char.user_id == 'Public'
  199. end
  200. end
  201. list.each do |name, array|
  202. unless array.empty?
  203. array = array.sort
  204. array.each_slice(100).each_with_index do |a, i|
  205. fields.push({
  206. name: "#{name} [#{i > 0 ? 'cont' : array.count}]",
  207. value: a.join(", ")
  208. })
  209. end
  210. end
  211. end
  212. #if active.count / 100 > 0
  213. #active = active.sort
  214. #active.each_slice(100) do |a|
  215. #fields.push({
  216. #name: "Active Guild Members [#{active.count}]",
  217. #value: a.join(", ")
  218. #})
  219. #end
  220. #else
  221. #fields.push({
  222. #name: "Active Guild Members [#{active.count}]",
  223. #value: active.sort.join(", ")
  224. #})if active.length > 0
  225. #end
  226. #fields.push({
  227. #name: "Former Guild Members [#{inactive.count}]",
  228. #value: inactive.sort.join(", ")
  229. #})if inactive.length > 0
  230. #fields.push({
  231. #name: "NPCs [#{owned_npcs.count}]",
  232. #value: owned_npcs.sort.join(", ")
  233. #})if owned_npcs.length > 0
  234. #fields.push({
  235. #name: "Public NPCs [#{unowned_npcs.count}]",
  236. #value: unowned_npcs.sort.join(", ")
  237. #})if unowned_npcs.length > 0
  238. embed = Embed.new(
  239. title: "Registered Pokemon [#{chars.count}]",
  240. fields: fields
  241. )
  242. if user
  243. user_name = user.nickname || user.name
  244. embed.color = user.color.combined
  245. embed.title = "#{user_name}'s Characters"
  246. end
  247. embed
  248. end
  249. def user_char_embed(chars, user)
  250. fields = []
  251. active = []
  252. inactive = []
  253. npcs = []
  254. user_name = user&.nickname || user&.name
  255. chars.each do |char|
  256. case char.active
  257. when 'Active'
  258. active.push char
  259. when 'Inactive'
  260. inactive.push char.name
  261. when 'NPC'
  262. npcs.push char.name
  263. end
  264. end
  265. active.each.with_index do |char, i|
  266. fields.push({
  267. name: "#{i+1} #{char.name}",
  268. value: "#{char.species} -- #{char.types}"
  269. })
  270. end
  271. unless inactive.empty?
  272. fields.push({
  273. name: "#{user_name}'s Inactive Characters",
  274. value: inactive.join(", ")
  275. })
  276. end
  277. unless npcs.empty?
  278. fields.push({ name: "#{user_name}'s NPCs", value: npcs.join(", ") })
  279. end
  280. allowed = User.find_by(id: user.id).level / 10 + 1
  281. allowed =
  282. user.roles.map(&:name).include?('Nitro Booster') ? allowed + 1 : allowed
  283. embed = Embed.new(
  284. title: "#{user_name}'s Characters [#{active.count}/#{allowed}]",
  285. description: "Click on the corresponding reaction to view the character",
  286. fields: fields
  287. )
  288. embed.color = user.color.combined if user&.color
  289. embed
  290. end
  291. def dup_char_embed(chars, name)
  292. fields = []
  293. chars.each.with_index do |char, i|
  294. fields.push({
  295. name: "#{Emoji::NUMBERS[i]}: #{char.species}",
  296. value: "Created by <@#{char.user_id}>"
  297. })
  298. end
  299. Embed.new(
  300. title: "Which #{name}?",
  301. description: "Click on the corresponding reaction to pick",
  302. fields: fields
  303. )
  304. end
  305. def char_image_embed(char, image, user, color)
  306. user_name = case user
  307. when String
  308. user.capitalize
  309. when nil
  310. 'Unknown User'
  311. else
  312. "#{user.name}##{user.tag}"
  313. end
  314. footer_text = "#{user_name} | #{char.active}"
  315. footer_text += " | #{char.rating}" if char.rating
  316. footer_text += " | #{image.category}"
  317. Embed.new(
  318. footer: {
  319. icon_url: user&.avatar_url,
  320. text: footer_text
  321. },
  322. title: "#{char.name} | #{image.keyword}",
  323. color: color,
  324. image: {
  325. url: image.url
  326. }
  327. )
  328. end
  329. def image_list_embed(char, images, user, color)
  330. desc = ""
  331. images.each do |img|
  332. desc += "[#{img.keyword}](#{img.url})\n" unless img.keyword == 'Default'
  333. end
  334. Embed.new(
  335. title: char.name,
  336. description: desc,
  337. color: color,
  338. footer: {
  339. icon_url: user.avatar_url,
  340. text: "#{user.name}##{user.tag} | #{char.active}"
  341. }
  342. )
  343. end