character.rb 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. def character_embed(char:, img: nil, user: nil, color:, section: nil)
  2. fields = []
  3. user_name = case user
  4. when String
  5. 'Adopt Me!'
  6. when nil
  7. 'Unknown User'
  8. else
  9. "#{user.name}##{user.tag}"
  10. end
  11. footer_text = "#{user_name} | #{char.active}"
  12. footer_text += " | #{char.rating}" if char.rating
  13. footer_text += " | #{img&.category} " if section == :image
  14. navigate = "React to Navigate"
  15. footer_text += " | #{navigate}" unless section.nil?
  16. status_effects = CharStatus.where(char_id: char.id)
  17. char_teams = CharTeam.where(char_id: char.id)
  18. embed = Embed.new(
  19. footer: {
  20. text: footer_text
  21. },
  22. title: char.name,
  23. color: color,
  24. )
  25. case section
  26. when :all, nil
  27. embed.description = char.personality if char.personality
  28. fields = char_type(char, fields)
  29. fields = char_status(char, fields, status_effects)
  30. fields = char_bio(char, fields, char_teams)
  31. fields = char_rumors(char, fields)
  32. when :default
  33. embed.description = navigate
  34. fields = char_sections(fields)
  35. when :bio
  36. embed.description = char.personality if char.personality
  37. fields = char_bio(char, fields, char_teams)
  38. when :type
  39. fields = char_type(char, fields)
  40. when :status
  41. fields = char_status(char, fields, status_effects)
  42. when :rumors
  43. fields = char_rumors(char, fields)
  44. when :image
  45. if img
  46. embed.title =
  47. "#{char.name} | #{img.keyword}" unless img.keyword == 'Default'
  48. embed.image = { url: img.url }
  49. else
  50. embed.description = "No character images found!"
  51. end
  52. when :bags
  53. bags = Inventory.where(char_id: char.id)
  54. fields = char_inv(bags, fields)
  55. end
  56. embed.thumbnail = { url: img.url } if img && section != :image
  57. embed.fields = fields
  58. embed.footer.icon_url = user.avatar_url if user && user != 'Public'
  59. embed
  60. end
  61. def char_bio(char, fields, char_teams)
  62. teams = []
  63. char_teams.each do |ct|
  64. teams.push(Team.find(ct.team_id).name)
  65. end
  66. fields.push(
  67. { name: 'Hometown', value: char.hometown, inline: true }
  68. )if char.hometown
  69. fields.push(
  70. { name: 'Location', value: char.location, inline: true }
  71. )if char.location
  72. fields.push(
  73. { name: 'Likes', value: char.likes }
  74. )if char.likes
  75. fields.push(
  76. { name: 'Dislikes', value: char.dislikes }
  77. )if char.dislikes
  78. fields.push(
  79. { name: 'Backstory', value: char.backstory }
  80. )if char.backstory
  81. fields.push(
  82. { name: 'Other', value: char.other }
  83. )if char.other
  84. fields.push(
  85. { name: 'DM Notes', value: char.dm_notes }
  86. )if char.dm_notes
  87. fields.push(
  88. { name: 'Team', value: teams.join("\n") }
  89. )if !teams.empty?
  90. fields
  91. end
  92. def char_type(char, fields)
  93. fields.push(
  94. { name: 'Species', value: char.species, inline: true }
  95. )if char.species
  96. fields.push(
  97. { name: 'Type', value: char.types, inline: true }
  98. )if char.types
  99. if char.attacks
  100. attacks = char.attacks
  101. attacks = attacks.gsub(/\s?\|\s?/, "\n")
  102. fields.push({ name: 'Attacks', value: attacks })
  103. end
  104. fields
  105. end
  106. def char_rumors(char, fields)
  107. fields.push(
  108. { name: 'Warnings', value: char.warnings }
  109. )if char.warnings
  110. if char.rumors
  111. rumors = char.rumors.split(/\s?\|\s?/)
  112. rumors = rumors.shuffle
  113. rumors = rumors.join("\n")
  114. fields.push({ name: 'Rumors', value: rumors })
  115. end
  116. fields
  117. end
  118. def char_status(char, fields, status_effects=nil)
  119. fields.push(
  120. { name: 'Age', value: char.age, inline: true }
  121. )if char.age
  122. fields.push(
  123. { name: 'Gender', value: char.gender, inline: true }
  124. )if char.gender
  125. fields.push(
  126. { name: 'Weight', value: char.weight, inline: true }
  127. )if char.weight
  128. fields.push(
  129. { name: 'Height', value: char.height, inline: true }
  130. )if char.height
  131. fields.push(
  132. { name: 'Sexual Orientation', value: char.orientation, inline: true }
  133. )if char.orientation
  134. fields.push(
  135. { name: 'Relationship Status', value: char.relationship, inline: true }
  136. )if char.relationship
  137. afs = []
  138. status_effects.each do |se|
  139. s = Status.find(se.status_id)
  140. if s.amount
  141. afs.push("#{se.amount}% #{s.effect.downcase}")
  142. else
  143. afs.push(s.effect.capitalize)
  144. end
  145. end
  146. fields.push(
  147. { name: "Current Afflictions", value: afs.join("\n") }
  148. )unless afs.empty?
  149. fields
  150. end
  151. def char_inv(bags, fields)
  152. inv = []
  153. bags.each do |line|
  154. item = Item.find(line.item_id)
  155. inv_line = line.amount > 1 ? "#{item.name} [#{line.amount}]" : item.name
  156. inv.push(inv_line)
  157. end
  158. fields.push({ name: "Bags", value: inv.join("\n"), inline: true })
  159. end
  160. def char_sections(fields)
  161. CharCarousel::REACTIONS.map do |emoji, message|
  162. fields.push({
  163. name: emoji,
  164. value: message,
  165. inline: true
  166. })
  167. end
  168. fields
  169. end
  170. def char_list_embed(chars, user = nil)
  171. fields = []
  172. active = []
  173. inactive= []
  174. owned_npcs = []
  175. unowned_npcs = []
  176. list = {
  177. "Active Guild Members" => active,
  178. "Former Guild Members" => inactive,
  179. "NPCs" => owned_npcs,
  180. "Public NPCs" => unowned_npcs
  181. }
  182. chars.each do |char|
  183. case char.active
  184. when 'Active'
  185. active.push char.name
  186. when 'Inactive'
  187. inactive.push char.name
  188. when 'NPC'
  189. owned_npcs.push char.name if char.user_id != 'Public'
  190. unowned_npcs.push char.name if char.user_id == 'Public'
  191. end
  192. end
  193. list.each do |name, array|
  194. unless array.empty?
  195. array = array.sort
  196. array.each_slice(100).each_with_index do |a, i|
  197. fields.push({
  198. name: "#{name} [#{i > 0 ? 'cont' : array.count}]",
  199. value: a.join(", ")
  200. })
  201. end
  202. end
  203. end
  204. #if active.count / 100 > 0
  205. #active = active.sort
  206. #active.each_slice(100) do |a|
  207. #fields.push({
  208. #name: "Active Guild Members [#{active.count}]",
  209. #value: a.join(", ")
  210. #})
  211. #end
  212. #else
  213. #fields.push({
  214. #name: "Active Guild Members [#{active.count}]",
  215. #value: active.sort.join(", ")
  216. #})if active.length > 0
  217. #end
  218. #fields.push({
  219. #name: "Former Guild Members [#{inactive.count}]",
  220. #value: inactive.sort.join(", ")
  221. #})if inactive.length > 0
  222. #fields.push({
  223. #name: "NPCs [#{owned_npcs.count}]",
  224. #value: owned_npcs.sort.join(", ")
  225. #})if owned_npcs.length > 0
  226. #fields.push({
  227. #name: "Public NPCs [#{unowned_npcs.count}]",
  228. #value: unowned_npcs.sort.join(", ")
  229. #})if unowned_npcs.length > 0
  230. embed = Embed.new(
  231. title: "Registered Pokemon [#{chars.count}]",
  232. fields: fields
  233. )
  234. if user
  235. user_name = user.nickname || user.name
  236. embed.color = user.color.combined
  237. embed.title = "#{user_name}'s Characters"
  238. end
  239. embed
  240. end
  241. def user_char_embed(chars, user)
  242. fields = []
  243. active = []
  244. inactive = []
  245. npcs = []
  246. user_name = user&.nickname || user&.name
  247. chars.each do |char|
  248. case char.active
  249. when 'Active'
  250. active.push char
  251. when 'Inactive'
  252. inactive.push char.name
  253. when 'NPC'
  254. npcs.push char.name
  255. end
  256. end
  257. active.each.with_index do |char, i|
  258. fields.push({
  259. name: "#{i+1} #{char.name}",
  260. value: "#{char.species} -- #{char.types}"
  261. })
  262. end
  263. unless inactive.empty?
  264. fields.push({
  265. name: "#{user_name}'s Inactive Characters",
  266. value: inactive.join(", ")
  267. })
  268. end
  269. unless npcs.empty?
  270. fields.push({ name: "#{user_name}'s NPCs", value: npcs.join(", ") })
  271. end
  272. allowed = User.find_by(id: user.id).level / 10 + 1
  273. allowed =
  274. user.roles.map(&:name).include?('Nitro Booster') ? allowed + 1 : allowed
  275. embed = Embed.new(
  276. title: "#{user_name}'s Characters [#{active.count}/#{allowed}]",
  277. description: "Click on the corresponding reaction to view the character",
  278. fields: fields
  279. )
  280. embed.color = user.color.combined if user&.color
  281. embed
  282. end
  283. def dup_char_embed(chars, name)
  284. fields = []
  285. chars.each.with_index do |char, i|
  286. fields.push({
  287. name: "#{Emoji::NUMBERS[i]}: #{char.species}",
  288. value: "Created by <@#{char.user_id}>"
  289. })
  290. end
  291. Embed.new(
  292. title: "Which #{name}?",
  293. description: "Click on the corresponding reaction to pick",
  294. fields: fields
  295. )
  296. end
  297. def char_image_embed(char, image, user, color)
  298. user_name = case user
  299. when String
  300. user.capitalize
  301. when nil
  302. 'Unknown User'
  303. else
  304. "#{user.name}##{user.tag}"
  305. end
  306. footer_text = "#{user_name} | #{char.active}"
  307. footer_text += " | #{char.rating}" if char.rating
  308. footer_text += " | #{image.category}"
  309. Embed.new(
  310. footer: {
  311. icon_url: user&.avatar_url,
  312. text: footer_text
  313. },
  314. title: "#{char.name} | #{image.keyword}",
  315. color: color,
  316. image: {
  317. url: image.url
  318. }
  319. )
  320. end
  321. def image_list_embed(char, images, user, color)
  322. desc = ""
  323. images.each do |img|
  324. desc += "[#{img.keyword}](#{img.url})\n" unless img.keyword == 'Default'
  325. end
  326. Embed.new(
  327. title: char.name,
  328. description: desc,
  329. color: color,
  330. footer: {
  331. icon_url: user.avatar_url,
  332. text: "#{user.name}##{user.tag} | #{char.active}"
  333. }
  334. )
  335. end