浏览代码

Start accetping image additions and changes

Kylie Jo Swistak 6 年之前
父节点
当前提交
b0c36daebb

+ 3 - 3
app/controllers/character_controller.rb

@@ -2,9 +2,9 @@ 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"])
+    if character = Character.find_by(edit_url: char_hash["edit_url"])
+      character.update!(char_hash)
+      character.reload
     else
       character = Character.create(char_hash)
     end

+ 16 - 3
app/controllers/image_controller.rb

@@ -1,8 +1,8 @@
 class ImageController
-  def self.edit_images(content, char_id)
+  def self.default_image(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')
+      unless CharImage.where(char_id: char_id).find_by(url: image_url[1])
+        image = CharImage.create(char_id: char_id, url: image_url[1], category: 'SFW', keyword: 'Primary')
       end
     end
 
@@ -12,4 +12,17 @@ class ImageController
       image_url[1]
     end
   end
+
+  def self.edit_image(params)
+    img_hash = CharImage.from_form(params)
+
+    if image = CharImage.where(char_id: img_hash["char_id"]).find_by(url: img_hash["url"])
+      image.update!(img_hash)
+      image.reload
+    else
+      image = CharImage.create(img_hash)
+    end
+
+    image
+  end
 end

+ 5 - 0
app/models/bot.rb

@@ -0,0 +1,5 @@
+# frozen_string_literal: true
+
+module Bot
+  CHARACTER = 627702340018896896
+end

+ 7 - 0
app/models/channel.rb

@@ -0,0 +1,7 @@
+# frozen_string_literal: true
+
+module Channel
+  CHARACTER = 594244240020865035
+  CHARACTER_NSFW = 594244240020865035
+  APPROVAL = 627700961913208849
+end

+ 28 - 1
app/models/char_images.rb

@@ -1,4 +1,31 @@
-class CharImages < ActiveRecord::Base
+class CharImage < ActiveRecord::Base
   validates :char_id, presence: true
   validates :url, presence: true
+
+  def self.from_form(params)
+    key_mapping = {
+      "**Character ID**" => "char_id",
+      "**Keyword**" => "keyword",
+      "**Category**" => "category",
+      "**URL**" => "url"
+    }
+
+    hash = {}
+
+    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
+    end
+
+    hash = hash.reject { |k,v| k == nil }
+    hash
+  end
 end

+ 3 - 3
app/models/characters.rb

@@ -81,9 +81,9 @@ class Character < ActiveRecord::Base
   def self.check_user(event)
     content = event.message.content
 
-    edit_url = EDIT_URL.match(content)
-    active = NEW_APP.match(content)
-    user_id = UID.match(content)
+    edit_url = Regex::EDIT_URL.match(content)
+    active = Regex::NEW_APP.match(content)
+    user_id = Regex::UID.match(content)
 
     user = User.find_by(id: user_id[1])
 

+ 7 - 0
app/models/color.rb

@@ -0,0 +1,7 @@
+# frozen_string_literal: true
+
+module Color
+  DEFAULT = "#95a5a6"
+  DISCORD = "#36393f"
+  ERROR = "#a41e1f"
+end

+ 5 - 0
app/models/image.rb

@@ -0,0 +1,5 @@
+# frozen_string_literal: true
+
+module Image
+  HAPPY = "https://static.pokemonpets.com/images/monsters-images-800-800/479-Rotom.png"
+end

+ 7 - 0
app/models/regex.rb

@@ -0,0 +1,7 @@
+# frozen_string_literal: true
+
+module Regex
+  UID = /<@([0-9]+)>/
+  EDIT_URL = /Edit\sKey\s\(ignore\):\s([\s\S]*)/
+  CHAR_APP = /\_New\sCharacter\sApplication\_:\s(.*)/
+end

+ 6 - 0
app/models/url.rb

@@ -0,0 +1,6 @@
+# frozen_string_literal: true
+
+module Url
+  CHARACTER = "https://docs.google.com/forms/d/e/1FAIpQLSfryXixX3aKBNQxZT8xOfWzuF02emkJbqJ1mbMGxZkwCvsjyA/viewform"
+  CHAR = "https://forms.gle/6HT931F1DvBsHMBf6"
+end

+ 27 - 34
app/responses/app.rb

@@ -1,56 +1,49 @@
-def new_app_embed(event)
-  name = event.author.nickname || event.author.name
-
-  chat_embed = Embed.new(
-    title: "New Application!",
-    description: "Hi, #{name},\nI see you'd like to start a new character application!\nI'll send you instructions in a dm!",
-    color: event.author.color.combined
+def new_app_embed(user_name, color)
+  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!",
+    color: color
   )
+end
 
-  embed = Embed.new(
-    title: "Hi, #{name}",
+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!",
-    color: event.author.color.combined,
+    color: color,
+    footer: {
+      text: "If you cannot copy your key, press the #{Emoji::PHONE}"
+    },
     fields: [
-      { name: "Please start your application here:", value: APP_FORM },
-      { name: "Your key is:", value: event.author.id }
+      { name: "Please start your application here:", value: Url::CHAR },
+      { name: "Your key is:", value: code }
     ]
   )
-
-  event.send_embed("", chat_embed)
-  embed
 end
 
-def edit_app_embed(event, edit_url, char_name)
-  name = event.author.nickname || event.author.name
-
-  chat_embed = Embed.new(
+def edit_app_embed(user_name, char_name, color)
+  Embed.new(
     title: "You want to edit #{char_name}?",
-    description: "Good news, #{name}! I'll dm you a link",
-    color: event.author.color.combined
+    description: "Good news, #{user_name}! I'll dm you a link",
+    color: color
   )
+end
 
-  embed = Embed.new(
+def edit_app_dm(char_name, edit_url, color)
+  Embed.new(
     title: "You may edit #{char_name} here:",
     description: edit_url,
-    color: event.author.color.combined
+    color: color
   )
-
-  event.send_embed("", chat_embed)
-  embed
 end
 
-def app_not_found_embed(event, char_name)
-  name = event.author.nickname || event.author.name
-
-  embed = Embed.new(
-    title: "I'm sorry, #{name}",
+def app_not_found_embed(user_name, char_name)
+  Embed.new(
+    title: "I'm sorry, #{user_name}",
     description: "I can't seem to find your character named, #{char_name}",
-    color: "#a41e1f",
+    color: Color::ERROR,
     fields: [
       { name: "Want to start a new application?", value: "You can start one with this command:\n```pkmn-app```"}
     ]
   )
-
-  event.send_embed("", embed)
 end

+ 17 - 1
app/responses/char_display.rb → app/responses/character.rb

@@ -1,7 +1,7 @@
 def character_embed(character, image, member)
   fields = []
   user = "#{member.name}##{member.tag}"
-  footer_text = "Created by #{user} | #{character.active}"
+  footer_text = "#{user} | #{character.active}"
   footer_text += " | #{character.rating}" if character.rating
 
   fields.push({name: 'Species', value: character.species, inline: true}) if character.species
@@ -25,6 +25,7 @@ def character_embed(character, image, member)
 
   embed = Embed.new(
     footer: {
+      icon_url: member.avatar_url,
       text: footer_text
     },
     title: character.name,
@@ -37,3 +38,18 @@ def character_embed(character, image, member)
 
   embed
 end
+
+def char_image_embed(character_name, image, user)
+  user_name = "#{user.name}##{user.tag}"
+
+  Embed.new(
+    footer: {
+      icon_url: user.avatar_url,
+      text: "#{user_name} | #{image.keyword} | #{image.category}"
+    },
+    title: character_name,
+    image: {
+      url: image.url
+    }
+  )
+end

+ 88 - 38
bot.rb

@@ -11,26 +11,8 @@ require 'active_record'
 
 # Constants: such as roles and channel ids
 
-# Users
-APP_BOT = 627702340018896896
-
-# Roles
 ADMINS = 308250685554556930
 
-# Channels
-CHAR_CHANNEL = 594244240020865035
-
-# Images
-HAP_ROTOM = "https://static.pokemonpets.com/images/monsters-images-800-800/479-Rotom.png"
-
-# URLs
-APP_FORM = "https://docs.google.com/forms/d/e/1FAIpQLSfryXixX3aKBNQxZT8xOfWzuF02emkJbqJ1mbMGxZkwCvsjyA/viewform"
-
-# Regexes
-UID = /<@([0-9]+)>/
-EDIT_URL = /Edit\sKey\s\(ignore\):\s([\s\S]*)/
-NEW_APP = /\_New\sCharacter\sApplication\_:\s(.*)/
-
 # ---
 
 Dotenv.load if BOT_ENV != 'production'
@@ -77,7 +59,7 @@ hello = Command.new(:hello, "Says hello!\nGreat for testing if the bot is respon
     description: greetings.sample,
     color: event.author.color.combined,
     thumbnail: {
-      url: HAP_ROTOM
+      url: Image::HAPPY
     }
   )
 end
@@ -112,24 +94,33 @@ end
 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
+  user_name = user.nickname || user.name
+  color = user.color ? user.color.combined : Color::DEFAULT
 
   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
+  if name && !character
+    app_not_found_embed(user_name, name)
+
+  elsif 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)
+    edit_url = Url::CHARACTER + character.edit_url
+    embed = edit_app_dm(name, edit_url, color)
 
-    bot.send_message(user_channel.id, "", false, embed)
+    bot.send_message(user.dm.id, "", false, embed)
+    edit_app_embed(user_name, name, color)
   elsif !name && !status
-    embed = new_app_embed(event, name)
-    bot.send_message(user_channel.id, "", false, embed)
+    embed = new_app_dm(user_name, color, user.id)
+
+    message = bot.send_message(user.dm.id, "", false, embed)
+    message.react(Emoji::PHONE)
+
+    new_app_embed(user_name, color)
   else
     command_error_embed("There was an error processing your application!", app)
   end
@@ -145,8 +136,6 @@ end
 
 opts = { "participants" => "May accept Everyone, Here, or a comma seperated list of names"}
 raffle = Command.new(:raffle, "Creates a raffle and picks a winner", opts) do |event, participant|
-  user_channel = event.author.dm
-
   participants =
     case participant
     when /^everyone$/i
@@ -158,7 +147,7 @@ raffle = Command.new(:raffle, "Creates a raffle and picks a winner", opts) do |e
     end
 
   winner = participants.sample
-  winner_name = 
+  winner_name =
     case winner
     when String
       winner
@@ -173,6 +162,29 @@ raffle = Command.new(:raffle, "Creates a raffle and picks a winner", opts) do |e
     end
 end
 
+opts = { "name | key, words | (n)sfw | url" => "" }
+image = Command.new(:image, "Edit your character's images", opts) do |event, name, keyword, tag, url|
+  character = Character.where(user_id: event.author.id).find_by!(name: name) if name
+  category = /(n)?sfw/i.match(tag) if tag
+
+  if character && category
+    image = "_New Character Image_:\n\n>>> **Character**: #{character.name}\n**Species**: #{character.species}"
+    image += "\n\n**Character ID**: #{character.id}\n**Keyword**: #{keyword}\n**Category**: #{tag}\n\n**URL**: #{url}"
+    approval = bot.send_message(Channel::APPROVAL, image, false, nil)
+
+    approval.react(Emoji::Y)
+    approval.react(Emoji::N)
+  end
+
+  if approval
+    success_embed("Your image has been submitted for approval!")
+  else
+    error_embed("Something went wrong!")
+  end
+rescue ActiveRecord::RecordNotFound
+  error_embed("Could not find your character name #{name}")
+end
+
 # ---
 
 commands = [
@@ -201,13 +213,28 @@ bot.message do |event|
     event.respond(reply)
   end
 
-  event.send_embed("", error_embed("Command not found!")) if command && !cmd
+  event.send_embed("", error_embed("Command not found!")) if command && !cmd && event.server
 
-  Character.check_user(event) if author == APP_BOT
+  Character.check_user(event) if author == Bot::CHARACTER
 end
 
+pm_commands = [ image ]
+
 # This will trigger when a dm is sent to the bot from a user
 bot.pm do |event|
+  content = event.message.content
+
+  command = /^pkmn-(\w+)/.match(content)
+  cmd = pm_commands.detect { |c| c.name == command[1].to_sym } if command
+
+  reply = cmd.call(content, event) if cmd
+
+  case reply
+  when Embed
+    event.send_embed("", reply)
+  when String
+    event.respond(reply)
+  end
 end
 
 # This will trigger when any reaction is added in discord
@@ -215,15 +242,19 @@ bot.reaction_add do |event|
   content = event.message.content
   reactions = event.message.reactions
 
-  maj = event.server.roles.find{ |r| r.id == ADMINS }.members.count / 2
+  maj = event.server.roles.find{ |r| r.id == ADMINS }.members.count / 2 if event.server
   maj = 1
 
   form =
     case
-    when event.message.author.id == APP_BOT
+    when event.message.author.id == Bot::CHARACTER
       :character_application
-    when event.message.from_bot? && content.match(NEW_APP)
+    when event.message.from_bot? && content.match(Regex::CHAR_APP)
       :character_rejection
+    when event.message.from_bot? && event.server.nil?
+      :user_key
+    when event.message.from_bot? && content.match(/\_New\sCharacter\sImage\_:/)
+      :image_application
     end
 
   vote =
@@ -238,20 +269,22 @@ bot.reaction_add do |event|
       :cross
     when reactions[Emoji::CRAYON].present? && reactions[Emoji::CRAYON].count > 1
       :crayon
+    when reactions[Emoji::PHONE].present? && reactions[Emoji::PHONE].count > 1
+      :phone
     end
 
   case [form, vote]
   when [:character_application, :yes]
     params = content.split("\n")
-    uid = UID.match(content)
+    uid = Regex::UID.match(content)
     member = event.server.member(uid[1])
 
     character = CharacterController.edit_character(params)
-    image_url = ImageController.edit_images(content, character.id)
+    image_url = ImageController.default_image(content, character.id)
 
     embed = character_embed(character, image_url, member) if character
     bot.send_message(
-      CHAR_CHANNEL,
+      Channel::CHARACTER,
       "Good news, <@#{member.id}>! Your character was approved",
       false,
       embed
@@ -264,7 +297,7 @@ bot.reaction_add do |event|
     reject_app(event, reject_char_embed(content))
 
   when [:character_rejection, :check]
-    member = event.server.member(UID.match(content)[1])
+    member = event.server.member(Regex::UID.match(content)[1])
     embed = message_user_embed(event)
 
     event.message.delete
@@ -276,6 +309,23 @@ bot.reaction_add do |event|
   when [:character_rejection, :crayon]
     event.message.delete
     bot.send_temporary_message(event.channel.id, "", 35, false, self_edit_embed(content))
+
+  when [:user_key, :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")
+    image = ImageController.edit_image(params)
+
+    char = Character.find(image.char_id)
+    user = event.server.member(char.user_id)
+    embed = char_image_embed(char.name, image, user)
+
+    event.message.delete if embed
+    channel = image.category.upcase == 'SFW' ? Channel::CHARACTER : Channel::CHARACTER_NSFW
+    bot.send_message(channel, "Image Approved!", false, embed)
   end
 end
 

+ 1 - 0
lib/emoji.rb

@@ -38,6 +38,7 @@ module Emoji
   BABY = "👶"
   VULGAR = "🖕"
   CRAYON = "🖍"
+  PHONE = "📱"
 
   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]