Forráskód Böngészése

clean up code to adopt some ruby best practices

Kylie Jo Swistak 6 éve
szülő
commit
74d1c3a6cc
7 módosított fájl, 188 hozzáadás és 122 törlés
  1. 19 16
      app/models/characters.rb
  2. 1 1
      app/models/commands.rb
  3. 32 0
      app/responses/errors.rb
  4. 19 12
      app/responses/help.rb
  5. 5 3
      app/responses/reject.rb
  6. 12 0
      app/responses/success.rb
  7. 100 90
      bot.rb

+ 19 - 16
app/models/characters.rb

@@ -80,26 +80,29 @@ class Character < ActiveRecord::Base
 
   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)
+
+    edit_url = EDIT_URL.match(content)
+    active = NEW_APP.match(content)
+    user_id = UID.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 user
+      member = event.server.member(user_id[1])
 
-        if characters < allowed_characters && characters < 6
-          approval_react(event)
-        else
-          too_many(event, member, edit_url, 'characters')
-        end
-      else
-        approval_react(event)
-      end
+      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
+
+
+      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
+    end
+
+    if member
+      too_many(event, member, edit_url, 'characters') if too_many
+      approval_react(event) unless too_many
     else
       unknown_member(event)
     end

+ 1 - 1
app/models/commands.rb

@@ -3,7 +3,7 @@ require 'optparse'
 class Command
   attr_reader :name, :operation
 
-  def initialize(name, description, options =[], &block)
+  def initialize(name, description, options ={}, &block)
     @name = name
     @description = description
     @options = options

+ 32 - 0
app/responses/errors.rb

@@ -0,0 +1,32 @@
+ERROR_RED = "#c42d2d"
+
+def error_embed(message)
+  Embed.new(
+    title: "There was an Error",
+    description: message,
+    color: ERROR_RED,
+    footer: {
+      text: "For more help, feel free to ask a Moderator or Guildmaster"
+    }
+  )
+end
+
+def admin_error_embed(message)
+  Embed.new(
+    title: "Error",
+    description: message,
+    color: ERROR_RED
+  )
+end
+
+def command_error_embed(message, command)
+  Embed.new(
+    title: "There was an Error",
+    description: message,
+    color: ERROR_RED,
+    footer: {
+      text: "For more help, feel free to ask a Moderator or Guildmaster"
+    },
+    fields: command_usage(command)
+  )
+end

+ 19 - 12
app/responses/help.rb

@@ -1,4 +1,6 @@
-def all_commands(commands)
+HELP_BLUE = "#4976ca"
+
+def all_commands_embed(commands)
   fields = []
 
   commands.each do |command|
@@ -8,7 +10,18 @@ def all_commands(commands)
   Embed.new(
     title: "Commands",
     description: "To learn more about any of the listed commands, use `pkmn-help [command]`",
-    color: "#73FE49",
+    color: HELP_BLUE,
+    fields: fields
+  )
+end
+
+def command_embed(command)
+  fields = command_usage(command)
+
+  Embed.new(
+    title: "pkmn-#{command.name}",
+    description: command.description,
+    color: HELP_BLUE,
     fields: fields
   )
 end
@@ -17,19 +30,13 @@ def command_usage(command)
   fields = []
 
   unless command.options.empty?
-    usage = "```\n"
-    command.options.each do |option|
-      usage += "pkmn-#{command.name} #{option}\n"
+    usage = "```bash\n"
+    command.options.map do |option, description|
+      usage += "##{description}\npkmn-#{command.name} #{option}\n\n"
     end
     usage += "```"
   end
 
   fields.push({name: "Usage", value: usage}) if command.options
-
-  Embed.new(
-    title: "pkmn-#{command.name}",
-    description: command.description,
-    color: "#73fe49",
-    fields: fields
-  )
+  fields
 end

+ 5 - 3
app/responses/reject.rb

@@ -1,3 +1,5 @@
+REJECT_RED = "#a41e1f"
+
 def reject_char_embed(app)
   image_url = /\*\*URL to the Character\'s Appearance\*\*\:\s(.*)/.match(app)
 
@@ -12,7 +14,7 @@ def reject_char_embed(app)
   Embed.new(
     title: "**_APPLICATION REJECTED_**",
     description: "Please indicate what message to forward to the user!",
-    color: "#a41e1f",
+    color: REJECT_RED,
     thumbnail: {
       url: image_url[1]
     },
@@ -36,7 +38,7 @@ def message_user_embed(event)
 
   embed = Embed.new(
     title: "**Your application has been rejected!!**",
-    color: "#a41e1f",
+    color: REJECT_RED,
     fields: [
       { name: "Listed reasons for rejection:", value: description },
       { name: "You can edit your application and resubmit here:", value: "#{APP_FORM}#{edit_url[1]}" }
@@ -51,7 +53,7 @@ def self_edit_embed(content)
 
   Embed.new(
     title: "Please edit the user's application and resubmit!",
-    color: "#a41e1f",
+    color: REJECT_RED,
     description: "#{APP_FORM}#{edit_url[1]}"
   )
 end

+ 12 - 0
app/responses/success.rb

@@ -0,0 +1,12 @@
+SUCCESS_GREEN = "#6bc037"
+
+def success_embed(message)
+  Embed.new(
+    title: "Hooray!",
+    description: message,
+    color: SUCCESS_GREEN,
+    footer: {
+      text: "High Five!"
+    }
+  )
+end

+ 100 - 90
bot.rb

@@ -29,6 +29,7 @@ APP_FORM = "https://docs.google.com/forms/d/e/1FAIpQLSfryXixX3aKBNQxZT8xOfWzuF02
 # Regexes
 UID = /<@([0-9]+)>/
 EDIT_URL = /Edit\sKey\s\(ignore\):\s([\s\S]*)/
+NEW_APP = /\_New\sCharacter\sApplication\_:\s(.*)/
 
 # ---
 
@@ -81,25 +82,23 @@ hello = Command.new(:hello, "Says hello!\nGreat for testing if the bot is respon
   )
 end
 
-help = Command.new(:help, "Displays help information for the commands", [nil, :command]) do |event, command|
-  if (cmd = /pkmn-(\w+)/.match(command))
-    command = cmd[1]
-  end
+opts = { "" => "displays a list of all commands", "command" => "displays info and usage for specified command" }
+help = Command.new(:help, "Displays help information for the commands", opts) do |event, command|
+  short = /pkmn-(\w+)/.match(command) if command
+  cmd = short ? short[1] : command if command
+  cmd = commands.detect { |c| c.name == cmd.to_sym } if cmd
 
-  if command
-    if cmd = commands.detect { |c| c.name == command.to_sym }
-      embed = command_usage(cmd)
-      event.send_embed("", embed)
-    else
-      event.respond("I don't know this command!")
-    end
+  if command && cmd
+    command_embed(cmd)
+  elsif !command
+    all_commands_embed(commands)
   else
-    embed = all_commands(commands)
-    event.send_embed("", embed)
+    command_error_embed("Command not found!", help)
   end
 end
 
-matchup = Command.new(:matchup, "Displays a chart of effectiveness for the given type", [:type]) do |event, type|
+opts = { "type" => "" }
+matchup = Command.new(:matchup, "Displays a chart of effectiveness for the given type", opts) do |event, type|
   channel = event.channel.id
   file = "images/Type #{type.capitalize}.png"
 
@@ -110,22 +109,29 @@ matchup = Command.new(:matchup, "Displays a chart of effectiveness for the given
   end
 end
 
-app = Command.new(:app, "Gives the user links for starting or editing character applications", [nil, :name]) do |event, name|
+opts = { "" => "starts a new app", "name" => "edits an existing app", "name | (in)active" => "sets app to active or inactive" }
+app = Command.new(:app, "Everything to do with character applications", opts) do |event, name, status|
   user = event.author
   user_channel = event.author.dm
 
-  if name
-    if character = Character.where(user_id: user.id).find_by(name: name)
-      edit_url = APP_FORM + character.edit_url
-      embed = edit_app_embed(event, edit_url, name)
+  character = Character.where(user_id: user.id).find_by(name: name) if name
+  active = status.match(/(in)?active/i) if status
+
+  if status && active && character
+    character.update!(active: active[0].capitalize)
+    character.reload
+
+    success_embed("Successfully updated #{name} to be #{active[0].downcase}")
+  elsif name && character && !status
+    edit_url = APP_FORM + character.edit_url
+    embed = edit_app_embed(event, edit_url, name)
 
-      bot.send_message(user_channel.id, "", false, embed)
-    else
-      app_not_found_embed(event, name)
-    end
-  else
-    embed = new_app_embed(event)
     bot.send_message(user_channel.id, "", false, embed)
+  elsif !name && !status
+    embed = new_app_embed(event, name)
+    bot.send_message(user_channel.id, "", false, embed)
+  else
+    command_error_embed("There was an error processing your application!", app)
   end
 end
 
@@ -141,27 +147,23 @@ commands = [
 # This will trigger on every message sent in discord
 bot.message do |event|
   content = event.message.content
+  author = event.author.id
 
-  if (match = /^pkmn-(\w+)/.match(content))
-      command = match[1]
+  command = /^pkmn-(\w+)/.match(content)
+  cmd = commands.detect { |c| c.name == command[1].to_sym } if command
 
-      if cmd = commands.detect { |c| c.name == command.to_sym }
-        reply = cmd.call(content, event)
+  reply = cmd.call(content, event) if cmd
 
-        if reply.is_a? Embed
-          event.send_embed("", reply)
-        elsif reply
-          event.respond(reply)
-        else
-          event.respond("Something went wrong!")
-        end
-      end
+  case reply
+  when Embed
+    event.send_embed("", reply)
+  when String
+    event.respond(reply)
   end
 
-  if event.author.id == APP_BOT
-    Character.check_user(event)
-  end
+  event.send_embed("", error_embed("Command not found!")) if command && !cmd
 
+  Character.check_user(event) if author == APP_BOT
 end
 
 # This will trigger when a dm is sent to the bot from a user
@@ -171,61 +173,69 @@ end
 # This will trigger when any reaction is added in discord
 bot.reaction_add do |event|
   content = event.message.content
+  reactions = event.message.reactions
 
-  if event.message.author.id == APP_BOT
-    maj = event.server.roles.find{ |r| r.id == ADMINS }.members.count / 2
-    maj = 1
-
-    if event.message.reacted_with(Emoji::Y).count > maj
-      params = content.split("\n")
-      uid = UID.match(content)
-      member = event.server.member(uid[1])
-
-      character = CharacterController.edit_character(params)
-      image_url = ImageController.edit_images(content, character.id)
+  maj = event.server.roles.find{ |r| r.id == ADMINS }.members.count / 2
+  maj = 1
 
-      embed = character_embed(character, image_url, member)
-
-      if embed
-        event.message.delete
-
-        bot.send_message(
-          CHAR_CHANNEL,
-          "Character Approved!",
-          false,
-          embed
-        )
-      else
-        event.respond("Something went wrong")
-      end
-
-    elsif event.message.reacted_with(Emoji::N).count > maj
-      embed = reject_char_embed(content)
-      reject_app(event, embed)
+  form =
+    case
+    when event.message.author.id == APP_BOT
+      :character_application
+    when event.message.from_bot? && content.match(NEW_APP)
+      :character_rejection
     end
-  end
-
-  if event.message.from_bot? && content.match(/\_New\sCharacter\sApplication\_/)
-    if event.message.reacted_with(Emoji::CHECK).count > 1
-      user_id = UID.match(content)
-      member = event.server.member(user_id[1])
 
-      embed = message_user_embed(event)
-
-      event.message.delete
-      bot.send_temporary_message(event.channel.id, "", 5, false, embed)
-
-      user_channel = member.dm
-      bot.send_message(user_channel.id, "", false, embed)
-
-    elsif event.message.reacted_with(Emoji::CROSS).count > 1
-      event.message.delete
-    elsif event.message.reacted_with(Emoji::CRAYON).count > 1
-      embed = self_edit_embed(content)
-
-      event.message.delete
-      bot.send_temporary_message(event.channel.id, "", 35, false, embed)
+  vote =
+    case
+    when reactions[Emoji::Y].present? && reactions[Emoji::Y].count > maj
+      :yes
+    when reactions[Emoji::N].present? && reactions[Emoji::N].count > maj
+      :no
+    when reactions[Emoji::CHECK].present? && reactions[Emoji::CHECK].count > 1
+      :check
+    when reactions[Emoji::CROSS].present? && reactions[Emoji::CROSS].count > 1
+      :cross
+    when reactions[Emoji::CRAYON].present? && reactions[Emoji::CRAYON].count > 1
+      :crayon
     end
+
+  case [form, vote]
+  when [:character_application, :yes]
+    params = content.split("\n")
+    uid = UID.match(content)
+    member = event.server.member(uid[1])
+
+    character = CharacterController.edit_character(params)
+    image_url = ImageController.edit_images(content, character.id)
+
+    embed = character_embed(character, image_url, member) if character
+    bot.send_message(
+      CHAR_CHANNEL,
+      "Good news, <@#{member.id}>! Your character was approved",
+      false,
+      embed
+    ) if embed
+
+    event.message.delete if embed
+    event.respond("", admin_error_embed("Something went wrong when saving application")) unless embed
+
+  when [:character_application, :no]
+    reject_app(event, reject_char_embed(content))
+
+  when [:character_rejection, :check]
+    member = event.server.member(UID.match(content)[1])
+    embed = message_user_embed(event)
+
+    event.message.delete
+    bot.send_temporary_message(event.channel.id, "", 5, false, embed)
+    bot.send_message(member.dm.id, "", false, embed)
+  when [:character_rejection, :cross]
+    event.message.delete
+
+  when [:character_rejection, :crayon]
+    event.message.delete
+    bot.send_temporary_message(event.channel.id, "", 35, false, self_edit_embed(content))
   end
 end