character.rb 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  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: 'Other', value: char.other }
  87. )if char.other
  88. fields.push(
  89. { name: 'DM Notes', value: char.dm_notes }
  90. )if char.dm_notes
  91. fields.push(
  92. { name: 'Team', value: teams.join("\n") }
  93. )if !teams.empty?
  94. fields
  95. end
  96. def char_type(char, fields)
  97. sp = char.shiny ? "#{char.species} #{Emoji::STAR}" : char.species
  98. fields.push(
  99. { name: 'Species', value: sp, inline: true }
  100. )if char.species
  101. fields.push(
  102. { name: 'Type', value: char.types, inline: true }
  103. )if char.types
  104. if char.attacks
  105. attacks = char.attacks
  106. attacks = attacks.gsub(/\s?\|\s?/, "\n")
  107. fields.push({ name: 'Attacks', value: attacks })
  108. end
  109. fields
  110. end
  111. def char_rumors(char, fields)
  112. fields.push(
  113. { name: 'Warnings', value: char.warnings }
  114. )if char.warnings
  115. if char.rumors
  116. rumors = char.rumors.split(/\s?\|\s?/)
  117. rumors = rumors.shuffle
  118. rumors = rumors.join("\n")
  119. fields.push({ name: 'Rumors', value: rumors })
  120. end
  121. fields
  122. end
  123. def char_status(char, fields, status_effects=nil)
  124. fields.push(
  125. { name: 'Age', value: char.age, inline: true }
  126. )if char.age
  127. fields.push(
  128. { name: 'Gender', value: char.gender, inline: true }
  129. )if char.gender
  130. fields.push(
  131. { name: 'Weight', value: char.weight, inline: true }
  132. )if char.weight
  133. fields.push(
  134. { name: 'Height', value: char.height, inline: true }
  135. )if char.height
  136. fields.push(
  137. { name: 'Sexual Orientation', value: char.orientation, inline: true }
  138. )if char.orientation
  139. fields.push(
  140. { name: 'Relationship Status', value: char.relationship, inline: true }
  141. )if char.relationship
  142. afs = []
  143. status_effects.each do |se|
  144. s = Status.find(se.status_id)
  145. if s.amount
  146. afs.push("#{se.amount}% #{s.effect.downcase}")
  147. else
  148. afs.push(s.effect.capitalize)
  149. end
  150. end
  151. fields.push(
  152. { name: "Current Afflictions", value: afs.join("\n") }
  153. )unless afs.empty?
  154. fields
  155. end
  156. def char_inv(bags, fields, name=nil)
  157. inv = []
  158. bags.each do |line|
  159. item = Item.find(line.item_id)
  160. inv_line = line.amount > 1 ? "#{item.name} [#{line.amount}]" : item.name
  161. inv.push(inv_line)
  162. end
  163. value = inv.join("\n") || "#{name} doesn't have any items"
  164. fields.push({ name: "Bags", value: value, inline: true })
  165. end
  166. def char_sections(fields)
  167. CharCarousel::REACTIONS.map do |emoji, message|
  168. fields.push({
  169. name: emoji,
  170. value: message,
  171. inline: true
  172. })
  173. end
  174. fields
  175. end
  176. def char_list_embed(chars, group, sort = nil)
  177. fields = []
  178. list = {}
  179. case group
  180. when /active/i
  181. title = "Registered Guild Members -- [#{chars.count}]"
  182. desc = "These are the pokemon registered to the guild, sorted by primary type"
  183. when /archived/i
  184. title = "Archived Guild Members -- [#{chars.count}]"
  185. desc = "These are the pokemon in the guild archives, sorted by primary type"
  186. when /npc/i
  187. title = "Registered Guild NPCs -- [#{chars.length}]"
  188. desc = "These are the NPCs from all around Zaplana, sorted by current location"
  189. when /special/i
  190. title = "Specail Characters -- [#{chars.count}]"
  191. desc = "These are the special pokemon around Zaplana, sorted by category"
  192. end
  193. case sort&.first
  194. when Type
  195. sort.each do |s|
  196. list[s.name] = chars.map do |c|
  197. next unless c.types.split("/").first === s.name
  198. name = c.name
  199. name = "~~#{name}~~" if c.rating&.match(/NSFW/i)
  200. name
  201. end.compact
  202. end
  203. list = list.reject { |k,v| v == [] }
  204. list.each do |k,v|
  205. fields.push({ name: k, value: v.join(", ") })
  206. end
  207. when Region
  208. sort.each do |s|
  209. list[s.name] = chars.map do |c|
  210. next unless c.region == s.name
  211. name = c.name
  212. name = "*#{name}*" if c.user_id.match(/public/i)
  213. name = "~~#{name}~~" if c.rating&.match(/NSFW/i)
  214. name
  215. end.compact
  216. end
  217. list["Unknown"] = chars.map do |c|
  218. next unless c.region.nil?
  219. name = c.name
  220. name = "*#{name}*" if c.user_id.match(/public/i)
  221. name = "~~#{name}~~" if c.rating&.match(/NSFW/i)
  222. name
  223. end.compact
  224. list = list.reject { |k,v| v == [] }
  225. list.each do |k,v|
  226. fields.push({ name: k, value: v.join(", ") })
  227. end
  228. when nil
  229. list["guild"] = []
  230. list["adoptable"] = []
  231. list["legend"] = []
  232. chars.each do |c|
  233. case c.special
  234. when /legend/i
  235. list["legend"].push("#{c.name}, #{c.species} -- last seen: #{c.location || "???"}")
  236. when /guild/i
  237. list["guild"].push("#{c.name}, #{c.species}")
  238. when /adoptable/i
  239. list["adoptable"].push("#{c.name}, #{c.species} -- #{c.location || "???"}")
  240. end
  241. end
  242. list = list.reject { |k,v| v == [] }
  243. list.each do |k,v|
  244. case k
  245. when /legend/i
  246. fields.push({ name: "Mythic/Legend Pokemon", value: v.join("\n") })
  247. when /guild/i
  248. fields.push({ name: "Guild Employees", value: v.join("\n") })
  249. when /adoptable/i
  250. fields.push({ name: "Adoptable NPCs", value: v.join("\n") })
  251. end
  252. end
  253. end
  254. if fields.empty?
  255. fields.push({name: "No Resulst", value: "--"})
  256. end
  257. binding.pry
  258. Embed.new(
  259. title: title,
  260. description: desc,
  261. fields: fields
  262. )
  263. end
  264. def user_char_embed(chars, user)
  265. fields = []
  266. active = []
  267. archived = []
  268. npcs = []
  269. user_name = user&.nickname || user&.name
  270. chars.each do |char|
  271. case char.active
  272. when 'Active'
  273. active.push char
  274. when 'Archived'
  275. archived.push char.name
  276. when 'NPC'
  277. npcs.push char.name
  278. end
  279. end
  280. active.each.with_index do |char, i|
  281. fields.push({
  282. name: "#{i+1} #{char.name}",
  283. value: "#{char.species} -- #{char.types}"
  284. })
  285. end
  286. unless archived.empty?
  287. fields.push({
  288. name: "#{user_name}'s Archived Characters",
  289. value: archived.join(", ")
  290. })
  291. end
  292. unless npcs.empty?
  293. fields.push({ name: "#{user_name}'s NPCs", value: npcs.join(", ") })
  294. end
  295. if user
  296. allowed = User.find_by(id: user&.id).level / 10 + 1
  297. allowed =
  298. user.roles.map(&:name).include?('Nitro Booster') ? allowed + 1 : allowed
  299. else
  300. allowed = '???'
  301. end
  302. embed = Embed.new(
  303. title: "#{user_name}'s Characters [#{active.count}/#{allowed}]",
  304. description: "Click on the corresponding reaction to view the character",
  305. fields: fields
  306. )
  307. embed.color = user.color.combined if user&.color
  308. embed
  309. end
  310. def dup_char_embed(chars, name)
  311. fields = []
  312. chars.each.with_index do |char, i|
  313. fields.push({
  314. name: "#{Emoji::NUMBERS[i]}: #{char.species}",
  315. value: "Created by <@#{char.user_id}>"
  316. })
  317. end
  318. Embed.new(
  319. title: "Which #{name}?",
  320. description: "Click on the corresponding reaction to pick",
  321. fields: fields
  322. )
  323. end
  324. def char_image_embed(char, image, user, color)
  325. user_name = case user
  326. when String
  327. user.capitalize
  328. when nil
  329. 'Unknown User'
  330. else
  331. "#{user.name}##{user.tag}"
  332. end
  333. footer_text = "#{user_name} | #{char.active}"
  334. footer_text += " | #{char.rating}" if char.rating
  335. footer_text += " | #{image.category}"
  336. Embed.new(
  337. footer: {
  338. icon_url: user&.avatar_url,
  339. text: footer_text
  340. },
  341. title: "#{char.name} | #{image.keyword}",
  342. color: color,
  343. image: {
  344. url: image.url
  345. }
  346. )
  347. end
  348. def image_list_embed(char, images, user, color)
  349. desc = ""
  350. images.each do |img|
  351. desc += "[#{img.keyword}](#{img.url})\n" unless img.keyword == 'Default'
  352. end
  353. Embed.new(
  354. title: char.name,
  355. description: desc,
  356. color: color,
  357. footer: {
  358. icon_url: user.avatar_url,
  359. text: "#{user.name}##{user.tag} | #{char.active}"
  360. }
  361. )
  362. end
  363. def nsfw_char_embed(char:, user: nil, color:, event: nil)
  364. icon = nil
  365. user_name = case user
  366. when /Public/i
  367. 'Adopt Me!'
  368. when /Server/i
  369. icon = event.server.icon_url if event
  370. 'Server Owned'
  371. when nil
  372. icon = UNKNOWN_USER_IMG
  373. 'Unknown User'
  374. else
  375. icon = user.avatar_url
  376. "#{user.name}##{user.tag}"
  377. end
  378. footer_text = "#{user_name} | #{char.active}"
  379. footer_text += " | #{char.rating}" if char.rating
  380. embed = Embed.new(
  381. footer: {
  382. icon_url: icon,
  383. text: footer_text
  384. },
  385. title: char.name,
  386. color: color,
  387. fields: [
  388. { name: 'Wrong Channel!', value: "The requested information contains NSFW content" }
  389. ]
  390. )
  391. embed
  392. end