2016-10-21 38 views
-1

我收到了ruby聊天機器人,如果在聊天過程中某個角色重複了五次或更多次,在第一次和第二次嘗試中,它會在第三次嘗試時警告用戶,用戶和第四個禁止了兩個小時的用戶,而且它目前正在如何在紅寶石中使用大寫字母方法

require_relative '../plugin' 

class Flood 
    include Chatbot::Plugin 
    match /(.*)/, :method => :check_swear, :use_prefix => false 

    def initialize(bot) 
    super(bot) 
    @data = {} 
    end 

    def check_swear(user, message) 
    message = message.downcase 
    array = ["aaaaa", "ñññññ", "bbbbb", "ccccc", "ddddd", "eeeee", "fffff", "ggggg", "hhhhh", "iiiii", "jjjjj", "mmmmm", ".....", "*****", "?????", "!!!!!", "zzzzz", "kkkkkk", "ooooo", "nnnnn", "ppppp", "qqqqq", "rrrrr", "-----", "_____", "¨¨¨¨¨¨¨¨", "{{{{{", "}}}}}", "#####"] 
    array.each do |e| 
     if message.include? e 
     if(@data[user.name] and @data[user.name] == 3) 
      @client.send_msg "%s: [[Wiki_Freddy_Fazbear's_Pizza:Reglas_y_Lineamientos|Has sido advertido. Tendrás un ban de 2 horas.]] 4/3" % user.name 
      @client.ban user.name, "7200", "Ban automático por exceso de carácteres - Si crees que esto fué un error, contacta con un [[Wiki_Freddy_Fazbear%27s_Pizza:Administradores|moderador u Admin en su muro de mensajes]]." 
      @client.send_msg "!mods por si acaso consideran necesario más tiempo de ban." 
      @client.kick user.name 
      @data[user.name] = 0 
     elsif(@data[user.name] and @data[user.name] == 2) 
      @data[user.name] ||= 0 
      @data[user.name] += 1 
      @client.send_msg "%s: [[Wiki_Freddy_Fazbear's_Pizza:Reglas_y_Lineamientos|Por favor, no repitas carácteres, última advertencia antes de un ban.]] 3/3" % user.name 
      @client.kick user.name 
     elsif(@data[user.name] and @data[user.name] == 1) 
      @data[user.name] ||= 0 
      @data[user.name] += 1 
      @client.send_msg "%s: [[Wiki_Freddy_Fazbear's_Pizza:Reglas_y_Lineamientos|Por favor, no repitas carácteres, última advertencia antes de un kick]], 2/3" % user.name 
     else 
      @data[user.name] ||= 0 
      @data[user.name] += 1 
      @client.send_msg "%s: [[Wiki_Freddy_Fazbear's_Pizza:Reglas_y_Lineamientos|Por favor, no repitas carácteres]], 1/3" % user.name 
     end 
     end 
    end 
    end 
end 

現在,我需要去改變它,它是所有相同,除了它不具有5個以上的字符觸發,但如果消息中有5個或更多的單詞是大寫字母,有人可以幫助我嗎?

編輯:順便說一下,如果有人還可以幫助我,使之引發的不僅僅是在數組列表,但任何字符的字符,這將是真棒

+0

@HolgerJust我試過這個https://gist.github.com/anonymous/44b7737b48a256f904add3f30cde08a3它沒有工作,我已經跑出想法:((關於標題,它應該如何命名,無論如何?) –

+0

因此,我會被警告/禁止說:「除美元外還有其他美元,例如澳元,加拿大元,港幣和新西蘭元?」_? – Stefan

回答

2

像下面的內容將返回true如果消息有5個或更多的大寫單詞。

def is_message_shouting?(message) 
    shouted_words = 0 

    message.split(' ').each do |word| 
    shouted_words += 1 if word.upcase == word 
    end 

    shouted_words >= 5 
end 

puts is_message_shouting? 'THIS IS A VERY SHOUTY MESSAGE' 
puts is_message_shouting? 'this is not a shouty message' 
puts is_message_shouting? 'THIS IS ALSO not a shouty message' 

輸出:

true 
false 
false 
+0

它工作的很好,現在的代碼就是這樣的 –

+0

woops,我沒有結束評論,https://gist.github.com/anonymous/f4ca4a734e55bde6a5df9585e1c29aed就是這樣,但是當聊天中的某人寫下「WunnyLove:La ultima vez quevía KaiserGreymon4 fue hace 5 dias,9 horas,4 minutos and 47 segundos」它觸發了,爲什麼? –

+0

哦,等等,如果你把4個單獨的數字觸發,我該如何解決它?x0 –

0

喜歡的東西

class String 
    def is_upper? 
     self == self.upcase 
    end 
end 

def shouting?(msg) 
    count = 0 
    0.upto(msg.size) { |i| count += 1 if msg[i].is_upper? } 
    count >= 5 
end 
+0

這將找到5個或更多的大寫字母的消息,但問題要求5個或更多的大寫單詞。 –

1
def shouting?(message) 
    message.split.count { |word| word == word.upcase } >= 5 
end 

沒有原添加,只是一個更清潔的實現一樣。