member.rb 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. raise 'Character not found!' if character.empty?
  74. end
  75. char_reply(event, character, section, keyword)
  76. end
  77. rescue ActiveRecord::RecordNotFound => e
  78. error_embed("Record Not Found!", e.message)
  79. #rescue StandardError => e
  80. #error_embed(e.message)
  81. end
  82. end
  83. def self.char_reply(event, character, section, keyword)
  84. # Current channel restricted?
  85. sfw = !event.channel.nsfw?
  86. # Determine if duplicate characters, then filter NSFW if SFW channel
  87. unless character.is_a? Character
  88. chars = sfw ? character.filter{ |c| c.rating != 'NSFW' } : character
  89. # If still more than 1 character, reply with duplicate embed
  90. if chars.length > 1
  91. embed = dup_char_embed(chars, chars.first.name)
  92. return BotResponse.new(
  93. embed: embed,
  94. reactions: Emoji::NUMBERS.take(chars.count),
  95. carousel: chars.map(&:id)
  96. )
  97. elsif chars.length == 0
  98. nsfw_char_embed(chars.first, event)
  99. else
  100. character = character.first
  101. end
  102. end
  103. case section
  104. when /image/i
  105. # Find image if specified
  106. image = CharImage.where(char_id: character.id).
  107. find_by('keyword ilike ?', keyword || 'Default')
  108. raise 'Image not found!' if keyword && !image
  109. when /journal/i
  110. # Find journal if specified
  111. if keyword
  112. journal = JournalEntry.where(char_id: character.id).
  113. find_by('title ilike ?', keyword)
  114. raise 'Journal not found!' if !journal
  115. else
  116. # Fetch Journal list if no keyword
  117. journal = JournalEntry.where(char_id: character.id).take(10)
  118. end
  119. end
  120. # Ensure the content is appropriate for the current channel
  121. if sfw && ( image&.category == 'NSFW' || character.rating == 'NSFW' )
  122. return nsfw_char_embed(character, event)
  123. end
  124. # Generate Character Embed
  125. embed = character_embed(
  126. character: character,
  127. event: event,
  128. section: section,
  129. image: image,
  130. journal: journal
  131. )
  132. # Determine Carousel Type and create reply
  133. if section&.match(/images?/i)
  134. BotResponse.new(
  135. embed: embed,
  136. carousel: image,
  137. reactions: ImageCarousel.sections.map{ |k,v| k }.push(Emoji::CROSS)
  138. )
  139. elsif section&.match(/journal/i)
  140. journal = journal.first unless journal.is_a? JournalEntry
  141. BotResponse.new(
  142. embed: embed,
  143. carousel: journal,
  144. reactions: JournalCarousel.sections.map{ |k,v| k }.push(Emoji::CROSS)
  145. )
  146. else
  147. BotResponse.new(
  148. embed: embed,
  149. carousel: character,
  150. reactions: CharacterCarousel.sections.map{ |k,v| k }.push(Emoji::CROSS)
  151. )
  152. end
  153. end
  154. def self.example_command(event=nil)
  155. sections = ['all', 'bio', 'type', 'status', 'rumors', 'image', 'bags', 'journal']
  156. case ['', 'user', 'name', 'section', 'keyword'].sample
  157. when ''
  158. []
  159. when 'user'
  160. user = Character.where(active: 'Active').order('RANDOM()').first.user_id
  161. member = event&.server&.member(user)
  162. ["@#{member&.nickname || member&.name || 'user_name'}"]
  163. when 'name'
  164. [Character.where.not(active: 'Deleted').order('RANDOM()').first.name]
  165. when 'section'
  166. [Character.where.not(active: 'Deleted').order('RANDOM()').first.name,
  167. sections.sample]
  168. when 'keyword'
  169. i = CharImage.where.not(keyword: 'Default').order('RANDOM()').first
  170. j = JournalEntry.order('RANDOM()').first
  171. [[Character.find(i.char_id).name, 'image', i.keyword],
  172. [Character.find(j.char_id).name, 'journal', j.title || j.date]].sample
  173. end
  174. end
  175. end