character.rb 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  1. def character_embed(character:, event:, section: nil, image: nil, journal: 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. when /journal/i
  54. fields = char_journal(character, fields, journal)
  55. end
  56. # Add fields to embed
  57. embed.fields = fields
  58. # Add ID to footer, and apply to embed
  59. footer_info.push(character.id)
  60. author_footer(embed, member, footer_info)
  61. end
  62. def char_bio(char, fields)
  63. # Find the appropriate teams
  64. char_teams = CharTeam.where(char_id: char.id, active: true).map(&:team_id)
  65. teams = Team.where(id: char_teams).map(&:name)
  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: 'Recent Events', value: char.recent_events }
  83. )if char.recent_events
  84. fields.push(
  85. { name: 'Other', value: char.other }
  86. )if char.other
  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. sp = char.shiny ? "#{char.species} #{Emoji::STAR}" : char.species
  94. fields.push(
  95. { name: 'Species', value: sp, inline: true }
  96. )if char.species
  97. fields.push(
  98. { name: 'Type', value: char.types, inline: true }
  99. )if char.types
  100. if char.attacks
  101. attacks = char.attacks
  102. attacks = attacks.gsub(/\s?\|\s?/, "\n")
  103. fields.push({ name: 'Attacks', value: attacks })
  104. end
  105. fields
  106. end
  107. def char_rumors(char, fields)
  108. fields.push(
  109. { name: 'Warnings', value: char.warnings }
  110. )if char.warnings
  111. if char.rumors
  112. rumors = char.rumors.split(/\s?\|\s?/)
  113. rumors = rumors.shuffle
  114. rumors = rumors.join("\n")
  115. fields.push({ name: 'Rumors', value: rumors })
  116. end
  117. fields
  118. end
  119. def char_status(char, fields, status_effects=nil)
  120. # Find any status effects on the character
  121. status_effects = CharStatus.where(char_id: char.id)
  122. fields.push(
  123. { name: 'Age', value: char.age, inline: true }
  124. )if char.age
  125. fields.push(
  126. { name: 'Gender', value: char.gender, inline: true }
  127. )if char.gender
  128. fields.push(
  129. { name: 'Weight', value: char.weight, inline: true }
  130. )if char.weight
  131. fields.push(
  132. { name: 'Height', value: char.height, inline: true }
  133. )if char.height
  134. fields.push(
  135. { name: 'Sexual Orientation', value: char.orientation, inline: true }
  136. )if char.orientation
  137. fields.push(
  138. { name: 'Relationship Status', value: char.relationship, inline: true }
  139. )if char.relationship
  140. afs = []
  141. status_effects.each do |se|
  142. s = Status.find(se.status_id)
  143. if s.amount
  144. afs.push("#{se.amount}% #{s.effect.downcase}")
  145. else
  146. afs.push(s.effect.capitalize)
  147. end
  148. end
  149. fields.push(
  150. { name: "Current Afflictions", value: afs.join("\n") }
  151. )unless afs.empty?
  152. fields
  153. end
  154. def char_inv(char, fields)
  155. # Retrive array of [item amount, item name], and format
  156. inv = char.fetch_inventory
  157. bags = inv.map { |i| i[0] > 1 ? "#{i[1]} [#{i[0]}]" : i[1] }
  158. # Show formatted items
  159. value = bags.empty? ? "#{char.name} doesn't have any items" : bags.join("\n")
  160. fields.push({ name: "Bags", value: value })
  161. fields
  162. end
  163. def char_journal(char, fields, journal)
  164. if journal.is_a? JournalEntry
  165. fields.push({ name: journal.title, value: journal.entry })
  166. elsif journal.empty?
  167. fields.push({ name: 'Error', value: 'No journal entries found' })
  168. else
  169. # Display each journal entry
  170. journal.each do |j|
  171. fields.push({ name: j&.title || j.date, value: j.entry })
  172. end
  173. end
  174. fields
  175. end
  176. def char_dm_notes(char, fields, event)
  177. return fields unless ENV['DM_CH'].include?(event.channel.id.to_s)
  178. fields.push(
  179. { name: 'DM Notes', value: char.dm_notes }
  180. )if char.dm_notes
  181. fields
  182. end
  183. def char_list_embed(chars, group, sort = nil)
  184. fields = []
  185. list = {}
  186. case group
  187. when /active/i
  188. title = "Registered Guild Members -- [#{chars.count}]"
  189. desc = "These are the pokemon registered to the guild, sorted by primary type"
  190. when /archived/i
  191. title = "Archived Guild Members -- [#{chars.count}]"
  192. desc = "These are the pokemon in the guild archives, sorted by primary type"
  193. when /npc/i
  194. title = "Registered Guild NPCs -- [#{chars.length}]"
  195. desc = "These are the NPCs from all around Zaplana, sorted by current location"
  196. when /special/i
  197. title = "Special Characters -- [#{chars.count}]"
  198. desc = "These are the special pokemon around Zaplana, sorted by category"
  199. end
  200. case sort&.first
  201. when Type
  202. sort.each do |s|
  203. list[s.name] = chars.map do |c|
  204. next unless c.types.split("/").first === s.name
  205. name = c.name
  206. name = "|| #{name} ||" if c.rating&.match(/NSFW/i)
  207. name
  208. end.compact
  209. end
  210. list = list.reject { |k,v| v == [] }
  211. list.each do |k,v|
  212. fields.push({ name: k, value: v.join(", ") })
  213. end
  214. when Region
  215. sort.each do |s|
  216. list[s.name] = chars.map do |c|
  217. next unless c.region == s.name
  218. name = c.name
  219. name = "*#{name}*" if c.user_id.match(/public/i)
  220. name = "|| #{name} ||" if c.rating&.match(/NSFW/i)
  221. name
  222. end.compact
  223. end
  224. list["Unknown"] = chars.map do |c|
  225. next unless c.region.nil?
  226. name = c.name
  227. name = "*#{name}*" if c.user_id.match(/public/i)
  228. name = "|| #{name} ||" if c.rating&.match(/NSFW/i)
  229. name
  230. end.compact
  231. list = list.reject { |k,v| v == [] }
  232. list.each do |k,v|
  233. fields.push({ name: k, value: v.join(", ") })
  234. end
  235. when nil
  236. list["guild"] = []
  237. list["adoptable"] = []
  238. list["legend"] = []
  239. chars.each do |c|
  240. case c.special
  241. when /legend/i
  242. list["legend"].push("#{c.name}, #{c.species} -- last seen: #{c.location || "???"}")
  243. when /guild/i
  244. list["guild"].push("#{c.name}, #{c.species}")
  245. else
  246. list["adoptable"].push("#{c.name}, #{c.species} -- #{c.location || "???"}")
  247. end
  248. end
  249. list = list.reject { |k,v| v == [] }
  250. list.each do |k,v|
  251. case k
  252. when /legend/i
  253. fields.push({ name: "Mythic/Legend Pokemon", value: v.join("\n") })
  254. when /guild/i
  255. fields.push({ name: "Guild Employees", value: v.join("\n") })
  256. when /adoptable/i
  257. fields.push({ name: "Adoptable NPCs", value: v.join("\n") })
  258. end
  259. end
  260. end
  261. if fields.empty?
  262. fields.push({name: "No Resulst", value: "--"})
  263. end
  264. Embed.new(
  265. title: title,
  266. description: desc,
  267. fields: fields,
  268. footer: {
  269. text: "React to Navigate | 1. Active | 2. Archived | 3. NPCs | 4. Special"
  270. }
  271. )
  272. end
  273. def user_char_embed(chars, member, nsfw=nil)
  274. fields = []
  275. active = []
  276. archived = []
  277. npcs = []
  278. user_name = member&.nickname || member&.name || "Unknown User"
  279. chars.each do |char|
  280. case char.active
  281. when 'Active'
  282. active.push char
  283. when 'Archived'
  284. archived.push char.name
  285. when 'NPC'
  286. npcs.push char.name
  287. end
  288. end
  289. active.each.with_index do |char, i|
  290. name = nsfw && char.rating == 'NSFW' ?
  291. "#{i+1} || #{char.name} ||" : "#{i+1} #{char.name}"
  292. fields.push({
  293. name: name,
  294. value: "#{char.species} -- #{char.types}"
  295. })
  296. end
  297. unless archived.empty?
  298. fields.push({
  299. name: "#{user_name}'s Archived Characters",
  300. value: archived.join(", ")
  301. })
  302. end
  303. unless npcs.empty?
  304. fields.push({ name: "#{user_name}'s NPCs", value: npcs.join(", ") })
  305. end
  306. # Find allowed active characters
  307. allowed = member ? User.find(member.id.to_s).allowed_chars(member) : '???'
  308. embed = Embed.new(
  309. title: "#{user_name}'s Characters [#{active.count}/#{allowed}]",
  310. description: "Click on the corresponding reaction to view the character",
  311. fields: fields
  312. )
  313. embed.color = member&.color&.combined if member&.color
  314. embed
  315. end
  316. def dup_char_embed(chars, name)
  317. fields = []
  318. chars.each.with_index do |char, i|
  319. fields.push({
  320. name: "#{Emoji::NUMBERS[i]}: #{char.species}",
  321. value: "Created by <@#{char.user_id}>"
  322. })
  323. end
  324. Embed.new(
  325. title: "Which #{name}?",
  326. description: "Click on the corresponding reaction to pick",
  327. fields: fields
  328. )
  329. end
  330. def image_list_embed(character, event)
  331. # Find the author, if they're a member, or in DMs use the event's author
  332. if event.server
  333. member = character.user_id.match(/public/i) ? 'Public' :
  334. event.server.member(character.user_id)
  335. else
  336. member = event.author
  337. end
  338. # Grab an array of the character's images
  339. images = CharImage.where(char_id: character.id).
  340. map{ |i| "[#{i.keyword}](#{i.url})" }
  341. # Create Embed
  342. embed = Embed.new(
  343. title: character.name,
  344. description: images.join("\n"),
  345. color: character.type_color
  346. )
  347. # Add footer to embed
  348. author_footer(embed, member, [character.active, character.id])
  349. end
  350. def nsfw_char_embed(character, event)
  351. # Find the author, if they're a member,
  352. member = event.server.member(character.user_id)
  353. # Create Embed
  354. embed = Embed.new(
  355. title: character.name,
  356. color: character.type_color,
  357. fields: [{
  358. name: 'Wrong Channel!',
  359. value: 'The requested information contains NSFW content'
  360. }]
  361. )
  362. embed.thumbnail = nil
  363. # Apply appropriate footer to embed
  364. author_footer(embed, member, [character.active, character.rating, character.id])
  365. end