fable.rb 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. class FableCarousel < Carousel
  2. def self.sections
  3. {
  4. Emoji::FIRST => 'first',
  5. Emoji::LEFT => 'left',
  6. Emoji::RIGHT => 'right',
  7. Emoji::LAST => 'last'
  8. }
  9. end
  10. def self.update_embed(event, carousel)
  11. # Save reactions and determine section
  12. reactions = event.message.reactions
  13. section = sections.filter{ |k,v| reactions[k]&.count.to_i > 1 }.values.first
  14. # Close the embed if that is chosen
  15. return carousel.close(event) if reactions[Emoji::CROSS]&.count.to_i > 1
  16. # Fetch the corresponding emoji, and remove non-bot reactions
  17. emoji = sections.key(section)
  18. event.message.reacted_with(emoji).each do |r|
  19. event.message.delete_reaction(r.id, emoji) unless r.current_bot?
  20. end
  21. # Find next fable
  22. fable = next_fable(section, carousel)
  23. # Update to new fable
  24. BotResponse.new(
  25. carousel: carousel,
  26. embed: fable_embed(fable, event)
  27. )
  28. end
  29. def self.next_fable(section, carousel)
  30. case section
  31. when 'first'
  32. Fable.order('id DESC').first
  33. when 'left'
  34. Fable.where('id < ?', carousel.fable_id).order('id DESC').first ||
  35. Fable.order('id DESC').first
  36. when 'right'
  37. Fable.where('id > ?', carousel.fable_id).order('id ASC').first ||
  38. Fable.order('id ASC').first
  39. when 'last'
  40. Fable.order('id ASC').first
  41. end
  42. end
  43. end