member.rb 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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 > 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. # Find image ID
  20. image = CharImage.where(keyword: 'Default')
  21. .find_by(char_id: carousel.char_id)
  22. # Transition into an ImageCarousel
  23. event.message.delete_all_reactions
  24. ImageCarousel.transition(event, carousel, image)
  25. when 'user'
  26. # Find User
  27. character = Character.find(carousel.char_id)
  28. user = User.find(character.user_id)
  29. # Transition into a UserCarousel
  30. event.message.delete_all_reactions
  31. UserCarousel.transition(event, carousel, user)
  32. when 'all', 'bags', 'family'
  33. # Fetch the corresponding emoji, and remove non-bot reactions
  34. emoji = sections.key(section)
  35. event.message.reacted_with(emoji).each do |r|
  36. event.message.delete_reaction(r.id, emoji) unless r.current_bot?
  37. end
  38. # Update character embed with new section
  39. BotResponse.new(
  40. carousel: carousel,
  41. embed: character_embed(
  42. character: Character.find(carousel.char_id),
  43. event: event,
  44. section: section
  45. )
  46. )
  47. end
  48. end
  49. def self.transition(event, carousel, character)
  50. # Update carousel to reflect new information
  51. carousel.update(
  52. char_id: character.id,
  53. image_id: nil,
  54. landmark_id: nil,
  55. options: nil
  56. )
  57. # Update reply
  58. BotResponse.new(
  59. carousel: carousel,
  60. reactions: sections.map{ |k,v| k }.push(Emoji::CROSS),
  61. embed: character_embed(
  62. character: character,
  63. event: event
  64. )
  65. )
  66. end
  67. end