Преглед на файлове

begin to clean up code, alter character creation to reflect form changes

Kylie Jo Swistak преди 6 години
родител
ревизия
d61aa2a1cf
променени са 3 файла, в които са добавени 126 реда и са изтрити 94 реда
  1. 74 0
      app/models/characters.rb
  2. 39 0
      app/responses/char_display.rb
  3. 13 94
      bot.rb

+ 74 - 0
app/models/characters.rb

@@ -3,4 +3,78 @@ class Character < ActiveRecord::Base
   validates :name, presence: true
   validates :species, presence: true
   validates :types, presence: true
+
+  def self.from_form(params)
+    key_mapping = {
+      "_New Character Application_" => "active",
+      "Submitted by" => "user_id",
+      " >>> **Characters Name**" => "name",
+      "**Species**" => "species",
+      "**Type**" => "types",
+      "**Age**" => "age",
+      "**Weight**" => "weight",
+      "**Height**" => "height",
+      "**Gender**" => "gender",
+      "**Sexual Orientation**" => "orientation",
+      "**Relationship Status**" => "relationship",
+      "**Attacks**" => "attacks",
+      "**Likes**" => "likes",
+      "**Dislikes**" => "dislikes",
+      "**Personality**" => "personality",
+      "**Hometown**" => "hometown",
+      "**Warnings**" => "warnings",
+      "**Rumors**" => "rumors",
+      "**Backstory**" => "backstory",
+      "**Other**" => "other",
+      "**Rating**" => "rating",
+      "**Current Location**" => "location",
+      "**DM Notes**" => "dm_notes",
+      "Edit Key (ignore)" => "edit_url",
+    }
+
+    hash = {
+      "user_id" => nil,
+      "name" => nil,
+      "species" => nil,
+      "types" => nil,
+      "age" => nil,
+      "weight" => nil,
+      "height" => nil,
+      "gender" => nil,
+      "orientation" => nil,
+      "relationship" => nil,
+      "attacks" => nil,
+      "likes" => nil,
+      "dislikes" => nil,
+      "personality" => nil,
+      "backstory" => nil,
+      "other" => nil,
+      "edit_url" => nil,
+      "active" => nil,
+      "dm_notes" => nil,
+      "location" => nil,
+      "rumors" => nil,
+      "hometown" => nil,
+      "warnings" => nil,
+      "rating" => nil
+    }
+
+    params.map do |item|
+      next if item.empty?
+
+      key,value = item.split(": ")
+      db_column = key_mapping[key]
+
+      if db_column == "active" && value == "Personal Character"
+        hash[db_column] = "Active"
+      elsif v = value.match(/<@([0-9]+)>/)
+        hash[db_column] = v[1]
+      else
+        hash[db_column] = value
+      end
+    end
+
+    hash = hash.reject { |k,v| k == nil }
+    hash
+  end
 end

+ 39 - 0
app/responses/char_display.rb

@@ -0,0 +1,39 @@
+def character_embed(character, image, member)
+  fields = []
+  user = "#{member.name}##{member.tag}"
+  footer_text = "Created by #{user} | #{character.active}"
+  footer_text += " | #{character.rating}" if character.rating
+
+  fields.push({name: 'Species', value: character.species, inline: true}) if character.species
+  fields.push({name: 'Type', value: character.types, inline: true}) if character.types
+  fields.push({name: 'Age', value: character.age, inline: true}) if character.age
+  fields.push({name: 'Weight', value: character.weight, inline: true}) if character.weight
+  fields.push({name: 'Height', value: character.height, inline: true}) if character.height
+  fields.push({name: 'Gender', value: character.gender, inline: true}) if character.gender
+  fields.push({name: 'Sexual Orientation', value: character.orientation, inline: true}) if character.orientation
+  fields.push({name: 'Relationship Status', value: character.relationship, inline: true}) if character.relationship
+  fields.push({name: 'Hometown', value: character.hometown, inline: true}) if character.hometown
+  fields.push({name: 'Location', value: character.location, inline: true}) if character.location
+  fields.push({name: 'Attacks', value: character.attacks}) if character.attacks
+  fields.push({name: 'Likes', value: character.likes}) if character.likes
+  fields.push({name: 'Dislikes', value: character.dislikes}) if character.dislikes
+  fields.push({name: 'Warnings', value: character.warnings}) if character.warnings
+  fields.push({name: 'Rumors', value: character.rumors}) if character.rumors
+  fields.push({name: 'Backstory', value: character.backstory}) if character.backstory
+  fields.push({name: 'Other', value: character.other}) if character.other
+  fields.push({name: 'DM Notes', value: character.dm_notes}) if character.dm_notes
+
+  embed = Embed.new(
+    footer: {
+      text: footer_text
+    },
+    title: character.name,
+    fields: fields
+  )
+
+  embed.description = character.personality if character.personality
+  embed.thumbnail = { url: image } if image
+  embed.color = member.color.combined if member.color.combined
+
+  embed
+end

+ 13 - 94
bot.rb

@@ -8,7 +8,6 @@ BOT_ENV = ENV.fetch('BOT_ENV') { 'development' }
 Bundler.require(:default, BOT_ENV)
 
 require 'active_record'
-require_relative 'lib/emoji'
 
 # Constants: such as roles and channel ids
 
@@ -46,6 +45,7 @@ ActiveRecord::Base.establish_connection(
 )
 
 Dir['app/**/*.rb'].each { |f| require File.join(File.expand_path(__dir__), f) }
+Dir["/lib/*.rb"].each {|file| require file }
 
 
 token = ENV['DISCORD_BOT_TOKEN']
@@ -75,66 +75,20 @@ def check_user(event)
   end
 end
 
-def edit_character(params, member)
-  key_mapping = {
-    "Submitted by" => "user_id",
-    " >>> **Characters Name**" => "name",
-    "**Species**" => "species",
-    "**Type**" => "types",
-    "**Age**" => "age",
-    "**Weight**" => "weight",
-    "**Height**" => "height",
-    "**Gender**" => "gender",
-    "**Sexual Orientation**" => "orientation",
-    "**Relationship Status**" => "relationship",
-    "**URL to your character's appearance**" => "image_url",
-    "**Attacks**" => "attacks",
-    "**Custom Attack Description**" => "custom_attack",
-    "**Likes**" => "likes",
-    "**Dislikes**" => "dislikes",
-    "**Personality**" => "personality",
-    "**Rumors**" => "rumors",
-    "**Backstory**" => "backstory",
-    "**Other**" => "other",
-    "Edit Key (ignore)" => "edit_url",
-  }
-
-  hash = {}
-
-  params.map do |item|
-    next if item.empty?
-
-    key,value = item.split(": ")
-    db_column = key_mapping[key]
-
-    if v = value.match(/<@([0-9]+)>/)
-      hash[db_column] = v[1]
-    else
-      hash[db_column] = value
-    end
-  end
-
-  # Should we add this to the form, and allow NPCs to be created the same way?
-  hash["active"] = 'Active'
-
-  if image_url = hash["image_url"]
-    hash = hash.reject { |k| k == "image_url" }
-  end
-
-  if custom_attack = hash["custom_attack"]
-    hash = hash.reject { |k| k == "custom_attack" }
-  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: hash["edit_url"])
-    char.update!(hash)
-    character = Character.find_by(edit_url: hash["edit_url"])
+  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(hash)
+    character = Character.create(char_hash)
   end
 
-  edit_images(image_url, character.id, 'sfw', 'primary') if image_url
-
-  character_embed(character, image_url, member)
+  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)
@@ -143,40 +97,6 @@ def edit_images(image_url, character_id, category, keyword)
   end
 end
 
-def character_embed(character, image, member)
-  fields = []
-  user = "#{member.name}##{member.tag}"
-
-  fields.push({name: 'Species', value: character.species, inline: true}) if character.species
-  fields.push({name: 'Type', value: character.types, inline: true}) if character.types
-  fields.push({name: 'Age', value: character.age, inline: true}) if character.age
-  fields.push({name: 'Weight', value: character.weight, inline: true}) if character.weight
-  fields.push({name: 'Height', value: character.height, inline: true}) if character.height
-  fields.push({name: 'Gender', value: character.gender, inline: true}) if character.gender
-  fields.push({name: 'Sexual Orientation', value: character.orientation, inline: true}) if character.orientation
-  fields.push({name: 'Relationship Status', value: character.relationship, inline: true}) if character.relationship
-  fields.push({name: 'Attacks', value: character.attacks}) if character.attacks
-  fields.push({name: 'Likes', value: character.likes}) if character.likes
-  fields.push({name: 'Dislikes', value: character.dislikes}) if character.dislikes
-  fields.push({name: 'Rumors', value: character.rumors}) if character.rumors
-  fields.push({name: 'Backstory', value: character.backstory}) if character.backstory
-  fields.push({name: 'Other', value: character.other}) if character.other
-
-  embed = Embed.new(
-    footer: {
-      text: "Created by #{user} | #{character.active}"
-    },
-    title: character.name,
-    fields: fields
-  )
-
-  embed.description = character.personality if character.personality
-  embed.thumbnail = { url: image } if image
-  embed.color = member.color.combined if member.color.combined
-
-  embed
-end
-
 # ---
 
 # Commands: structure basic bot commands here
@@ -272,11 +192,10 @@ bot.reaction_add do |event|
     maj = 1
 
     if event.message.reacted_with(Emoji::Y).count > maj
-      msg = event.message.content.split("\n")
-      uid = /<@([0-9]+)>/.match(event.message.content)
+      uid = /<@([0-9]+)>/.match(content)
       member = event.server.member(uid[1])
 
-      embed = edit_character(msg, member)
+      embed = edit_character(content, member)
 
       if embed
         event.message.delete