character.rb 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  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 primary type"
  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{ |c| c.name if c.types.split("/").first === s.name }.compact
  197. end
  198. list = list.reject { |k,v| v == [] }
  199. list.each do |k,v|
  200. fields.push({ name: k, value: v.join(", ") })
  201. end
  202. when Region
  203. sort.each do |s|
  204. list[s.name] = chars.map do |c|
  205. next unless c.region == s.name
  206. name = c.name
  207. name += "*#{name}*" if c.user_id === /public/i
  208. name += "~~#{name}~~" if c.rating === /NSFW/i
  209. name
  210. end.compact
  211. end
  212. list["Unknown"] = chars.map do |c|
  213. next unless c.region.nil?
  214. name = c.name
  215. name += "*#{name}*" if c.user_id === /public/i
  216. name += "~~#{name}~~" if c.rating === /NSFW/i
  217. name
  218. end.compact
  219. list = list.reject { |k,v| v == [] }
  220. list.each do |k,v|
  221. fields.push({ name: k, value: v.join(", ") })
  222. end
  223. when nil
  224. list["guild"] = []
  225. list["legend"] = []
  226. chars.each do |c|
  227. case c.special
  228. when /legend/i
  229. list["legend"].push("#{c.name}, #{c.species} -- last seen: #{c.location || "???"}")
  230. when /guild/i
  231. list["guild"].push("#{c.name}, #{c.species}")
  232. end
  233. end
  234. list = list.reject { |k,v| v == [] }
  235. list.each do |k,v|
  236. case k
  237. when /legend/i
  238. fields.push({ name: "Mythic/Legend Pokemon", value: v.join("\n") })
  239. when /guild/i
  240. fields.push({ name: "Guild Employees", value: v.join("\n") })
  241. end
  242. end
  243. end
  244. if fields.empty?
  245. fields.push({name: "No Resulst", value: "--"})
  246. end
  247. Embed.new(
  248. title: title,
  249. description: desc,
  250. fields: fields
  251. )
  252. end
  253. def user_char_embed(chars, user)
  254. fields = []
  255. active = []
  256. archived = []
  257. npcs = []
  258. user_name = user&.nickname || user&.name
  259. chars.each do |char|
  260. case char.active
  261. when 'Active'
  262. active.push char
  263. when 'Archived'
  264. archived.push char.name
  265. when 'NPC'
  266. npcs.push char.name
  267. end
  268. end
  269. active.each.with_index do |char, i|
  270. fields.push({
  271. name: "#{i+1} #{char.name}",
  272. value: "#{char.species} -- #{char.types}"
  273. })
  274. end
  275. unless archived.empty?
  276. fields.push({
  277. name: "#{user_name}'s Archived Characters",
  278. value: archived.join(", ")
  279. })
  280. end
  281. unless npcs.empty?
  282. fields.push({ name: "#{user_name}'s NPCs", value: npcs.join(", ") })
  283. end
  284. if user
  285. allowed = User.find_by(id: user&.id).level / 10 + 1
  286. allowed =
  287. user.roles.map(&:name).include?('Nitro Booster') ? allowed + 1 : allowed
  288. else
  289. allowed = '???'
  290. end
  291. embed = Embed.new(
  292. title: "#{user_name}'s Characters [#{active.count}/#{allowed}]",
  293. description: "Click on the corresponding reaction to view the character",
  294. fields: fields
  295. )
  296. embed.color = user.color.combined if user&.color
  297. embed
  298. end
  299. def dup_char_embed(chars, name)
  300. fields = []
  301. chars.each.with_index do |char, i|
  302. fields.push({
  303. name: "#{Emoji::NUMBERS[i]}: #{char.species}",
  304. value: "Created by <@#{char.user_id}>"
  305. })
  306. end
  307. Embed.new(
  308. title: "Which #{name}?",
  309. description: "Click on the corresponding reaction to pick",
  310. fields: fields
  311. )
  312. end
  313. def char_image_embed(char, image, user, color)
  314. user_name = case user
  315. when String
  316. user.capitalize
  317. when nil
  318. 'Unknown User'
  319. else
  320. "#{user.name}##{user.tag}"
  321. end
  322. footer_text = "#{user_name} | #{char.active}"
  323. footer_text += " | #{char.rating}" if char.rating
  324. footer_text += " | #{image.category}"
  325. Embed.new(
  326. footer: {
  327. icon_url: user&.avatar_url,
  328. text: footer_text
  329. },
  330. title: "#{char.name} | #{image.keyword}",
  331. color: color,
  332. image: {
  333. url: image.url
  334. }
  335. )
  336. end
  337. def image_list_embed(char, images, user, color)
  338. desc = ""
  339. images.each do |img|
  340. desc += "[#{img.keyword}](#{img.url})\n" unless img.keyword == 'Default'
  341. end
  342. Embed.new(
  343. title: char.name,
  344. description: desc,
  345. color: color,
  346. footer: {
  347. icon_url: user.avatar_url,
  348. text: "#{user.name}##{user.tag} | #{char.active}"
  349. }
  350. )
  351. end
  352. def nsfw_char_embed(char:, user: nil, color:, event: nil)
  353. icon = nil
  354. user_name = case user
  355. when /Public/i
  356. 'Adopt Me!'
  357. when /Server/i
  358. icon = event.server.icon_url if event
  359. 'Server Owned'
  360. when nil
  361. icon = UNKNOWN_USER_IMG
  362. 'Unknown User'
  363. else
  364. icon = user.avatar_url
  365. "#{user.name}##{user.tag}"
  366. end
  367. footer_text = "#{user_name} | #{char.active}"
  368. footer_text += " | #{char.rating}" if char.rating
  369. embed = Embed.new(
  370. footer: {
  371. icon_url: icon,
  372. text: footer_text
  373. },
  374. title: char.name,
  375. color: color,
  376. fields: [
  377. { name: 'Wrong Channel!', value: "The requested information contains NSFW content" }
  378. ]
  379. )
  380. embed
  381. end