Browse Source

continue code cleanup, fix rejection to reflect form changes

Kylie Jo Swistak 6 years ago
parent
commit
7bebc67ce0

+ 14 - 0
app/controllers/character_controller.rb

@@ -0,0 +1,14 @@
+class CharacterController
+  def self.edit_character(params)
+    char_hash = Character.from_form(params)
+
+    if char = Character.find_by(edit_url: char_hash["edit_url"])
+      char.update!(char_hash)
+      character = Character.find_by(edit_url: char_hash["edit_url"])
+    else
+      character = Character.create(char_hash)
+    end
+
+    character
+  end
+end

+ 11 - 0
app/controllers/image_controller.rb

@@ -0,0 +1,11 @@
+class ImageController
+  def self.edit_images(content, char_id)
+    if image_url = /\*\*URL to the Character\'s Appearance\*\*\:\s(.*)/.match(content)
+      unless CharImages.where(char_id: char_id).find_by(url: image_url[1])
+        image = CharImages.create(char_id: char_id, url: image_url[1], category: 'SFW', keyword: 'Primary')
+      end
+    end
+
+    image
+  end
+end

+ 24 - 0
app/models/char_app_responses.rb

@@ -0,0 +1,24 @@
+# frozen_string_literal: true
+
+module CharAppResponses
+  GRAMMAR = "Please check your grammar and\ncapitalization"
+  UNITS = "Please specify your units in\nImperial or Metric"
+  IMAGE = "Your image is inappropriate for\ndefault use"
+  LORE = "One or more responses are\nconflicting with server lore"
+  UNDER_AGE = "Your age conflicts with the\nspecified rating"
+  INVALID = "One or more responses are\ninvalid"
+  VULGAR = "Your application is too vulgar,\nor conflicts with server rules"
+  DM_NOTES = "Please elaborate on your\nDM Notes"
+  INLINE_SPACE = "------------------------------------"
+
+  REJECT_MESSAGES = {
+    Emoji::SPEECH_BUBBLE => GRAMMAR,
+    Emoji::SCALE => UNITS,
+    Emoji::PICTURE => IMAGE,
+    Emoji::BOOKS => LORE,
+    Emoji::BABY => UNDER_AGE,
+    Emoji::SKULL => INVALID,
+    Emoji::VULGAR => VULGAR,
+    Emoji::NOTE => DM_NOTES
+  }
+end

+ 27 - 0
app/models/characters.rb

@@ -77,4 +77,31 @@ class Character < ActiveRecord::Base
     hash = hash.reject { |k,v| k == nil }
     hash
   end
+
+  def self.check_user(event)
+    content = event.message.content
+    edit_url = /Edit\sKey\s\(ignore\):\s([\s\S]*)/.match(content)
+    active = /\_New\sCharacter\sApplication\_:\s(.*)/.match(content)
+    user_id = /<@([0-9]+)>/.match(content)
+
+    user = User.find_by(id: user_id[1])
+    member = event.server.member(user_id[1])
+    if user && member
+
+      if active[1] == "Personal Character"
+        allowed_characters = (user.level / 10 + 1)
+        characters = Character.where(user_id: user_id[1]).where(active: "Active").count
+
+        if characters < allowed_characters && characters < 6
+          approval_react(event)
+        else
+          too_many(event, member, edit_url, 'characters')
+        end
+      else
+        approval_react(event)
+      end
+    else
+      unknown_member(event)
+    end
+  end
 end

+ 19 - 0
app/responses/application_responses.rb

@@ -0,0 +1,19 @@
+require_relative '../../lib/emoji.rb'
+
+def approval_react(event)
+  event.message.react(Emoji::Y)
+  event.message.react(Emoji::N)
+end
+
+def too_many(event, user, edit_url, model)
+  event.server.member(user).dm("You have too many #{model}!\nPlease deactivate and try again #{edit_url[1]}")
+  event.message.delete
+end
+
+def unknown_member(event)
+  content = event.message.content
+  content += "\n\n **_I DONT KNOW THIS APPLICANT_**"
+
+  event.message.delete
+  event.respond(content)
+end

+ 21 - 0
app/responses/reject.rb

@@ -0,0 +1,21 @@
+def reject_char_embed(app)
+  image_url = /\*\*URL to the Character\'s Appearance\*\*\:\s(.*)/.match(app)
+
+  fields = []
+
+  CharAppResponses::REJECT_MESSAGES.map do |emoji, message|
+    fields.push({ name: emoji, value: "#{message}\n#{CharAppResponses::INLINE_SPACE}", inline: true })
+  end
+
+  fields.push({ name: "Submitting", value: "#{Emoji::CHECK} : Indicates you are ready to send the corresponding messages to the user\n#{Emoji::CROSS} : Indicates you want to dismiss this message and not send a message to the user\n#{Emoji::CRAYON} : Indicates you want to edit the users form for them, and resubmit on their behalf" })
+
+  Embed.new(
+    title: "**_APPLICATION REJECTED_**",
+    description: "Please indicate what message to forward to the user!",
+    color: "#a41e1f",
+    thumbnail: {
+      url: image_url[1]
+    },
+    fields: fields
+  )
+end

+ 41 - 88
bot.rb

@@ -26,6 +26,8 @@ HAP_ROTOM = "https://static.pokemonpets.com/images/monsters-images-800-800/479-R
 # URLs
 APP_FORM = "https://docs.google.com/forms/d/e/1FAIpQLSfryXixX3aKBNQxZT8xOfWzuF02emkJbqJ1mbMGxZkwCvsjyA/viewform"
 
+# Regexes
+
 # ---
 
 Dotenv.load if BOT_ENV != 'production'
@@ -45,58 +47,13 @@ ActiveRecord::Base.establish_connection(
 )
 
 Dir['app/**/*.rb'].each { |f| require File.join(File.expand_path(__dir__), f) }
-Dir["/lib/*.rb"].each {|file| require file }
+Dir['/lib/*.rb'].each { |f| require f }
 
 
 token = ENV['DISCORD_BOT_TOKEN']
 bot = Discordrb::Bot.new(token: token)
 
 # Methods: define basic methods here
-
-def check_user(event)
-  content = event.message.content
-  edit_url = /Edit\sKey\s\(ignore\):\s([\s\S]*)/.match(content)
-
-  if user_id = (/<@([0-9]+)>/.match(content))
-    user = User.find_by(id: user_id[1])
-
-    allowed_characters = (user.level / 10 + 1)
-    characters = Character.where(user_id: user_id[1]).count
-
-    if characters < allowed_characters && characters < 6
-      event.message.react(Emoji::Y)
-      event.message.react(Emoji::N)
-    else
-      event.server.member(user_id[1]).dm("You have too many characters!\nPlease deactivate and try again #{edit_url[1]}")
-      event.message.delete
-    end
-  else
-    event.message.edit("#{content}\n\nI don't know this user!")
-  end
-end
-
-def edit_character(message, member)
-  params = message.split("\n")
-  char_hash = Character.from_form(params)
-  image_url = /\*\*URL to the Character\'s Appearance\*\*\:\s(.*)/.match(message)
-
-  if char = Character.find_by(edit_url: char_hash["edit_url"])
-    char.update!(char_hash)
-    character = Character.find_by(edit_url: char_hash["edit_url"])
-  else
-    character = Character.create(char_hash)
-  end
-
-  edit_images(image_url[1], character.id, 'sfw', 'primary') if image_url[1]
-  character_embed(character, image_url[1], member)
-end
-
-def edit_images(image_url, character_id, category, keyword)
-  unless CharImages.where(char_id: character_id).find_by(url: image_url)
-    CharImages.create(char_id: character_id, url: image_url, category: category, keyword: keyword)
-  end
-end
-
 # ---
 
 # Commands: structure basic bot commands here
@@ -178,7 +135,7 @@ bot.message do |event|
   end
 
   if event.author.id == APP_BOT
-    check_user(event)
+    Character.check_user(event)
   end
 
 end
@@ -192,10 +149,14 @@ bot.reaction_add do |event|
     maj = 1
 
     if event.message.reacted_with(Emoji::Y).count > maj
+      params = content.split("\n")
       uid = /<@([0-9]+)>/.match(content)
       member = event.server.member(uid[1])
 
-      embed = edit_character(content, member)
+      character = CharacterController.edit_character(params)
+      image_url = ImageController.edit_images(content, character.id)
+
+      embed = character_embed(character, image_url, member)
 
       if embed
         event.message.delete
@@ -210,40 +171,31 @@ bot.reaction_add do |event|
         event.respond("Something went wrong")
       end
     elsif event.message.reacted_with(Emoji::N).count > maj
-      message = event.message.content
-      split_message = message.split("\n")
-
-      i = 0
-      split_message.each do |row|
-        if row.match(/\*\*/)
-          if row.match(/>>>/)
-            row.insert 5, "#{Emoji::ALL[i]} "
-            i += 1
-          else
-            row.insert 0, "#{Emoji::ALL[i]} "
-            i += 1
-          end
-        end
-      end
-
-      edited_message = split_message.join("\n")
-      new_message = "**_APPLICATION REJECTED!!_**\n--------------\n\n#{edited_message}\n\n\nPlease indicate what needs to be updated with the corresponding reactions!\nWhen you're done hit #{Emoji::CHECK}, or to dismiss hit #{Emoji::CROSS}"
+      edit_url = /Edit\sKey\s\(ignore\):\s([\s\S]*)/.match(content)
+      user_id = /<@([0-9]+)>/.match(content)
 
-      event.message.delete
-      rejected = event.respond(new_message)
+      new_message = "#{user_id[1]}:#{edit_url[1]}"
+      embed = reject_char_embed(content)
 
-      j = 0
-      i.times do
-        rejected.react(Emoji::ALL[j])
-        j += 1
-      end
+      #event.message.delete
+      rejected = event.send_embed(content, embed)
 
+      rejected.react(Emoji::SPEECH_BUBBLE)
+      rejected.react(Emoji::SCALE)
+      rejected.react(Emoji::PICTURE)
+      rejected.react(Emoji::BOOKS)
+      rejected.react(Emoji::BABY)
+      rejected.react(Emoji::SKULL)
+      rejected.react(Emoji::VULGAR)
+      rejected.react(Emoji::NOTE)
       rejected.react(Emoji::CHECK)
       rejected.react(Emoji::CROSS)
+      rejected.react(Emoji::CRAYON)
+
     end
   end
 
-  if event.message.from_bot? && content.match(/\*\*\_APPLICATION\sREJECTED\!\!\_\*\*/)
+  if event.message.from_bot? && content.match(/\_New\sCharacter\sApplication\_/)
     if event.message.reacted_with(Emoji::CHECK).count > 1
       reactions = event.message.reactions
 
@@ -251,29 +203,30 @@ bot.reaction_add do |event|
       user_id = /<@([0-9]+)>/.match(content)
       member = event.server.member(user_id[1])
 
-      message = "Your application has been rejected!\nPlease fix the following lines, and resubmit here:\n#{APP_FORM}#{edit_url[1]}"
-      rows = reactions.count - 2
-      i = 0
+      message = "Your application has been rejected!"
 
-      rows.times do
-        if reactions[Emoji::ALL[i]].count > 1
-          row = /#{Emoji::ALL[i]}\s(.*)/.match(content)
-          message += "\n> #{row[1]}"
+      Emoji::APP_SECTIONS.each do |reaction|
+        if reactions[reaction].count > 1
+          m = CharAppResponses::REJECT_MESSAGES[reaction].gsub("\n", " ")
+          message += "\n#{m}"
         end
-
-        i += 1
       end
 
-      temp_message = "Your application has been rejected!\nPlease fix the following lines, and resubmit here:\n[users url goes here]"
-      message = "Your application has been rejected!\nPlease fix the following lines, and resubmit here:\n#{APP_FORM}#{edit_url[1]}"
-
-      event.message.delete
-      event.send_temporary_message(temp_message, 15)
+      #event.message.delete
+      event.send_temporary_message(message, 25)
 
+      message += "\n\nYou may edit your application and resubmit here:\n#{APP_FORM}#{edit_url[1]}"
       member.dm(message)
 
     elsif event.message.reacted_with(Emoji::CROSS).count > 1
       event.message.delete
+    elsif event.message.reacted_with(Emoji::CRAYON).count > 1
+      edit_url = /Edit\sKey\s\(ignore\):\s([\s\S]*)/.match(content)
+
+      message = "Please edit the users application and resubmit!\n#{APP_FORM}#{edit_url[1]}"
+
+      #event.message.delete
+      event.send_temporary_message(message, 25)
     end
   end
 end

+ 11 - 1
lib/emoji.rb

@@ -29,6 +29,16 @@ module Emoji
   Z = "🇿"
   CHECK = "✅"
   CROSS = "❌"
+  SPEECH_BUBBLE = "💬"
+  SCALE = "⚖"
+  PICTURE = "🖼"
+  BOOKS = "📚"
+  NOTE = "🗒"
+  SKULL = "☠"
+  BABY = "👶"
+  VULGAR = "🖕"
+  CRAYON = "🖍"
 
-  ALL = [A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z]
+  LETTERS = [A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z]
+  APP_SECTIONS = [SPEECH_BUBBLE, SCALE, PICTURE, BOOKS, BABY, SKULL, VULGAR, NOTE]
 end