user.rb 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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(user_id: user.id).order(:rating)
  18. # Handle nsfw chars in sfw channels
  19. active_chars = all_chars.filter{ |c| c.active == 'Active' }
  20. sfw_chars = active_chars.filter{ |c| c.rating != 'NSFW' }
  21. sfw = !event.channel.nsfw?
  22. chars = sfw ? sfw_chars : active_chars
  23. # Update carousel to reflect new information
  24. carousel.update(
  25. char_id: nil,
  26. image_id: nil,
  27. landmark_id: nil,
  28. options: chars.map{ |c| c.id }
  29. )
  30. # Array of section reactions and an X
  31. user_reactions = Emoji::NUMBERS.take(chars.length)
  32. user_reactions.push(Emoji::CROSS)
  33. # Update reply
  34. member = event.server.member(user.id)
  35. BotResponse.new(
  36. carousel: carousel,
  37. reactions: user_reactions,
  38. embed: user_char_embed(all_chars, member, sfw)
  39. )
  40. end
  41. end