fable.rb 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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. if title
  19. fable =
  20. Fable.find_by('title ilike ?', title) ||
  21. Fable.where('? ilike any(keywords)', title)
  22. end
  23. raise 'Fable not found' if fable.empty?
  24. # Display
  25. # embed = fable.lenth > 1 ? fable_list(fable) : fable_embed(fable, event)
  26. BotResponse.new(
  27. embed: fable_embed(fable.first, event),
  28. carousel: fable.first,
  29. reactions: FableCarousel.sections.map { |k, _v| k }.push(Emoji::CROSS)
  30. )
  31. when nil
  32. # Display first Fable
  33. fable = Fable.first
  34. BotResponse.new(
  35. embed: fable_embed(fable, event),
  36. carousel: fable,
  37. reactions: FableCarousel.sections.map { |k, _v| k }.push(Emoji::CROSS)
  38. )
  39. end
  40. rescue ActiveRecord::RecordNotFound => e
  41. error_embed('Record Not Found!', e.message)
  42. rescue StandardError => e
  43. error_embed(e.message)
  44. end
  45. end
  46. def self.example_command(_event=nil)
  47. case ['', 'title', 'keyword'].sample
  48. when ''
  49. []
  50. when 'title'
  51. [Fable.order('RANDOM()').first.name]
  52. when 'keyword'
  53. [Fable.order('RANDOM()').first.keywords.sample]
  54. end
  55. end
  56. end