member.rb 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. class CharacterCarousel < Carousel
  2. def self.sections
  3. {
  4. Emoji::EYES => 'all',
  5. Emoji::PICTURE => 'image',
  6. Emoji::BAGS => 'bags',
  7. Emoji::FAMILY => 'family',
  8. Emoji::BUST => 'user'
  9. }
  10. end
  11. def self.update_embed(event, carousel)
  12. # Save reactions and determine section
  13. reactions = event.message.reactions
  14. section = sections.filter{ |k,v| reactions[k]&.count.to_i > 1 }.values.first
  15. # Close if X is chosen
  16. return carousel.close(event) if reactions[Emoji::CROSS]&.count.to_i > 1
  17. case section
  18. when 'image'
  19. # Transition into an ImageCarousel
  20. event.message.delete_all_reactions
  21. ImageCarousel.transition(event, carousel, Character.find(carousel.char_id))
  22. when 'user'
  23. # Find User
  24. character = Character.find(carousel.char_id)
  25. user = User.find(character.user_id)
  26. # Transition into a UserCarousel
  27. event.message.delete_all_reactions
  28. UserCarousel.transition(event, carousel, user)
  29. when 'all', 'bags', 'family'
  30. # Fetch the corresponding emoji, and remove non-bot reactions
  31. emoji = sections.key(section)
  32. event.message.reacted_with(emoji).each do |r|
  33. event.message.delete_reaction(r.id, emoji) unless r.current_bot?
  34. end
  35. # Update character embed with new section
  36. BotResponse.new(
  37. carousel: carousel,
  38. embed: character_embed(
  39. character: Character.find(carousel.char_id),
  40. event: event,
  41. section: section
  42. )
  43. )
  44. end
  45. end
  46. def self.transition(event, carousel, character)
  47. # Update carousel to reflect new information
  48. carousel.update(
  49. char_id: character.id,
  50. image_id: nil,
  51. landmark_id: nil,
  52. options: nil
  53. )
  54. # Update reply
  55. BotResponse.new(
  56. carousel: carousel,
  57. reactions: sections.map{ |k,v| k }.push(Emoji::CROSS),
  58. embed: character_embed(
  59. character: character,
  60. event: event
  61. )
  62. )
  63. end
  64. end