character.rb 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  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) if ENV['DM_CH'].include?(event.channel.id.to_s)
  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) if ENV['DM_CH'].include?(event.channel.id.to_s)
  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. end
  160. def char_dm_notes(char, fields)
  161. fields.push(
  162. { name: 'DM Notes', value: char.dm_notes }
  163. )if char.dm_notes
  164. end
  165. def char_list_embed(chars, group, sort = nil)
  166. fields = []
  167. list = {}
  168. case group
  169. when /active/i
  170. title = "Registered Guild Members -- [#{chars.count}]"
  171. desc = "These are the pokemon registered to the guild, sorted by primary type"
  172. when /archived/i
  173. title = "Archived Guild Members -- [#{chars.count}]"
  174. desc = "These are the pokemon in the guild archives, sorted by primary type"
  175. when /npc/i
  176. title = "Registered Guild NPCs -- [#{chars.length}]"
  177. desc = "These are the NPCs from all around Zaplana, sorted by current location"
  178. when /special/i
  179. title = "Special Characters -- [#{chars.count}]"
  180. desc = "These are the special pokemon around Zaplana, sorted by category"
  181. end
  182. case sort&.first
  183. when Type
  184. sort.each do |s|
  185. list[s.name] = chars.map do |c|
  186. next unless c.types.split("/").first === s.name
  187. name = c.name
  188. name = "~~#{name}~~" if c.rating&.match(/NSFW/i)
  189. name
  190. end.compact
  191. end
  192. list = list.reject { |k,v| v == [] }
  193. list.each do |k,v|
  194. fields.push({ name: k, value: v.join(", ") })
  195. end
  196. when Region
  197. sort.each do |s|
  198. list[s.name] = chars.map do |c|
  199. next unless c.region == s.name
  200. name = c.name
  201. name = "*#{name}*" if c.user_id.match(/public/i)
  202. name = "~~#{name}~~" if c.rating&.match(/NSFW/i)
  203. name
  204. end.compact
  205. end
  206. list["Unknown"] = chars.map do |c|
  207. next unless c.region.nil?
  208. name = c.name
  209. name = "*#{name}*" if c.user_id.match(/public/i)
  210. name = "~~#{name}~~" if c.rating&.match(/NSFW/i)
  211. name
  212. end.compact
  213. list = list.reject { |k,v| v == [] }
  214. list.each do |k,v|
  215. fields.push({ name: k, value: v.join(", ") })
  216. end
  217. when nil
  218. list["guild"] = []
  219. list["adoptable"] = []
  220. list["legend"] = []
  221. chars.each do |c|
  222. case c.special
  223. when /legend/i
  224. list["legend"].push("#{c.name}, #{c.species} -- last seen: #{c.location || "???"}")
  225. when /guild/i
  226. list["guild"].push("#{c.name}, #{c.species}")
  227. else
  228. list["adoptable"].push("#{c.name}, #{c.species} -- #{c.location || "???"}")
  229. end
  230. end
  231. list = list.reject { |k,v| v == [] }
  232. list.each do |k,v|
  233. case k
  234. when /legend/i
  235. fields.push({ name: "Mythic/Legend Pokemon", value: v.join("\n") })
  236. when /guild/i
  237. fields.push({ name: "Guild Employees", value: v.join("\n") })
  238. when /adoptable/i
  239. fields.push({ name: "Adoptable NPCs", value: v.join("\n") })
  240. end
  241. end
  242. end
  243. if fields.empty?
  244. fields.push({name: "No Resulst", value: "--"})
  245. end
  246. Embed.new(
  247. title: title,
  248. description: desc,
  249. fields: fields,
  250. footer: {
  251. text: "React to Navigate | 1. Active | 2. Archived | 3. NPCs | 4. Special"
  252. }
  253. )
  254. end
  255. def user_char_embed(chars, member, nsfw=nil)
  256. fields = []
  257. active = []
  258. archived = []
  259. npcs = []
  260. user_name = member&.nickname || member&.name || "Unknown User"
  261. chars.each do |char|
  262. case char.active
  263. when 'Active'
  264. active.push char
  265. when 'Archived'
  266. archived.push char.name
  267. when 'NPC'
  268. npcs.push char.name
  269. end
  270. end
  271. active.each.with_index do |char, i|
  272. fields.push({
  273. name: "#{i+1} #{char.name}",
  274. value: "#{'~~' if nsfw}#{char.species} -- #{char.types}#{'~~' if nsfw}"
  275. })
  276. end
  277. unless archived.empty?
  278. fields.push({
  279. name: "#{user_name}'s Archived Characters",
  280. value: archived.join(", ")
  281. })
  282. end
  283. unless npcs.empty?
  284. fields.push({ name: "#{user_name}'s NPCs", value: npcs.join(", ") })
  285. end
  286. # Find allowed active characters
  287. allowed = member ? User.find(member.id.to_s).allowed_chars(member) : '???'
  288. embed = Embed.new(
  289. title: "#{user_name}'s Characters [#{active.count}/#{allowed}]",
  290. description: "Click on the corresponding reaction to view the character",
  291. fields: fields
  292. )
  293. embed.color = member&.color&.combined if member&.color
  294. embed
  295. end
  296. def dup_char_embed(chars, name)
  297. fields = []
  298. chars.each.with_index do |char, i|
  299. fields.push({
  300. name: "#{Emoji::NUMBERS[i]}: #{char.species}",
  301. value: "Created by <@#{char.user_id}>"
  302. })
  303. end
  304. Embed.new(
  305. title: "Which #{name}?",
  306. description: "Click on the corresponding reaction to pick",
  307. fields: fields
  308. )
  309. end
  310. def image_list_embed(character, event)
  311. # Find the author, if they're a member, or in DMs use the event's author
  312. if event.server
  313. member = character.user_id.match(/public/i) ? 'Public' :
  314. event.server.member(character.user_id)
  315. else
  316. member = event.author
  317. end
  318. # Grab an array of the character's images
  319. images = CharImage.where(char_id: character.id).
  320. map{ |i| "[#{i.keyword}](#{i.url})" }
  321. # Create Embed
  322. embed = Embed.new(
  323. title: character.name,
  324. description: images.join("\n"),
  325. color: character.type_color
  326. )
  327. # Add footer to embed
  328. author_footer(embed, member, [character.active, character.id])
  329. end
  330. def nsfw_char_embed(character, event)
  331. # Find the author, if they're a member,
  332. member = event.server.member(character.user_id)
  333. # Create Embed
  334. embed = Embed.new(
  335. title: character.name,
  336. color: character.type_color,
  337. fields: [{
  338. name: 'Wrong Channel!',
  339. value: 'The requested information contains NSFW content'
  340. }]
  341. )
  342. embed.thumbnail = nil
  343. # Apply appropriate footer to embed
  344. author_footer(embed, member, [character.active, character.rating, character.id])
  345. end