fable_app.rb 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. require './app/app_forms/app_form.rb'
  2. class FableApplication < ApplicationForm
  3. def self.process
  4. @process||= Application.new('Fable Application') do |event|
  5. # Calculate majority
  6. maj = majority(event)
  7. # Check votes
  8. reactions = event.message.reactions
  9. if reactions[Emoji::Y]&.count.to_i > maj && star(event)
  10. approve(event)
  11. elsif reactions[Emoji::N]&.count.to_i > maj
  12. deny(event)
  13. elsif reactions[Emoji::CRAYON]&.count.to_i > 1
  14. edit(event)
  15. elsif reactions[Emoji::CROSS]&.count.to_i > 1
  16. remove(event)
  17. end
  18. end
  19. end
  20. def self.approve(event)
  21. # Save the application
  22. app = event.message.embeds.first
  23. # Save fable
  24. fable = FableController.edit_fable(app)
  25. reply = BotResponse.new(
  26. destination: ENV['FABLE_CH'],
  27. text: "Good News, <@#{fable.user_id}>! Your fable was published!",
  28. embed: fable_embed(fable: fable, event: event)
  29. )
  30. event.message.delete
  31. reply
  32. end
  33. def self.deny(event)
  34. reply = BotResponse.new(
  35. embed: reject_app(event.message.embeds.first, :fable),
  36. reactions: FableApp::REJECT_MESSAGES.map{ |k,v| k }.push(Emoji::CHECK)
  37. )
  38. # Delete message and reply
  39. event.message.delete
  40. reply
  41. end
  42. def self.edit(event)
  43. # Save the application
  44. app = event.message.embeds.first
  45. reply = BotResponse.new(
  46. destination: event.channel.id,
  47. embed: self_edit_embed(app.footer.text, Url::FABLE),
  48. timer: 35
  49. )
  50. # Delete app and reply
  51. event.message.delete
  52. reply
  53. end
  54. end