user.rb 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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_user(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. journal_page: nil
  30. )
  31. # Array of section reactions and an X
  32. user_reactions = Emoji::NUMBERS.take(chars.length)
  33. user_reactions.push(Emoji::CROSS)
  34. # Update reply
  35. member = event.server.member(user.id)
  36. BotResponse.new(
  37. carousel: carousel,
  38. reactions: user_reactions,
  39. embed: user_char_embed(all_chars, member, event, sfw)
  40. )
  41. end
  42. def self.transition_character(event, carousel, character)
  43. # Character array
  44. chars = [ character ]
  45. chars.concat( Character.where(alt_form: character.id) )
  46. # Update carousel to reflect new information
  47. carousel.update(
  48. char_id: nil,
  49. image_id: nil,
  50. landmark_id: nil,
  51. options: chars.map{ |c| c.id },
  52. journal_page: nil
  53. )
  54. # Array of section reactions and an X
  55. user_reactions = Emoji::NUMBERS.take(chars.length)
  56. user_reactions.push(Emoji::CROSS)
  57. # Update reply
  58. BotResponse.new(
  59. carousel: carousel,
  60. reactions: user_reactions,
  61. embed: character_embed(character: character, event: event, section: 'forms')
  62. )
  63. end
  64. end