team_join_app.rb 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. require './app/app_forms/app_form.rb'
  2. class TeamJoinApplication < ApplicationForm
  3. def self.process
  4. @process ||= Application.new('Team Join Request') do |event|
  5. # Calculate majority and check votes
  6. maj = majority(event)
  7. reactions = event.message.reactions
  8. # Check votes
  9. if reactions[Emoji::Y]&.count.to_i > maj
  10. approve(event)
  11. elsif reactions[Emoji::N]&.count.to_i > maj
  12. deny(event)
  13. end
  14. rescue StandardError => e
  15. error_embed(e.message)
  16. end
  17. end
  18. def self.approve(event)
  19. # Save the app, character, and team
  20. app = event.message.embeds.first
  21. character = Character.find(app.footer.text.split(/\s?\|\s?/).last)
  22. team = Team.find_by!(channel: event.channel.id)
  23. # Add the character to the team
  24. team.join(character, event)
  25. member = event.server.member(character.user_id)
  26. member.add_role(team.role.to_i)
  27. # Welcome them to new team!
  28. reply = BotResponse.new(
  29. destination: team.channel,
  30. text: "Welcome #{character.name} to the team!"
  31. )
  32. event.message.delete
  33. reply
  34. end
  35. def self.deny(event)
  36. # Save the app, character, and team
  37. app = event.message.embeds.first
  38. character = Character.find(app.footer.text.split(/\s?\|\s?/).last)
  39. team = Team.find_by!(channel: event.channel.id)
  40. # Notify the requester of denial
  41. reply = BotResponse.new(
  42. destination: ENV['TEAM_CH'],
  43. text: "#{character.name}'s request to join #{team.name} has been denied"
  44. )
  45. event.message.delete
  46. reply
  47. end
  48. def self.majority(event)
  49. # Find the corresponding team
  50. team = Team.find_by!(channel: event.channel.id)
  51. # The total number of voters, divided by 2, +1
  52. (event.server.roles.find{ |r| r.id == team.role.to_i }.
  53. members.count / 2) + 1
  54. end
  55. end