character_controller.rb 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. class CharacterController
  2. def self.edit_character(params)
  3. char_hash = Character.from_form(params)
  4. if character = Character.find_by(edit_url: char_hash["edit_url"])
  5. character.update(char_hash)
  6. character.reload
  7. else
  8. character = Character.create(char_hash)
  9. end
  10. character
  11. end
  12. def self.authenticate_char_app(event)
  13. # Save application embed
  14. app = event.message.embeds.first
  15. # Nothing to check if the character is public, already active, or not a PC
  16. return true if app.description.match(/(public|server)/i) ||
  17. Character.find_by(edit_url: app.footer.text)&.active == 'Active' ||
  18. !app.title.match('Personal Character')
  19. # Save the user id, and find the db user, and discord member
  20. user_id = UID.match(app.description)
  21. user = User.find(user_id[1])
  22. member = event.server.member(user_id[1])
  23. # Error, if member cannot be found
  24. if member.nil?
  25. event.message.react(Emoji::WHAT)
  26. return 'invalid'
  27. end
  28. # Count the active characters, and subtract from their allowed max
  29. chars = Character.where(active: 'Active', user_id: user.id).count
  30. if user.allowed_chars(member) - chars > 0
  31. true
  32. else
  33. false
  34. end
  35. end
  36. end