character.rb 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. def character_embed(character:, event:, section: nil, image: nil)
  2. # Find the author, if they're a member, or in DMs use the event's author
  3. if event.server
  4. member = character.user_id.match(/public/i) ? 'Public' :
  5. event.server.member(character.user_id)
  6. else
  7. member = event.author
  8. end
  9. fields = []
  10. embed = Embed.new(
  11. title: character.name,
  12. color: character.type_color
  13. )
  14. # Save image, if there is one, and footer info
  15. default_img = CharImage.where(char_id: character.id).find_by(keyword: 'Default')
  16. footer_info = [character.active, character.rating]
  17. embed.thumbnail = { url: default_img.url } if default_img
  18. # Fill out the fields based on the section
  19. case section
  20. when /all/i, /default/i, nil
  21. embed.description = character.personality if character.personality
  22. fields = char_type(character, fields)
  23. fields = char_status(character, fields)
  24. fields = char_bio(character, fields)
  25. fields = char_rumors(character, fields)
  26. fields = char_dm_notes(character, fields, event)
  27. when /bio/i
  28. embed.description = character.personality if character.personality
  29. fields = char_bio(character, fields)
  30. fields = char_dm_notes(character, fields, event)
  31. when /types?/i
  32. fields = char_type(character, fields)
  33. when /status/i
  34. fields = char_status(character, fields)
  35. when /rumors?/i
  36. fields = char_rumors(character, fields)
  37. when /images?/i
  38. image = image ? image : default_img
  39. if image
  40. embed.title =
  41. "#{character.name} | #{image.keyword}" unless image.keyword == 'Default'
  42. embed.image = { url: image.url }
  43. # Replace the rating with the image's rating
  44. footer_info.pop
  45. footer_info.push(image.category)
  46. else
  47. embed.description = "No character images found!"
  48. end
  49. # Remove default image
  50. embed.thumbnail = nil
  51. when /bags?/i, /inventory/i
  52. fields = char_inv(character, fields)
  53. end
  54. # Add fields to embed
  55. embed.fields = fields
  56. # Add ID to footer, and apply to embed
  57. footer_info.push(character.id)
  58. author_footer(embed, member, footer_info)
  59. end
  60. def char_bio(char, fields)
  61. # Find the appropriate teams
  62. char_teams = CharTeam.where(char_id: char.id, active: true).map(&:team_id)
  63. teams = Team.where(id: char_teams).map(&:name)
  64. fields.push(
  65. { name: 'Hometown', value: char.hometown, inline: true }
  66. )if char.hometown
  67. fields.push(
  68. { name: 'Location', value: char.location, inline: true }
  69. )if char.location
  70. fields.push(
  71. { name: 'Likes', value: char.likes }
  72. )if char.likes
  73. fields.push(
  74. { name: 'Dislikes', value: char.dislikes }
  75. )if char.dislikes
  76. fields.push(
  77. { name: 'Backstory', value: char.backstory }
  78. )if char.backstory
  79. fields.push(
  80. { name: 'Recent Events', value: char.recent_events }
  81. )if char.recent_events
  82. fields.push(
  83. { name: 'Other', value: char.other }
  84. )if char.other
  85. fields.push(
  86. { name: 'Team', value: teams.join("\n") }
  87. )if !teams.empty?
  88. fields
  89. end
  90. def char_type(char, fields)
  91. sp = char.shiny ? "#{char.species} #{Emoji::STAR}" : char.species
  92. fields.push(
  93. { name: 'Species', value: sp, inline: true }
  94. )if char.species
  95. fields.push(
  96. { name: 'Type', value: char.types, inline: true }
  97. )if char.types
  98. if char.attacks
  99. attacks = char.attacks
  100. attacks = attacks.gsub(/\s?\|\s?/, "\n")
  101. fields.push({ name: 'Attacks', value: attacks })
  102. end
  103. fields
  104. end
  105. def char_rumors(char, fields)
  106. fields.push(
  107. { name: 'Warnings', value: char.warnings }
  108. )if char.warnings
  109. if char.rumors
  110. rumors = char.rumors.split(/\s?\|\s?/)
  111. rumors = rumors.shuffle
  112. rumors = rumors.join("\n")
  113. fields.push({ name: 'Rumors', value: rumors })
  114. end
  115. fields
  116. end
  117. def char_status(char, fields, status_effects=nil)
  118. # Find any status effects on the character
  119. status_effects = CharStatus.where(char_id: char.id)
  120. fields.push(
  121. { name: 'Age', value: char.age, inline: true }
  122. )if char.age
  123. fields.push(
  124. { name: 'Gender', value: char.gender, inline: true }
  125. )if char.gender
  126. fields.push(
  127. { name: 'Weight', value: char.weight, inline: true }
  128. )if char.weight
  129. fields.push(
  130. { name: 'Height', value: char.height, inline: true }
  131. )if char.height
  132. fields.push(
  133. { name: 'Sexual Orientation', value: char.orientation, inline: true }
  134. )if char.orientation
  135. fields.push(
  136. { name: 'Relationship Status', value: char.relationship, inline: true }
  137. )if char.relationship
  138. afs = []
  139. status_effects.each do |se|
  140. s = Status.find(se.status_id)
  141. if s.amount
  142. afs.push("#{se.amount}% #{s.effect.downcase}")
  143. else
  144. afs.push(s.effect.capitalize)
  145. end
  146. end
  147. fields.push(
  148. { name: "Current Afflictions", value: afs.join("\n") }
  149. )unless afs.empty?
  150. fields
  151. end
  152. def char_inv(char, fields)
  153. # Retrive array of [item amount, item name], and format
  154. inv = char.fetch_inventory
  155. bags = inv.map { |i| i[0] > 1 ? "#{i[1]} [#{i[0]}]" : i[1] }
  156. # Show formatted items
  157. value = bags.empty? ? "#{char.name} doesn't have any items" : bags.join("\n")
  158. fields.push({ name: "Bags", value: value })
  159. fields
  160. end
  161. def char_dm_notes(char, fields, event)
  162. return fields unless ENV['DM_CH'].include?(event.channel.id.to_s)
  163. fields.push(
  164. { name: 'DM Notes', value: char.dm_notes }
  165. )if char.dm_notes
  166. fields
  167. end
  168. def char_list_embed(chars, group, sort = nil)
  169. fields = []
  170. list = {}
  171. case group
  172. when /active/i
  173. title = "Registered Guild Members -- [#{chars.count}]"
  174. desc = "These are the pokemon registered to the guild, sorted by primary type"
  175. when /archived/i
  176. title = "Archived Guild Members -- [#{chars.count}]"
  177. desc = "These are the pokemon in the guild archives, sorted by primary type"
  178. when /npc/i
  179. title = "Registered Guild NPCs -- [#{chars.length}]"
  180. desc = "These are the NPCs from all around Zaplana, sorted by current location"
  181. when /special/i
  182. title = "Special Characters -- [#{chars.count}]"
  183. desc = "These are the special pokemon around Zaplana, sorted by category"
  184. end
  185. case sort&.first
  186. when Type
  187. sort.each do |s|
  188. list[s.name] = chars.map do |c|
  189. next unless c.types.split("/").first === s.name
  190. name = c.name
  191. name = "~~#{name}~~" if c.rating&.match(/NSFW/i)
  192. name
  193. end.compact
  194. end
  195. list = list.reject { |k,v| v == [] }
  196. list.each do |k,v|
  197. fields.push({ name: k, value: v.join(", ") })
  198. end
  199. when Region
  200. sort.each do |s|
  201. list[s.name] = chars.map do |c|
  202. next unless c.region == s.name
  203. name = c.name
  204. name = "*#{name}*" if c.user_id.match(/public/i)
  205. name = "~~#{name}~~" if c.rating&.match(/NSFW/i)
  206. name
  207. end.compact
  208. end
  209. list["Unknown"] = chars.map do |c|
  210. next unless c.region.nil?
  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. list = list.reject { |k,v| v == [] }
  217. list.each do |k,v|
  218. fields.push({ name: k, value: v.join(", ") })
  219. end
  220. when nil
  221. list["guild"] = []
  222. list["adoptable"] = []
  223. list["legend"] = []
  224. chars.each do |c|
  225. case c.special
  226. when /legend/i
  227. list["legend"].push("#{c.name}, #{c.species} -- last seen: #{c.location || "???"}")
  228. when /guild/i
  229. list["guild"].push("#{c.name}, #{c.species}")
  230. else
  231. list["adoptable"].push("#{c.name}, #{c.species} -- #{c.location || "???"}")
  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. when /adoptable/i
  242. fields.push({ name: "Adoptable NPCs", value: v.join("\n") })
  243. end
  244. end
  245. end
  246. if fields.empty?
  247. fields.push({name: "No Resulst", value: "--"})
  248. end
  249. Embed.new(
  250. title: title,
  251. description: desc,
  252. fields: fields,
  253. footer: {
  254. text: "React to Navigate | 1. Active | 2. Archived | 3. NPCs | 4. Special"
  255. }
  256. )
  257. end
  258. def user_char_embed(chars, member, nsfw=nil)
  259. fields = []
  260. active = []
  261. archived = []
  262. npcs = []
  263. user_name = member&.nickname || member&.name || "Unknown User"
  264. chars.each do |char|
  265. case char.active
  266. when 'Active'
  267. active.push char
  268. when 'Archived'
  269. archived.push char.name
  270. when 'NPC'
  271. npcs.push char.name
  272. end
  273. end
  274. active.each.with_index do |char, i|
  275. fields.push({
  276. name: "#{i+1} #{char.name}",
  277. value: "#{'~~' if nsfw}#{char.species} -- #{char.types}#{'~~' if nsfw}"
  278. })
  279. end
  280. unless archived.empty?
  281. fields.push({
  282. name: "#{user_name}'s Archived Characters",
  283. value: archived.join(", ")
  284. })
  285. end
  286. unless npcs.empty?
  287. fields.push({ name: "#{user_name}'s NPCs", value: npcs.join(", ") })
  288. end
  289. # Find allowed active characters
  290. allowed = member ? User.find(member.id.to_s).allowed_chars(member) : '???'
  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 = member&.color&.combined if member&.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 image_list_embed(character, event)
  314. # Find the author, if they're a member, or in DMs use the event's author
  315. if event.server
  316. member = character.user_id.match(/public/i) ? 'Public' :
  317. event.server.member(character.user_id)
  318. else
  319. member = event.author
  320. end
  321. # Grab an array of the character's images
  322. images = CharImage.where(char_id: character.id).
  323. map{ |i| "[#{i.keyword}](#{i.url})" }
  324. # Create Embed
  325. embed = Embed.new(
  326. title: character.name,
  327. description: images.join("\n"),
  328. color: character.type_color
  329. )
  330. # Add footer to embed
  331. author_footer(embed, member, [character.active, character.id])
  332. end
  333. def nsfw_char_embed(character, event)
  334. # Find the author, if they're a member,
  335. member = event.server.member(character.user_id)
  336. # Create Embed
  337. embed = Embed.new(
  338. title: character.name,
  339. color: character.type_color,
  340. fields: [{
  341. name: 'Wrong Channel!',
  342. value: 'The requested information contains NSFW content'
  343. }]
  344. )
  345. embed.thumbnail = nil
  346. # Apply appropriate footer to embed
  347. author_footer(embed, member, [character.active, character.rating, character.id])
  348. end