fable.rb 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. require './app/commands/base_command.rb'
  2. class FableCommand < BaseCommand
  3. def self.opts
  4. {
  5. usage: {
  6. title: "Searches for the fable by title or keyword. If none is given, " +
  7. "R0ry will start with the first story"
  8. }
  9. }
  10. end
  11. def self.cmd
  12. desc = 'The book of Zaplanic Myths, Fables, and Legends'
  13. @cmd ||= Command.new(:fable, desc, opts) do |event, title|
  14. case title
  15. when /all/i
  16. when String
  17. # Search for Fable
  18. fable =
  19. Fable.find_by('title ilike ?', title) ||
  20. Fable.where('? ilike any(keywords)', title) if title
  21. raise 'Fable not found' if fable.empty?
  22. # Display
  23. #embed = fable.lenth > 1 ? fable_list(fable) : fable_embed(fable, event)
  24. BotResponse.new(
  25. embed: fable_embed(fable.first, event),
  26. carousel: fable.first,
  27. reactions: FableCarousel.sections.map{ |k,v| k }.push(Emoji::CROSS)
  28. )
  29. when nil
  30. # Display first Fable
  31. fable = Fable.first
  32. BotResponse.new(
  33. embed: fable_embed(fable, event),
  34. carousel: fable,
  35. reactions: FableCarousel.sections.map{ |k,v| k }.push(Emoji::CROSS)
  36. )
  37. end
  38. rescue ActiveRecord::RecordNotFound => e
  39. error_embed("Record Not Found!", e.message)
  40. rescue StandardError => e
  41. error_embed(e.message)
  42. end
  43. end
  44. def self.example_command(event=nil)
  45. case ['', 'title', 'keyword'].sample
  46. when ''
  47. []
  48. when 'title'
  49. [Fable.order('RANDOM()').first.name]
  50. when 'keyword'
  51. [Fable.order('RANDOM()').first.keywords.sample]
  52. end
  53. end
  54. end