team_join_app.rb 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. check_votes(event, maj)
  8. rescue StandardError => e
  9. error_embed(e.message)
  10. end
  11. end
  12. def self.approve(event)
  13. # Save the app, character, and team
  14. app = event.message.embeds.first
  15. character = Character.find(app.footer.text.split(/\s?\|\s?/).last)
  16. team = Team.find_by!(channel: event.channel.id)
  17. # Add the character to the team
  18. team.join(character, event)
  19. member = event.server.member(character.user_id)
  20. member.add_role(team.role.to_i)
  21. # Welcome them to new team!
  22. reply = BotResponse.new(
  23. destination: team.channel,
  24. text: "Welcome #{character.name} to the team!"
  25. )
  26. event.message.delete
  27. reply
  28. end
  29. def self.deny(event)
  30. # Save the app, character, and team
  31. app = event.message.embeds.first
  32. character = Character.find(app.footer.text.split(/\s?\|\s?/).last)
  33. team = Team.find_by!(channel: event.channel.id)
  34. # Notify the requester of denial
  35. reply = BotResponse.new(
  36. destination: ENV['TEAM_CH'],
  37. text: "#{character.name}'s request to join #{team.name} has been denied"
  38. )
  39. event.message.delete
  40. reply
  41. end
  42. def self.majority(event)
  43. # Find the corresponding team
  44. team = Team.find_by!(channel: event.channel.id)
  45. # The total number of voters, divided by 2, +1
  46. (event.server.roles.find{ |r| r.id == team.role.to_i }.
  47. members.count / 2) + 1
  48. end
  49. end