character.rb 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  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, active: true)
  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, :default
  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 :bio
  40. embed.description = char.personality if char.personality
  41. fields = char_bio(char, fields, char_teams)
  42. when :type
  43. fields = char_type(char, fields)
  44. when :status
  45. fields = char_status(char, fields, status_effects)
  46. when :rumors
  47. fields = char_rumors(char, fields)
  48. when :image
  49. if img
  50. embed.title =
  51. "#{char.name} | #{img.keyword}" unless img.keyword == 'Default'
  52. embed.image = { url: img.url }
  53. else
  54. embed.description = "No character images found!"
  55. end
  56. when :bags
  57. bags = Inventory.where(char_id: char.id)
  58. fields = char_inv(bags, fields, char.name)
  59. end
  60. embed.thumbnail = { url: img.url } if img && section != :image
  61. embed.fields = fields
  62. embed.footer.icon_url = icon
  63. embed
  64. end
  65. def char_bio(char, fields, char_teams)
  66. teams = []
  67. char_teams.each do |ct|
  68. teams.push(Team.find(ct.team_id).name)
  69. end
  70. fields.push(
  71. { name: 'Hometown', value: char.hometown, inline: true }
  72. )if char.hometown
  73. fields.push(
  74. { name: 'Location', value: char.location, inline: true }
  75. )if char.location
  76. fields.push(
  77. { name: 'Likes', value: char.likes }
  78. )if char.likes
  79. fields.push(
  80. { name: 'Dislikes', value: char.dislikes }
  81. )if char.dislikes
  82. fields.push(
  83. { name: 'Backstory', value: char.backstory }
  84. )if char.backstory
  85. fields.push(
  86. { name: 'Recent Events', value: char.recent_events }
  87. )if char.recent_events
  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, group, sort = nil)
  180. fields = []
  181. list = {}
  182. case group
  183. when /active/i
  184. title = "Registered Guild Members -- [#{chars.count}]"
  185. desc = "These are the pokemon registered to the guild, sorted by primary type"
  186. when /archived/i
  187. title = "Archived Guild Members -- [#{chars.count}]"
  188. desc = "These are the pokemon in the guild archives, sorted by primary type"
  189. when /npc/i
  190. title = "Registered Guild NPCs -- [#{chars.length}]"
  191. desc = "These are the NPCs from all around Zaplana, sorted by current location"
  192. when /special/i
  193. title = "Special Characters -- [#{chars.count}]"
  194. desc = "These are the special pokemon around Zaplana, sorted by category"
  195. end
  196. case sort&.first
  197. when Type
  198. sort.each do |s|
  199. list[s.name] = chars.map do |c|
  200. next unless c.types.split("/").first === s.name
  201. name = c.name
  202. name = "~~#{name}~~" if c.rating&.match(/NSFW/i)
  203. name
  204. end.compact
  205. end
  206. list = list.reject { |k,v| v == [] }
  207. list.each do |k,v|
  208. fields.push({ name: k, value: v.join(", ") })
  209. end
  210. when Region
  211. sort.each do |s|
  212. list[s.name] = chars.map do |c|
  213. next unless c.region == s.name
  214. name = c.name
  215. name = "*#{name}*" if c.user_id.match(/public/i)
  216. name = "~~#{name}~~" if c.rating&.match(/NSFW/i)
  217. name
  218. end.compact
  219. end
  220. list["Unknown"] = chars.map do |c|
  221. next unless c.region.nil?
  222. name = c.name
  223. name = "*#{name}*" if c.user_id.match(/public/i)
  224. name = "~~#{name}~~" if c.rating&.match(/NSFW/i)
  225. name
  226. end.compact
  227. list = list.reject { |k,v| v == [] }
  228. list.each do |k,v|
  229. fields.push({ name: k, value: v.join(", ") })
  230. end
  231. when nil
  232. list["guild"] = []
  233. list["adoptable"] = []
  234. list["legend"] = []
  235. chars.each do |c|
  236. case c.special
  237. when /legend/i
  238. list["legend"].push("#{c.name}, #{c.species} -- last seen: #{c.location || "???"}")
  239. when /guild/i
  240. list["guild"].push("#{c.name}, #{c.species}")
  241. when nil
  242. list["adoptable"].push("#{c.name}, #{c.species} -- #{c.location || "???"}")
  243. end
  244. end
  245. list = list.reject { |k,v| v == [] }
  246. list.each do |k,v|
  247. case k
  248. when /legend/i
  249. fields.push({ name: "Mythic/Legend Pokemon", value: v.join("\n") })
  250. when /guild/i
  251. fields.push({ name: "Guild Employees", value: v.join("\n") })
  252. when /adoptable/i
  253. fields.push({ name: "Adoptable NPCs", value: v.join("\n") })
  254. end
  255. end
  256. end
  257. if fields.empty?
  258. fields.push({name: "No Resulst", value: "--"})
  259. end
  260. Embed.new(
  261. title: title,
  262. description: desc,
  263. fields: fields,
  264. footer: {
  265. text: "React to Navigate | 1. Active | 2. Archived | 3. NPCs | 4. Special"
  266. }
  267. )
  268. end
  269. def user_char_embed(chars, user)
  270. fields = []
  271. active = []
  272. archived = []
  273. npcs = []
  274. user_name = user&.nickname || user&.name
  275. chars.each do |char|
  276. case char.active
  277. when 'Active'
  278. active.push char
  279. when 'Archived'
  280. archived.push char.name
  281. when 'NPC'
  282. npcs.push char.name
  283. end
  284. end
  285. active.each.with_index do |char, i|
  286. fields.push({
  287. name: "#{i+1} #{char.name}",
  288. value: "#{char.species} -- #{char.types}"
  289. })
  290. end
  291. unless archived.empty?
  292. fields.push({
  293. name: "#{user_name}'s Archived Characters",
  294. value: archived.join(", ")
  295. })
  296. end
  297. unless npcs.empty?
  298. fields.push({ name: "#{user_name}'s NPCs", value: npcs.join(", ") })
  299. end
  300. if user
  301. allowed = User.find_by(id: user&.id).level / 10 + 1
  302. allowed =
  303. user.roles.map(&:name).include?('Nitro Booster') ? allowed + 1 : allowed
  304. else
  305. allowed = '???'
  306. end
  307. embed = Embed.new(
  308. title: "#{user_name}'s Characters [#{active.count}/#{allowed}]",
  309. description: "Click on the corresponding reaction to view the character",
  310. fields: fields
  311. )
  312. embed.color = user.color.combined if user&.color
  313. embed
  314. end
  315. def dup_char_embed(chars, name)
  316. fields = []
  317. chars.each.with_index do |char, i|
  318. fields.push({
  319. name: "#{Emoji::NUMBERS[i]}: #{char.species}",
  320. value: "Created by <@#{char.user_id}>"
  321. })
  322. end
  323. Embed.new(
  324. title: "Which #{name}?",
  325. description: "Click on the corresponding reaction to pick",
  326. fields: fields
  327. )
  328. end
  329. def char_image_embed(char, image, user, color)
  330. user_name = case user
  331. when String
  332. user.capitalize
  333. when nil
  334. 'Unknown User'
  335. else
  336. "#{user.name}##{user.tag}"
  337. end
  338. footer_text = "#{user_name} | #{char.active}"
  339. footer_text += " | #{char.rating}" if char.rating
  340. footer_text += " | #{image.category}"
  341. Embed.new(
  342. footer: {
  343. icon_url: user&.avatar_url,
  344. text: footer_text
  345. },
  346. title: "#{char.name} | #{image.keyword}",
  347. color: color,
  348. image: {
  349. url: image.url
  350. }
  351. )
  352. end
  353. def image_list_embed(char, images, user, color)
  354. desc = ""
  355. images.each do |img|
  356. desc += "[#{img.keyword}](#{img.url})\n" unless img.keyword == 'Default'
  357. end
  358. Embed.new(
  359. title: char.name,
  360. description: desc,
  361. color: color,
  362. footer: {
  363. icon_url: user.avatar_url,
  364. text: "#{user.name}##{user.tag} | #{char.active}"
  365. }
  366. )
  367. end
  368. def nsfw_char_embed(char:, user: nil, color:, event: nil)
  369. icon = nil
  370. user_name = case user
  371. when /Public/i
  372. 'Adopt Me!'
  373. when /Server/i
  374. icon = event.server.icon_url if event
  375. 'Server Owned'
  376. when nil
  377. icon = UNKNOWN_USER_IMG
  378. 'Unknown User'
  379. else
  380. icon = user.avatar_url
  381. "#{user.name}##{user.tag}"
  382. end
  383. footer_text = "#{user_name} | #{char.active}"
  384. footer_text += " | #{char.rating}" if char.rating
  385. embed = Embed.new(
  386. footer: {
  387. icon_url: icon,
  388. text: footer_text
  389. },
  390. title: char.name,
  391. color: color,
  392. fields: [
  393. { name: 'Wrong Channel!', value: "The requested information contains NSFW content" }
  394. ]
  395. )
  396. embed
  397. end