team_join_app.rb 1.8 KB

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