character_controller.rb 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  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. # Count the active characters, and subtract from their allowed max
  24. chars = Character.where(active: 'Active', user_id: user.id).count
  25. if user.allowed_chars(member) - chars > 0
  26. true
  27. else
  28. false
  29. end
  30. rescue ActiveRecord::RecordNotFound => e
  31. error_embed("Record not Found!", e.message)
  32. rescue StandardError => e
  33. error_embed(e.message)
  34. end
  35. end