Kaynağa Gözat

Code Cleanup

Kylie Jo Swistak 6 yıl önce
ebeveyn
işleme
a22f46f02b

+ 24 - 13
app/controllers/image_controller.rb

@@ -1,28 +1,39 @@
 class ImageController
   def self.default_image(content, char_id)
-    if image_url = /\*\*URL to the Character\'s Appearance\*\*\:\s(.*)/.match(content)
-      if image = CharImage.where(char_id: char_id).find_by(keyword: 'Default')
-        image.update(url: image_url[1])
-        image.reload
-      else
-        image = CharImage.create(char_id: char_id, url: image_url[1], category: 'SFW', keyword: 'Default')
-      end
+    img_url =
+      /\*\*URL to the Character\'s Appearance\*\*\:\s(.*)/.match(content)
+    img = CharImage.where(char_id: char_id),find_by(keyword: 'Default')
 
+    case
+    when img_url && img
+      img.update(url: img_url[1])
+      img.reload
+    when img_url && !img
+      img = CharImage.create(
+        char_id: char_id,
+        url: img_url[1],
+        category: 'SFW',
+        keyword: 'Default'
+      )
     end
 
-    image ? image.url : image_url[1]
+    img
   end
 
   def self.edit_image(params)
     img_hash = CharImage.from_form(params)
+    char_id = img_hash["char_id"]
+    keyword = img_hash["keyword"]
 
-    if image = CharImage.where(char_id: img_hash["char_id"]).find_by(keyword: img_hash["keyword"])
-      image.update!(img_hash)
-      image.reload
+    img = CharImage.where(char_id: char_id).find_by(keyword: keyword)
+
+    if img
+      img.update!(img_hash)
+      img.reload
     else
-      image = CharImage.create(img_hash)
+      img = CharImage.create(img_hash)
     end
 
-    image
+    img
   end
 end

+ 9 - 7
app/models/characters.rb

@@ -91,18 +91,20 @@ class Character < ActiveRecord::Base
       member = event.server.member(user_id[1])
 
       calc_max = (user.level / 10 + 1)
-      allowed_characters = calc_max > 6 ? 6 : calc_max
-      active_characters = Character.where(user_id: user_id[1]).where(active: "Active").count
+      allowed_chars = calc_max > 6 ? 6 : calc_max
+      active_chars =
+        Character.where(user_id: user_id[1]).where(active: "Active")
+      active_chars = active_chars.map(&:edit_url)
 
+      new_active =
+        active[1] == "Personal Character" && !active_chars.include?(edit_url[1])
 
-      new_active = active[1] == "Personal Character" && Character.where(active: 'NPC').find_by(edit_url: edit_url[1])
-
-      too_many = new_active ? active_characters < allowed_characters : false
+      too_many = new_active ? active_chars.count >= allowed_chars : false
     end
 
     if member
-      too_many(event, member, edit_url, 'characters') if too_many
-      approval_react(event) unless too_many
+      too_many ?
+        too_many(event, member, edit_url, 'characters') : approval_react(event)
     else
       unknown_member(event)
     end

+ 8 - 3
app/responses/app.rb

@@ -1,7 +1,9 @@
 def new_app_embed(user_name, color)
+  desc = "Hi, #{user_name},\nI see you'd like to start a new character" +
+    " application!\nI'll send you instructions in a dm!"
   Embed.new(
     title: "New Appliction!",
-    description: "Hi, #{user_name},\nI see you'd like to start a new character application!\nI'll send you instructions in a dm!",
+    description: desc,
     color: color
   )
 end
@@ -9,7 +11,7 @@ end
 def new_app_dm(user_name, color, code)
   Embed.new(
     title: "Hi, #{user_name}",
-    description: "If you have any questions, please feel free to ask a Guildmaster!",
+    description: "If you have any questions, feel free to ask a Guildmaster!",
     color: color,
     footer: {
       text: "If you cannot copy your key, press the #{Emoji::PHONE}"
@@ -43,7 +45,10 @@ def app_not_found_embed(user_name, char_name)
     description: "I can't seem to find your character named, #{char_name}",
     color: Color::ERROR,
     fields: [
-      { name: "Want to start a new application?", value: "You can start one with this command:\n```pkmn-app```"}
+      {
+        name: "Want to start a new application?",
+        value: "You can start one with this command:\n```pkmn-app```"
+      }
     ]
   )
 end

+ 4 - 1
app/responses/application_responses.rb

@@ -6,7 +6,10 @@ def approval_react(event)
 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]}")
+  message = "You have too many #{model}!" +
+    "\nPlease deactivate and try again #{Url::CHARACTER}#{edit_url[1]}"
+
+  event.server.member(user).dm(message)
   event.message.delete
 end
 

+ 8 - 7
app/responses/errors.rb

@@ -1,14 +1,16 @@
 ERROR_RED = "#c42d2d"
 
-def error_embed(message)
-  Embed.new(
-    title: "There was an Error",
-    description: message,
+def error_embed(title, message = nil)
+  embed = Embed.new(
+    title: title,
     color: ERROR_RED,
     footer: {
       text: "For more help, feel free to ask a Moderator or Guildmaster"
     }
   )
+
+  embed.description = message if message
+  embed
 end
 
 def admin_error_embed(message)
@@ -19,10 +21,9 @@ def admin_error_embed(message)
   )
 end
 
-def command_error_embed(message, command)
+def command_error_embed(title, command)
   Embed.new(
-    title: "There was an Error",
-    description: message,
+    title: title,
     color: ERROR_RED,
     footer: {
       text: "For more help, feel free to ask a Moderator or Guildmaster"

+ 3 - 1
app/responses/help.rb

@@ -2,6 +2,8 @@ HELP_BLUE = "#4976ca"
 
 def all_commands_embed(commands)
   fields = []
+  desc = "To learn more about any of the listed commands," +
+    " use `pkmn-help [command]`"
 
   commands.each do |command|
     fields.push({name: "pkmn-#{command.name}", value: command.description})
@@ -9,7 +11,7 @@ def all_commands_embed(commands)
 
   Embed.new(
     title: "Commands",
-    description: "To learn more about any of the listed commands, use `pkmn-help [command]`",
+    description: desc,
     color: HELP_BLUE,
     fields: fields
   )

+ 18 - 15
app/responses/poll.rb

@@ -1,21 +1,24 @@
 def new_poll_embed(event, question, options)
-    fields = []
-    name = event.author.nickname || event.author.name
+  fields = []
+  name = event.author.nickname || event.author.name
 
-    options.map.with_index do |option, index|
-      fields.push({ name: "#{Emoji::LETTERS[index]} #{option}", value: CharAppResponses::INLINE_SPACE, inline: true })
-    end
+  options.map.with_index do |option, index|
+    fields.push({
+      name: "#{Emoji::LETTERS[index]} #{option}",
+      value: CharAppResponses::INLINE_SPACE, inline: true
+    })
+  end
 
-    chat_embed = Embed.new(
-        title:  question,
-        description: "Created by : #{name}",
-        fields: fields
-    )
+  chat_embed = Embed.new(
+    title:  question,
+    description: "Created by : #{name}",
+    fields: fields
+  )
 
-    chat_embed.color = event.author.color.combined
+  chat_embed.color = event.author.color.combined
 
-    poll = event.send_embed("", chat_embed)
-    options.each.with_index do |_, index|
-      poll.react(Emoji::LETTERS[index])
-    end
+  poll = event.send_embed("", chat_embed)
+  options.each.with_index do |_, index|
+    poll.react(Emoji::LETTERS[index])
   end
+end

+ 25 - 4
app/responses/reject.rb

@@ -4,10 +4,25 @@ def reject_char_embed(app)
   fields = []
 
   CharAppResponses::REJECT_MESSAGES.map do |emoji, message|
-    fields.push({ name: emoji, value: "#{message}\n#{CharAppResponses::INLINE_SPACE}", inline: true })
+    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" })
+  instructions =
+    "#{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"
+
+  fields.push({
+    name: "Submitting",
+    value: instructions
+  })
 
   embed = Embed.new(
     title: "**_APPLICATION REJECTED_**",
@@ -38,8 +53,14 @@ def message_user_embed(event)
     title: "**Your application has been rejected!!**",
     color: Color::ERROR,
     fields: [
-      { name: "Listed reasons for rejection:", value: description },
-      { name: "You can edit your application and resubmit here:", value: "#{Url::CHARACTER}#{edit_url[1]}" }
+      {
+        name: "Listed reasons for rejection:",
+        value: description
+      },
+      {
+        name: "You can edit your application and resubmit here:",
+        value: "#{Url::CHARACTER}#{edit_url[1]}"
+      }
     ]
   )
 

+ 7 - 3
bot.rb

@@ -194,7 +194,8 @@ image = Command.new(:image, desc, opts) do |event, name, keyword, tag, url|
 
   case
   when char && keyword && url && tag && tag.match(/(n)?sfw/i)
-    img_app = CharImage.to_form(char.name, char.species, char.id, keyword, tag, url)
+    img_app =
+      CharImage.to_form(char.name, char.species, char.id, keyword, tag, url)
 
     approval = bot.send_message(Channel::APPROVAL, img_app, false, nil)
     approval.react(Emoji::Y)
@@ -216,7 +217,10 @@ image = Command.new(:image, desc, opts) do |event, name, keyword, tag, url|
   end
 
 rescue ActiveRecord::RecordNotFound
-  error_embed("Could not find your character name #{name}")
+  error_embed(
+    "Character not Found!",
+    "I could not find your character named #{name}"
+  )
 end
 
 # ---
@@ -319,7 +323,7 @@ bot.reaction_add do |event|
     color = CharacterController.type_color(char)
 
 
-    embed = character_embed(char, image_url, user, color) if character
+    embed = character_embed(char, image_url, user, color) if char
 
     if embed
       bot.send_message(