character.rb 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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. chars.each do |char|
  177. case char.active
  178. when 'Active'
  179. active.push char.name
  180. when 'Inactive'
  181. inactive.push char.name
  182. when 'NPC'
  183. owned_npcs.push char.name if char.user_id != 'Public'
  184. unowned_npcs.push char.name if char.user_id == 'Public'
  185. end
  186. end
  187. fields.push({
  188. name: "Active Guild Members [#{active.count}]",
  189. value: active.sort.join(", ")
  190. })if active.length > 0
  191. fields.push({
  192. name: "Former Guild Members [#{inactive.count}]",
  193. value: inactive.sort.join(", ")
  194. })if inactive.length > 0
  195. fields.push({
  196. name: "NPCs [#{owned_npcs.count}]",
  197. value: owned_npcs.sort.join(", ")
  198. })if owned_npcs.length > 0
  199. fields.push({
  200. name: "Public NPCs [#{unowned_npcs.count}]",
  201. value: unowned_npcs.sort.join(", ")
  202. })if unowned_npcs.length > 0
  203. embed = Embed.new(
  204. title: "Registered Pokemon [#{chars.count}]",
  205. fields: fields
  206. )
  207. if user
  208. user_name = user.nickname || user.name
  209. embed.color = user.color.combined
  210. embed.title = "#{user_name}'s Characters"
  211. end
  212. embed
  213. end
  214. def user_char_embed(chars, user)
  215. fields = []
  216. active = []
  217. inactive = []
  218. npcs = []
  219. user_name = user&.nickname || user&.name
  220. chars.each do |char|
  221. case char.active
  222. when 'Active'
  223. active.push char
  224. when 'Inactive'
  225. inactive.push char.name
  226. when 'NPC'
  227. npcs.push char.name
  228. end
  229. end
  230. active.each.with_index do |char, i|
  231. fields.push({
  232. name: "#{i+1} #{char.name}",
  233. value: "#{char.species} -- #{char.types}"
  234. })
  235. end
  236. unless inactive.empty?
  237. fields.push({
  238. name: "#{user_name}'s Inactive Characters",
  239. value: inactive.join(", ")
  240. })
  241. end
  242. unless npcs.empty?
  243. fields.push({ name: "#{user_name}'s NPCs", value: npcs.join(", ") })
  244. end
  245. allowed = User.find_by(id: chars.first.user_id).level / 10 + 1
  246. embed = Embed.new(
  247. title: "#{user_name}'s Characters [#{active.count}/#{allowed}]",
  248. description: "Click on the corresponding reaction to view the character",
  249. fields: fields
  250. )
  251. embed.color = user.color.combined if user&.color
  252. embed
  253. end
  254. def dup_char_embed(chars, name)
  255. fields = []
  256. chars.each.with_index do |char, i|
  257. fields.push({
  258. name: "#{Emoji::NUMBERS[i]}: #{char.species}",
  259. value: "Created by <@#{char.user_id}>"
  260. })
  261. end
  262. Embed.new(
  263. title: "Which #{name}?",
  264. description: "Click on the corresponding reaction to pick",
  265. fields: fields
  266. )
  267. end
  268. def char_image_embed(char, image, user, color)
  269. user_name = case user
  270. when String
  271. user.capitalize
  272. when nil
  273. 'Unknown User'
  274. else
  275. "#{user.name}##{user.tag}"
  276. end
  277. footer_text = "#{user_name} | #{char.active}"
  278. footer_text += " | #{char.rating}" if char.rating
  279. footer_text += " | #{image.category}"
  280. Embed.new(
  281. footer: {
  282. icon_url: user&.avatar_url,
  283. text: footer_text
  284. },
  285. title: "#{char.name} | #{image.keyword}",
  286. color: color,
  287. image: {
  288. url: image.url
  289. }
  290. )
  291. end
  292. def image_list_embed(char, images, user, color)
  293. desc = ""
  294. images.each do |img|
  295. desc += "[#{img.keyword}](#{img.url})\n" unless img.keyword == 'Default'
  296. end
  297. Embed.new(
  298. title: char.name,
  299. description: desc,
  300. color: color,
  301. footer: {
  302. icon_url: user.avatar_url,
  303. text: "#{user.name}##{user.tag} | #{char.active}"
  304. }
  305. )
  306. end