character.rb 9.3 KB

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