Ver Fonte

accept applications via embed

Kylie Jo Swistak há 6 anos atrás
pai
commit
5e50fd5ff6

+ 5 - 7
app/controllers/image_controller.rb

@@ -1,17 +1,15 @@
 class ImageController
-  def self.default_image(content, char_id)
-    img_url =
-      /\*\*URL to the Character\'s Appearance\*\*\:\s(.*)/.match(content)
+  def self.default_image(url, char_id)
     img = CharImage.where(char_id: char_id).find_by(keyword: 'Default')
 
     case
-    when img_url && img
-      img.update(url: img_url[1])
+    when url && img
+      img.update(url: url)
       img.reload
-    when img_url && !img
+    when url && !img
       img = CharImage.create(
         char_id: char_id,
-        url: img_url[1],
+        url: url,
         category: 'SFW',
         keyword: 'Default'
       )

+ 2 - 2
app/models/bot.rb

@@ -1,7 +1,7 @@
 # frozen_string_literal: true
 
 module Bot
-  CHARACTER_TEST = 627702340018896896
-
   CHARACTER = 627702340018896896
+
+  #CHARACTER = 636981966784299009
 end

+ 6 - 6
app/models/channel.rb

@@ -1,11 +1,11 @@
 # frozen_string_literal: true
 
 module Channel
-  CHARACTER_TEST = 594244240020865035
-  CHARACTER_NSFW_TEST = 594244240020865035
-  APPROVAL_TEST = 627700961913208849
+  CHARACTER = 594244240020865035
+  CHARACTER_NSFW = 594244240020865035
+  APPROVAL = 627700961913208849
 
-  CHARACTER = 636979066003783739
-  CHARACTER_NSFW = 636979066003783739
-  APPROVAL = 636979008847872000
+  #CHARACTER = 636979066003783739
+  #CHARACTER_NSFW = 636979066003783739
+  #APPROVAL = 636979008847872000
 end

+ 30 - 24
app/models/char_images.rb

@@ -2,40 +2,46 @@ class CharImage < ActiveRecord::Base
   validates :char_id, presence: true
   validates :url, presence: true
 
-  def self.from_form(params)
+  def self.from_form(app)
     key_mapping = {
-      "**Character ID**" => "char_id",
-      "**Keyword**" => "keyword",
-      "**Category**" => "category",
-      "**URL**" => "url"
+      "Keyword" => "keyword",
+      "Category" => "category",
     }
 
     hash = {}
+    app.fields.each do |field|
+      next if field.nil?
 
-    params.map do |item|
-      next if item.empty?
-
-      key,value = item.split(": ")
-      db_column = key_mapping[key]
-
-      if db_column == "category"
-        hash[db_column] = value.upcase
-      else
-        hash[db_column] = value
-      end
+      db_column = key_mapping[field.name]
+      hash[db_column] =
+        db_column == 'category' ? field.value.upcase : field.value
     end
 
+    hash["char_id"] = app.footer.text
+    hash["url"] = app.image.url
+
     hash = hash.reject { |k,v| k == nil }
     hash
   end
 
-  def self.to_form(name, species, id, keyword, category, url, user_id)
-    form =
-      "_New Character Image_:\nSubmitted by: <@#{user_id}>\n\n>>> " +
-      "**Character**: #{name}\n**Species**: #{species}\n" +
-      "**Character ID**: #{id}\n**Keyword**: #{keyword}\n" +
-      "**Category**: #{category}\n**URL**: #{url}"
-
-    form
+  def self.to_form(char:, keyword:, category:, url:, user_id:)
+    Embed.new(
+      title: "#{char.name} | #{char.species}",
+      description: "<@#{user_id}>",
+      author: {
+        name: "Image Application",
+        icon_url: "https://i.imgur.com/fiLLQED.jpg"
+      },
+      image: {
+        url: url
+      },
+      footer: {
+        text: char.id
+      },
+      fields: [
+        { name: 'Keyword', value: keyword, inline: true },
+        { name: 'Category', value: category, inline: true }
+      ]
+    )
   end
 end

+ 37 - 41
app/models/characters.rb

@@ -4,31 +4,29 @@ class Character < ActiveRecord::Base
   validates :species, presence: true
   validates :types, presence: true
 
-  def self.from_form(params)
+  def self.from_form(app)
     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",
+      "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",
     }
 
@@ -59,19 +57,17 @@ class Character < ActiveRecord::Base
       "rating" => nil
     }
 
-    params.map do |item|
-      next if item.empty?
+    user_id = Regex::UID.match(app.description)
+    active = app.title == "Personal Character" ? 'Active' : 'NPC'
 
-      key,value = item.split(": ")
-      db_column = key_mapping[key]
+    hash["user_id"] = user_id[1]
+    hash["active"] = active
 
-      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
+    app.fields.each do |field|
+      next if field.nil?
+
+      db_column = key_mapping[field.name]
+      hash[db_column] = field.value
     end
 
     hash = hash.reject { |k,v| k == nil }
@@ -79,11 +75,11 @@ class Character < ActiveRecord::Base
   end
 
   def self.check_user(event)
-    content = event.message.content
+    app = event.message.embeds.first
 
-    edit_url = Regex::EDIT_URL.match(content)
-    active = Regex::CHAR_APP.match(content)
-    user_id = Regex::UID.match(content)
+    edit_url = app.footer.text
+    active = app.title
+    user_id = Regex::UID.match(app.description)
 
     user = User.find_by(id: user_id[1])
 
@@ -97,12 +93,12 @@ class Character < ActiveRecord::Base
       active_chars = active_chars.map(&:edit_url)
 
       new_active =
-        active[1] == "Personal Character" && !active_chars.include?(edit_url[1])
+        active == "Personal Character" && !active_chars.include?(edit_url)
 
       too_many = new_active ? active_chars.count >= allowed_chars : false
     end
 
-    if member
+    if user && member
       too_many ?
         too_many(event, member, edit_url, 'characters') : approval_react(event)
     else

+ 1 - 1
app/responses/app.rb

@@ -2,7 +2,7 @@ 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!",
+    title: "New Application!",
     description: desc,
     color: color
   )

+ 7 - 8
app/responses/application_responses.rb

@@ -5,18 +5,17 @@ def approval_react(event)
   event.message.react(Emoji::N)
 end
 
-def too_many(event, user, edit_url, model)
+#def too_many(event, user, edit_url, model)
+def too_many(event, edit_url, model)
   message = "You have too many #{model}!" +
-    "\nPlease deactivate and try again #{Url::CHARACTER}#{edit_url[1]}"
+    "\nPlease deactivate and try again #{Url::CHARACTER}#{edit_url}"
 
-  event.server.member(user).dm(message)
+  #event.server.member(user).dm(message)
+  event.respond(message)
   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)
+  event.message.react(Emoji::WHAT)
+  event.message.react(Emoji::CROSS)
 end

+ 7 - 5
app/responses/character.rb

@@ -1,15 +1,16 @@
-def character_embed(char:, img: nil, user:, color:, section: :all)
+def character_embed(char:, img: nil, user:, color:, section: nil)
   fields = []
-  footer_text = "#{user.name}##{user.tag} | #{char.active}"
+  user_name = user ? "#{user.name}##{user.tag}" : "Unknown User"
+
+  footer_text = "#{user_name} | #{char.active}"
   footer_text += " | #{char.rating}" if char.rating
   footer_text += " | #{img.category} " if section == :image
 
   navigate = "React to Navigate"
-  footer_text += " | #{navigate}"
+  footer_text += " | #{navigate}" unless section.nil?
 
   embed = Embed.new(
     footer: {
-      icon_url: user.avatar_url,
       text: footer_text
     },
     title: char.name,
@@ -17,7 +18,7 @@ def character_embed(char:, img: nil, user:, color:, section: :all)
   )
 
   case section
-  when :all
+  when :all, nil
     embed.description = char.personality if char.personality
     fields = char_type(char, fields)
     fields = char_status(char, fields)
@@ -48,6 +49,7 @@ def character_embed(char:, img: nil, user:, color:, section: :all)
 
   embed.thumbnail = { url: img.url } if img && section != :image
   embed.fields = fields
+  embed.footer.icon_url = user.avatar_url if user
 
   embed
 end

+ 42 - 24
app/responses/reject.rb

@@ -2,10 +2,14 @@ MSG = "Please resubmit when you've addressed the issues!\n"
 FTR = "If you have any questions, feel free to ask a Guildmaster"
 
 def reject_char_embed(app)
-  image_url = /\*\*URL to the Character\'s Appearance\*\*\:\s(.*)/.match(app)
-
   fields = []
 
+  app.fields.each do |f|
+    fields.push({ name: f.name, value: f.value, inline: true })
+  end
+
+  fields.push({ name: "\u200b", value: "\u200b" })
+
   CharApp::REJECT_MESSAGES.map do |emoji, message|
     fields.push({
       name: emoji,
@@ -28,20 +32,33 @@ def reject_char_embed(app)
   })
 
   embed = Embed.new(
-    title: "**_APPLICATION REJECTED_**",
-    description: "Please indicate what message to forward to the user!",
+    title: app.title,
+    description: app.description,
+    author: {
+      name: app.author.name.gsub('Application', 'Rejection'),
+      icon_url: app.author.icon_url
+    },
     color: Color::ERROR,
+    footer: {
+      text: app.footer.text
+    },
     fields: fields
   )
 
-  embed.thumbnail = { url: image_url[1] } if image_url
+  embed.thumbnail.url = app.thumbnail.url if app.thumbnail
+
   embed
 end
 
 def reject_img_embed(app)
-  img_url = /\*\*URL\*\*:\s(.*)/.match(app)
   fields = []
 
+  app.fields.each do |f|
+    fields.push({ name: f.name, value: f.value, inline: true })
+  end
+
+  fields.push({ name: "\u200b", value: "\u200b" })
+
   ImgApp::REJECT_MESSAGES.map do |emoji, message|
     fields.push({
       name: emoji,
@@ -61,22 +78,28 @@ def reject_img_embed(app)
     value: instructions
   })
 
-  embed = Embed.new(
-    title: "**_APPLICATION REJECTED_**",
-    description: "Please indicate what message to forward to the user!",
+  Embed.new(
+    title: app.title,
+    description: app.description,
+    author: {
+      name: app.author.name.gsub('Application', 'Rejection'),
+      icon_url: app.author.icon_url
+    },
+    thumbnail: {
+      url: app.image.url
+    },
     color: Color::ERROR,
+    footer: {
+      text: app.footer.text
+    },
     fields: fields
   )
-
-  embed.thumbnail = { url: img_url[1] } if img_url
-  embed
 end
 
 def user_char_app(event)
   reactions = event.message.reactions
-  content = event.message.content
+  app = event.message.embeds.first
 
-  edit_url = Regex::EDIT_URL.match(content)
   description = ""
 
   Emoji::CHAR_APP.each do |reaction|
@@ -99,7 +122,7 @@ def user_char_app(event)
       },
       {
         name: MSG,
-        value: "[Edit Your Application](#{Url::CHARACTER}#{edit_url[1]})"
+        value: "[Edit Your Application](#{Url::CHARACTER}#{app.footer.text})"
       }
     ]
   )
@@ -109,9 +132,8 @@ end
 
 def user_img_app(event)
   reactions = event.message.reactions
-  content = event.message.content
+  app = event.message.embeds.first
 
-  img_url = /\*\*URL\*\*:\s(.*)/.match(content)
   description = ""
 
   Emoji::IMG_APP.each do |reaction|
@@ -126,9 +148,6 @@ def user_img_app(event)
   embed = Embed.new(
     title: "**Your application has been rejected!!**",
     color: Color::ERROR,
-    thumbnail: {
-      url: img_url[1]
-    },
     footer: {
       text: FTR
     },
@@ -140,15 +159,14 @@ def user_img_app(event)
     ]
   )
 
+  embed.thumbnail.url = app.thumbnail.url if app.thumbnail
   embed
 end
 
-def self_edit_embed(content)
-  edit_url = Regex::EDIT_URL.match(content)
-
+def self_edit_embed(app)
   Embed.new(
     title: "Don't forget to resubmit!",
     color: Color::ERROR,
-    description: "[Edit the Application](#{Url::CHARACTER}#{edit_url[1]})"
+    description: "[Edit the Application](#{Url::CHARACTER}#{app.footer.text})"
   )
 end

+ 40 - 42
bot.rb

@@ -11,8 +11,8 @@ require 'active_record'
 
 # Constants: such as roles and channel ids
 
-ADMINS_TEST = 308250685554556930
-ADMINS = 453262984769437696
+ADMINS = 308250685554556930
+#ADMINS = 453262984769437696
 
 # ---
 
@@ -206,7 +206,7 @@ image = Command.new(:image, desc, opts) do |event, name, keyword, tag, url|
 
   char = Character.where(user_id: user.id).find_by!(name: name) if name
   color = CharacterController.type_color(char) if char
-  img = CharImage.find_by(keyword: keyword) if keyword
+  img = CharImage.where(char_id: char.id).find_by(keyword: keyword) if keyword
 
   case
   when /^Default$/i.match(keyword)
@@ -216,16 +216,14 @@ image = Command.new(:image, desc, opts) do |event, name, keyword, tag, url|
     )
   when char && keyword && url && tag && tag.match(/(n)?sfw/i)
     img_app = CharImage.to_form(
-      char.name,
-      char.species,
-      char.id,
-      keyword,
-      tag,
-      url,
-      user.id
+      char: char,
+      keyword: keyword,
+      category: tag,
+      url: url,
+      user_id: user.id
     )
 
-    approval = bot.send_message(Channel::APPROVAL, img_app, false, nil)
+    approval = bot.send_message(Channel::APPROVAL, "", false, img_app)
     approval.react(Emoji::Y)
     approval.react(Emoji::N)
 
@@ -294,6 +292,11 @@ member = Command.new(:member, desc, opts) do |event, name, section, keyword|
 
     Carousel.create(message_id: msg.id, options: chars_id)
     option_react(msg, chars_id)
+  when name && chars.empty?
+    error_embed(
+      "Character not found!",
+      "Could not find a character named #{name}"
+    )
   when name && chars && !char
     embed = dup_char_embed(chars, name)
     chars_id = chars.map(&:id)
@@ -440,8 +443,9 @@ end
 
 # This will trigger when any reaction is added in discord
 bot.reaction_add do |event|
-  content = event.message.content
   reactions = event.message.reactions
+  app = event.message.embeds.first
+  carousel = Carousel.find_by(message_id: event.message.id)
 
   maj = if event.server
           event.server.roles.find{ |r| r.id == ADMINS }.members.count / 2
@@ -449,17 +453,14 @@ bot.reaction_add do |event|
   maj = 1
 
   form =
-    case
-    when event.message.author.id == Bot::CHARACTER
-      :character_application
-    when event.message.from_bot? && content.match(Regex::CHAR_APP)
-      :character_rejection
-    when event.message.from_bot? && event.server.nil?
-      :pm
-    when event.message.from_bot? && content.match(/\_New\sCharacter\sImage\_:/)
-      :image_application
-    when carousel = Carousel.find_by(message_id: event.message.id)
-      :carousel
+    case app.author.name
+    when 'New App' then :new_app
+    when 'Character Application' then :character_application
+    when 'Character Rejection' then :character_rejection
+    when 'Image Application' then :image_application
+    when 'Image Rejection' then :image_rejection
+    else
+      :carousel if carousel
     end
 
   vote =
@@ -487,12 +488,12 @@ bot.reaction_add do |event|
 
   case [form, vote]
   when [:character_application, :yes]
-    params = content.split("\n")
-    uid = Regex::UID.match(content)
+    app = event.message.embeds.first
+    uid = Regex::UID.match(app.description)
     user = event.server.member(uid[1])
 
-    char = CharacterController.edit_character(params)
-    img = ImageController.default_image(content, char.id)
+    char = CharacterController.edit_character(app)
+    img = ImageController.default_image(app.thumbnail.url, char.id)
     color = CharacterController.type_color(char)
 
     embed = character_embed(
@@ -505,7 +506,7 @@ bot.reaction_add do |event|
     if embed
       bot.send_message(
         Channel::CHARACTER,
-        "Good news, <@#{user.id}>! Your character was approved",
+        "Good news, <@#{uid}>! Your character was approved",
         false,
         embed
       )
@@ -517,11 +518,10 @@ bot.reaction_add do |event|
       )
     end
   when [:character_application, :no]
-    content = event.message.content
-    embed = reject_char_embed(content)
+    embed = reject_char_embed(app)
 
     event.message.delete
-    reject = event.send_embed(content, embed)
+    reject = event.send_embed("", embed)
 
     Emoji::CHAR_APP.each do |reaction|
       reject.react(reaction)
@@ -532,7 +532,7 @@ bot.reaction_add do |event|
     reject.react(Emoji::CRAYON)
 
   when [:character_rejection, :check]
-    user = event.server.member(Regex::UID.match(content)[1])
+    user = event.server.member(Regex::UID.match(app.description)[1])
     embed = user_char_app(event)
 
     event.message.delete
@@ -548,17 +548,16 @@ bot.reaction_add do |event|
       "",
       35,
       false,
-      self_edit_embed(content)
+      self_edit_embed(app)
     )
 
-  when [:pm, :phone]
+  when [:new_app, :phone]
     event.message.delete_own_reaction(Emoji::PHONE)
     user = event.message.reacted_with(Emoji::PHONE).first
 
     bot.send_message(user.dm.id, user.id, false, nil)
   when [:image_application, :yes]
-    params = content.split("\n")
-    img = ImageController.edit_image(params)
+    img = ImageController.edit_image(app)
 
     char = Character.find(img.char_id)
     user = event.server.member(char.user_id)
@@ -574,11 +573,10 @@ bot.reaction_add do |event|
               end
     bot.send_message(channel, "Image Approved!", false, embed)
   when [:image_application, :no]
-    content = event.message.content
-    embed = reject_img_embed(content)
+    embed = reject_img_embed(app)
 
     event.message.delete
-    reject = event.send_embed(content, embed)
+    reject = event.send_embed("", embed)
 
     Emoji::IMG_APP.each do |reaction|
       reject.react(reaction)
@@ -587,14 +585,14 @@ bot.reaction_add do |event|
     reject.react(Emoji::CHECK)
     reject.react(Emoji::CROSS)
 
-  when [:image_application, :check]
-    user = event.server.member(Regex::UID.match(content)[1])
+  when [:image_rejection, :check]
+    user = event.server.member(Regex::UID.match(app.description)[1])
     embed = user_img_app(event)
 
     event.message.delete
     bot.send_temporary_message(event.channel.id, "", 5, false, embed)
     bot.send_message(user.dm.id, "", false, embed)
-  when [:image_application, :cross]
+  when [:image_rejection, :cross]
     event.message.delete
 
   when [:carousel, :notebook]

+ 1 - 0
lib/emoji.rb

@@ -46,6 +46,7 @@ module Emoji
 
   CHECK = "✅"
   CROSS = "❌"
+  WHAT = "⁉"
 
   SPEECH_BUBBLE = "💬"
   SCALE = "⚖"