member.rb 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. bags: [ Emoji::BAGS, "View the character's inventory" ],
  10. family: [ Emoji::FAMILY, "View related characters" ],
  11. user: [ Emoji::BUST, "View the writer's other characters in a list" ]
  12. },
  13. # Usage has each option, in order with instructions, and a real example
  14. usage: {
  15. name:
  16. "Searches characters for the specified name, or discord user. " +
  17. "If no name is given, R0ry will show a list of all characters",
  18. section:
  19. "Skips to the specified section, some options include: bio, type, status, " +
  20. "rumors, image, bags. If no section is given, R0ry will default to history",
  21. keyword:
  22. "Displays a specific image, searched by its title, or keyword. " +
  23. "Can only be used if the section option is `image`",
  24. }
  25. }
  26. end
  27. def self.cmd
  28. desc = "Display info about the guild members"
  29. @cmd ||= Command.new(:member, desc, opts) do |event, name, section, keyword|
  30. # Determine display type: user, character, or list
  31. case name
  32. # Show user's character list
  33. when UID
  34. # Find User to display, and a list of their characters
  35. member = event.server.member(UID.match(name)[1])
  36. characters = Character.where(user_id: UID.match(name)[1])
  37. active_chars = characters.filter{ |c| c.active == 'Active' }
  38. # Handle sfw channels and nsfw characters
  39. sfw = !event.channel.nsfw?
  40. sfw_chars = active_chars.filter{ |c| c.rating == 'SFW' }
  41. chars = sfw ? sfw_chars : active_chars
  42. # Generate embed and reply
  43. BotResponse.new(
  44. embed: user_char_embed(characters, member, sfw),
  45. carousel: active_chars.map(&:id),
  46. reactions: Emoji::NUMBERS.take(chars.count).push(Emoji::CROSS)
  47. )
  48. # Show Character List Embed
  49. when nil
  50. # Grab list of active characters, and types
  51. characters = Character.where(active: 'Active').order(:name)
  52. types = Type.all
  53. # Create reaction list
  54. reactions = Emoji::NUMBERS.take(4)
  55. # Generate embed, and reply
  56. BotResponse.new(
  57. embed: char_list_embed(characters, 'active', types),
  58. reactions: reactions.push(Emoji::CROSS),
  59. carousel: 'Guild'
  60. )
  61. # Show character embed
  62. when Integer
  63. # Find Character by ID and generate embed
  64. character = Character.find(name)
  65. char_reply(event, character, section, keyword)
  66. else
  67. # Find Character by name and generate embed
  68. character = Character.where('name ilike ?', name)
  69. raise 'Character not found!' if character.empty?
  70. char_reply(event, character, section, keyword)
  71. end
  72. rescue ActiveRecord::RecordNotFound => e
  73. error_embed("Record Not Found!", e.message)
  74. rescue StandardError => e
  75. error_embed(e.message)
  76. end
  77. end
  78. def self.char_reply(event, character, section, keyword)
  79. # Current channel restricted?
  80. sfw = !event.channel.nsfw?
  81. # Determine if duplicate characters, then filter NSFW if SFW channel
  82. if character.count > 1 && sfw
  83. chars = character.filter{ |c| c.rating == 'SFW' || c.rating == 'Hidden' } if sfw
  84. # If still more than 1 character, reply with duplicate embed
  85. if chars.length > 1
  86. embed = dup_char_embed(chars, chars.first.name)
  87. return BotResponse.new(
  88. embed: embed,
  89. reactions: Emoji::NUMBERS.take(chars.count)
  90. )
  91. end
  92. end
  93. character = character.first
  94. # Find image if specified
  95. image = CharImage.where(char_id: character.id).
  96. find_by('keyword ilike ?', keyword || 'Default')
  97. # Ensure the content is appropriate for the current channel
  98. if sfw && ( image&.category == 'NSFW' || character.rating == 'NSFW' )
  99. return nsfw_char_embed(character, event)
  100. end
  101. # Generate Character Embed
  102. embed = character_embed(
  103. character: character,
  104. event: event,
  105. section: section,
  106. image: image
  107. )
  108. # Determine Carousel Type and create reply
  109. if section&.match(/images?/i)
  110. BotResponse.new(
  111. embed: embed,
  112. carousel: image,
  113. reactions: ImageCarousel.sections.map{ |k,v| k }.push(Emoji::CROSS)
  114. )
  115. else
  116. BotResponse.new(
  117. embed: embed,
  118. carousel: character,
  119. reactions: CharacterCarousel.sections.map{ |k,v| k }.push(Emoji::CROSS)
  120. )
  121. end
  122. end
  123. def self.example_command(event=nil)
  124. sections = ['all', 'bio', 'type', 'status', 'rumors', 'image', 'bags']
  125. case ['', 'user', 'name', 'section', 'keyword'].sample
  126. when ''
  127. []
  128. when 'user'
  129. user = Character.where(active: 'Active').order('RANDOM()').first.user_id
  130. member = event&.server&.member(user)
  131. ["@#{member&.nickname || member&.name || 'user_name'}"]
  132. when 'name'
  133. [Character.where.not(active: 'Deleted').order('RANDOM()').first.name]
  134. when 'section'
  135. [Character.where.not(active: 'Deleted').order('RANDOM()').first.name,
  136. sections.sample]
  137. when 'keyword'
  138. i = CharImage.where.not(keyword: 'Default').order('RANDOM()').first
  139. [Character.find(i.char_id).name, 'image', i.keyword]
  140. end
  141. end
  142. end