user.rb 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. class UserCarousel < Carousel
  2. def self.update_embed(event, carousel)
  3. # Save reactions and determine section
  4. reactions = event.message.reactions
  5. reaction = Emoji::NUMBERS.filter{ |n| reactions[n]&.count.to_i > 1 }.first
  6. char_index = Emoji::NUMBERS.index(reaction)
  7. # Close if X is chosen
  8. return carousel.close(event) if reactions[Emoji::CROSS]&.count.to_i > 1
  9. # Find character
  10. character = Character.find(carousel.options[char_index])
  11. # Transition into a CharacterCarousel
  12. event.message.delete_all_reactions
  13. CharacterCarousel.transition(event, carousel, character)
  14. end
  15. def self.transition(event, carousel, user)
  16. # Character array
  17. all_chars = Character.where(active: 'Active', user_id: user.id).order(:rating)
  18. sfw_chars = all_chars.filter{ |c| c.rating == 'SFW' }
  19. chars = event.channel.nsfw? ? all_chars : sfw_chars
  20. # Update carousel to reflect new information
  21. carousel.update(
  22. char_id: nil,
  23. image_id: nil,
  24. landmark_id: nil,
  25. options: chars.map{ |c| c.id }
  26. )
  27. # Array of section reactions and an X
  28. user_reactions = Emoji::NUMBERS.take(chars.length)
  29. user_reactions.push(Emoji::CROSS)
  30. # Update reply
  31. member = event.server.member(user.id)
  32. BotResponse.new(
  33. carousel: carousel,
  34. reactions: user_reactions,
  35. embed: user_char_embed(chars, member)
  36. )
  37. end
  38. end