浏览代码

complete the member command and carousel support

Kylie Jo Swistak 6 年之前
父节点
当前提交
62846960de
共有 5 个文件被更改,包括 123 次插入7 次删除
  1. 0 1
      app/models/carousels.rb
  2. 7 0
      app/responses/carousel.rb
  3. 53 0
      app/responses/character.rb
  4. 50 6
      bot.rb
  5. 13 0
      lib/emoji.rb

+ 0 - 1
app/models/carousels.rb

@@ -1,4 +1,3 @@
 class Carousel < ActiveRecord::Base
   validates :message_id, presence: true
-  validates :char_id, presence: true
 end

+ 7 - 0
app/responses/carousel.rb

@@ -12,3 +12,10 @@ def arrow_react(message)
   message.react(Emoji::RIGHT)
   message.react(Emoji::CROSS)
 end
+
+def option_react(message, opts)
+  opts.each.with_index do |_, i|
+    message.react(Emoji::NUMBERS[i])
+  end
+  message.react(Emoji::CROSS)
+end

+ 53 - 0
app/responses/character.rb

@@ -186,6 +186,59 @@ def char_list_embed(chars, user = nil)
   embed
 end
 
+def user_char_embed(chars, user)
+  fields = []
+  active = []
+  npcs = []
+  user_name = user.nickname || user.name
+
+  chars.each do |char|
+    case char.active
+    when 'Active'
+      active.push char
+    when 'NPC'
+      npcs.push char.name
+    end
+  end
+
+  active.each.with_index do |char, i|
+    fields.push({
+      name: "#{i+1} #{char.name}",
+      value: "#{char.species} -- #{char.types}"
+    })
+  end
+
+  unless npcs.empty?
+    fields.push({ name: "#{user_name}'s NPCs", value: npcs.join(", ") })
+  end
+
+  embed = Embed.new(
+    title: "#{user_name}'s Characters",
+    description: "Click on the corresponding reaction to view the character",
+    fields: fields
+  )
+
+  embed.color = user.color.combined if user.color
+  embed
+end
+
+def dup_char_embed(chars, name)
+  fields = []
+
+  chars.each.with_index do |char, i|
+    fields.push({
+      name: "#{Emoji::NUMBERS[i]}: #{char.species}",
+      value: "Created by <@#{char.user_id}>"
+    })
+  end
+
+  Embed.new(
+    title: "Which #{name}?",
+    description: "Click on the corresponding reaction to pick",
+    fields: fields
+  )
+end
+
 def char_image_embed(char, image, user, color)
   footer = "#{user.name}##{user.tag} | #{char.active}" +
     " | #{image.category}"

+ 50 - 6
bot.rb

@@ -265,11 +265,14 @@ member = Command.new(:member, desc, opts) do |event, name, section, keyword|
   when Regex::UID
     user_id = Regex::UID.match(name)
   when String
-    char = Character.find_by!(name: name)
+    chars = Character.where(name: name)
+    char = chars.first if chars.length == 1
 
-    img = CharImage.where(char_id: char.id).find_by(keyword: 'Default')
-    user = event.server.member(char.user_id)
-    color = CharacterController.type_color(char)
+    if char
+      img = CharImage.where(char_id: char.id).find_by(keyword: 'Default')
+      user = event.server.member(char.user_id)
+      color = CharacterController.type_color(char)
+    end
   end
 
   case
@@ -279,8 +282,25 @@ member = Command.new(:member, desc, opts) do |event, name, section, keyword|
   when name && user_id
     chars = Character.where(user_id: user_id[1])
     user = event.server.member(user_id[1])
+    chars_id = []
+
+    chars.each do |char|
+      chars_id.push char.id if char.active == 'Active'
+    end
+
+    embed = user_char_embed(chars, user)
+    msg = event.send_embed("", embed)
+
+    Carousel.create(message_id: msg.id, options: chars_id)
+    option_react(msg, chars_id)
+  when name && chars && !char
+    embed = dup_char_embed(chars, name)
+    chars_id = chars.map(&:id)
+
+    msg = event.send_embed("", embed)
+    Carousel.create(message_id: msg.id, options: chars_id)
 
-    char_list_embed(chars, user)
+    option_react(msg, chars_id)
   when name && char && !section
     embed = character_embed(
       char: char,
@@ -461,6 +481,7 @@ bot.reaction_add do |event|
     when reactions[Emoji::LEFT]&.count.to_i > 1 then :left
     when reactions[Emoji::RIGHT]&.count.to_i > 1 then :right
     when reactions[Emoji::UNDO]&.count.to_i > 1 then :back
+    when reactions.any? { |k,v| Emoji::NUMBERS.include? k } then :number
     end
 
   case [form, vote]
@@ -737,9 +758,32 @@ bot.reaction_add do |event|
       section: :image
     )
     event.message.edit("", embed)
+
+  when [:carousel, :number]
+    char_index = nil
+    Emoji::NUMBERS.each.with_index do |emoji, i|
+      char_index = i if reactions[emoji]&.count.to_i > 1
+    end
+
+    if char_index
+      event.message.delete_all_reactions
+
+      char = Character.find(carousel.options[char_index])
+      carousel.update(id: carousel.id, char_id: char.id)
+
+      embed = character_embed(
+        char: char,
+        img: CharImage.where(char_id: char.id).find_by(keyword: 'Default'),
+        user: event.server.member(char.user_id),
+        color: CharacterController.type_color(char),
+        section: :default
+      )
+      event.message.edit("", embed)
+      section_react(event.message)
+    end
   when [:carousel, :cross]
     event.message.delete
-    Carousel.find_by(message_id: event.message.id).delete
+    carousel.delete
   end
 end
 

+ 13 - 0
lib/emoji.rb

@@ -28,6 +28,18 @@ module Emoji
   Y = "🇾"
   Z = "🇿"
 
+  ONE = "1⃣"
+  TWO = "2⃣"
+  THREE = "3⃣"
+  FOUR = "4⃣"
+  FIVE = "5⃣"
+  SIX = "6⃣"
+  SEVEN = "7⃣"
+  EIGHT = "8⃣"
+  NINE = "9⃣"
+  TEN = "🔟"
+  NUMS = "🔢"
+
   LEFT = "◀"
   RIGHT = "▶"
   UNDO = "⏫"
@@ -59,6 +71,7 @@ module Emoji
 
 
   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]
+  NUMBERS = [ONE, TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN]
   CHAR_APP = [SPEECH_BUBBLE, SCALE, PICTURE, BOOKS, BABY, SKULL, VULGAR, NOTE]
   IMG_APP = [DOG, KEY, FLAG, PAGE, BOOKS, VULGAR]
   CAROUSEL = [NOTEBOOK, QUESTION, PALLET, EAR, PICTURE, BAGS, FAMILY, EYES, KEY, CROSS]