member.rb 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. require './app/commands/base_command.rb'
  2. class MemberCommand < BaseCommand
  3. def self.opts
  4. {
  5. # Nav consists of reaction sections and descriptions
  6. nav: {
  7. all: [ Emoji::EYES, "View all info about the character" ],
  8. image: [ Emoji::PICTURE, "Scroll though the character's images" ],
  9. journal: [ Emoji::NOTEBOOK, "Scroll though pages of journal entries" ],
  10. bags: [ Emoji::BAGS, "View the character's inventory" ],
  11. family: [ Emoji::FAMILY, "View related characters" ],
  12. user: [ Emoji::BUST, "View the writer's other characters in a list" ]
  13. },
  14. # Usage has each option, in order with instructions, and a real example
  15. usage: {
  16. name:
  17. "Searches characters for the specified name, or discord user. " +
  18. "If no name is given, R0ry will show a list of all characters",
  19. section:
  20. "Skips to the specified section, some options include: bio, type, status, " +
  21. "rumors, image, bags. If no section is given, R0ry will default to history",
  22. keyword:
  23. "Displays a specific image or journal, searched by its title, or keyword. " +
  24. "Can only be used if the section option is `image` or `journal`",
  25. }
  26. }
  27. end
  28. def self.cmd
  29. desc = "Display info about the guild members"
  30. @cmd ||= Command.new(:member, desc, opts) do |event, name, section, keyword|
  31. # Determine display type: user, character, or list
  32. case name
  33. # Show user's character list
  34. when UID
  35. # Find User to display, and a list of their characters
  36. member = event.server.member(UID.match(name)[1])
  37. characters = Character.where(user_id: UID.match(name)[1])
  38. active_chars = characters.filter{ |c| c.active == 'Active' }
  39. # Handle sfw channels and nsfw characters
  40. sfw = !event.channel.nsfw?
  41. sfw_chars = active_chars.filter{ |c| c.rating != 'NSFW' }
  42. chars = sfw ? sfw_chars : active_chars
  43. # Generate embed and reply
  44. BotResponse.new(
  45. embed: user_char_embed(characters, member, sfw),
  46. carousel: active_chars.map(&:id),
  47. reactions: Emoji::NUMBERS.take(chars.count).push(Emoji::CROSS)
  48. )
  49. # Show Character List Embed
  50. when nil
  51. # Grab list of active characters, and types
  52. characters = Character.where(active: 'Active').order(:name)
  53. types = Type.all
  54. # Create reaction list
  55. reactions = Emoji::NUMBERS.take(4)
  56. # Generate embed, and reply
  57. BotResponse.new(
  58. embed: char_list_embed(characters, 'active', types),
  59. reactions: reactions.push(Emoji::CROSS),
  60. carousel: 'Guild'
  61. )
  62. # Show character embed
  63. else
  64. # Find Character
  65. if name.to_i > 0
  66. character = Character.find(name)
  67. elsif section&.match(/deleted?/i)
  68. character = Character.where(active: 'Deleted')
  69. .where('name ilike ?', name)
  70. else
  71. character = Character.where.not(active: 'Deleted')
  72. .where('name ilike ?', name)
  73. .or(Character.where.not(active: 'Deleted')
  74. .where('? = any(aliases)', name))
  75. raise 'Character not found!' if character.empty?
  76. end
  77. char_reply(event, character, section, keyword)
  78. end
  79. rescue ActiveRecord::RecordNotFound => e
  80. error_embed("Record Not Found!", e.message)
  81. #rescue StandardError => e
  82. #error_embed(e.message)
  83. end
  84. end
  85. def self.char_reply(event, character, section, keyword)
  86. # Current channel restricted?
  87. sfw = !event.channel.nsfw?
  88. # Determine if duplicate characters, then filter NSFW if SFW channel
  89. unless character.is_a? Character
  90. chars = sfw ? character.filter{ |c| c.rating != 'NSFW' } : character
  91. # If still more than 1 character, reply with duplicate embed
  92. if chars.length > 1
  93. embed = dup_char_embed(chars, chars.first.name)
  94. return BotResponse.new(
  95. embed: embed,
  96. reactions: Emoji::NUMBERS.take(chars.count),
  97. carousel: chars.map(&:id)
  98. )
  99. elsif chars.length == 0
  100. nsfw_char_embed(chars.first, event)
  101. else
  102. character = character.first
  103. end
  104. end
  105. case section
  106. when /image/i
  107. # Find image if specified
  108. image = CharImage.where(char_id: character.id).
  109. find_by('keyword ilike ?', keyword || 'Default')
  110. raise 'Image not found!' if keyword && !image
  111. when /journal/i
  112. # Find journal if specified
  113. if keyword
  114. journal = JournalEntry.where(char_id: character.id).
  115. find_by('title ilike ?', keyword)
  116. raise 'Journal not found!' if !journal
  117. else
  118. # Fetch Journal list if no keyword
  119. journal = JournalEntry.where(char_id: character.id).take(10)
  120. end
  121. end
  122. # Ensure the content is appropriate for the current channel
  123. if sfw && ( image&.category == 'NSFW' || character.rating == 'NSFW' )
  124. return nsfw_char_embed(character, event)
  125. end
  126. # Generate Character Embed
  127. embed = character_embed(
  128. character: character,
  129. event: event,
  130. section: section,
  131. image: image,
  132. journal: journal
  133. )
  134. # Determine Carousel Type and create reply
  135. if section&.match(/images?/i)
  136. BotResponse.new(
  137. embed: embed,
  138. carousel: image,
  139. reactions: ImageCarousel.sections.map{ |k,v| k }.push(Emoji::CROSS)
  140. )
  141. elsif section&.match(/journal/i)
  142. journal = journal.first unless journal.is_a? JournalEntry
  143. BotResponse.new(
  144. embed: embed,
  145. carousel: journal,
  146. reactions: JournalCarousel.sections.map{ |k,v| k }.push(Emoji::CROSS)
  147. )
  148. elsif section&.match(/(alt(ernate)?)?\s?forms?/i)
  149. chars = []
  150. if character.alt_form
  151. chars.push( Character.find(character.alt_form) )
  152. else
  153. chars.push(character)
  154. end
  155. # Add forms
  156. chars.concat( Character.where(alt_form: chars.first.id) )
  157. BotResponse.new(
  158. embed: embed,
  159. carousel: chars.map{ |c| c.id },
  160. reactions: Emoji::NUMBERS.take(chars.length).push(Emoji::CROSS)
  161. )
  162. else
  163. BotResponse.new(
  164. embed: embed,
  165. carousel: character,
  166. reactions: CharacterCarousel.sections.map{ |k,v| k }.push(Emoji::CROSS)
  167. )
  168. end
  169. end
  170. def self.example_command(event=nil)
  171. sections = ['all', 'bio', 'type', 'status', 'rumors', 'image', 'bags', 'journal']
  172. case ['', 'user', 'name', 'section', 'keyword'].sample
  173. when ''
  174. []
  175. when 'user'
  176. user = Character.where(active: 'Active').order('RANDOM()').first.user_id
  177. member = event&.server&.member(user)
  178. ["@#{member&.nickname || member&.name || 'user_name'}"]
  179. when 'name'
  180. [Character.where.not(active: 'Deleted').order('RANDOM()').first.name]
  181. when 'section'
  182. [Character.where.not(active: 'Deleted').order('RANDOM()').first.name,
  183. sections.sample]
  184. when 'keyword'
  185. i = CharImage.where.not(keyword: 'Default').order('RANDOM()').first
  186. j = JournalEntry.order('RANDOM()').first
  187. [[Character.find(i.char_id).name, 'image', i.keyword],
  188. [Character.find(j.char_id).name, 'journal', j.title || j.date]].sample
  189. end
  190. end
  191. end