-1

I got a chatbot in ruby that if a character is repeated five times or more in the chat, in the first and second attempt, it warns the user, at the third attempt it kicks the user and the fourth one bans the user for two hours, and it's currently working

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

Now, I need to change it, being all the same except for that it does not trigger with 5 characters or more, but if 5 or more words in a message are in capital letters, can someone help me?

Edit: By the way, if someone can also help me to make it trigger not just with the characters in the array list but any character, it would be awesome

  • @HolgerJust I tried this https://gist.github.com/anonymous/44b7737b48a256f904add3f30cde08a3 bit it didn't worked and I already runned out of ideas :( (About the title, how it should be named, anyway? ) – carlos quintero Oct 21 '16 at 10:10
  • So I will be warned / banned for saying _"There are other dollars beside USD, e.g. AUD, CAD, HKD and NZD"_? – Stefan Oct 21 '16 at 10:55

3 Answers3

2

Something like the following will return true if a message has 5 or more uppercase words.

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'

Outputs:

true
false
false
Jack Bracken
  • 1,233
  • 12
  • 23
  • woops, I didnt end the comment, https://gist.github.com/anonymous/f4ca4a734e55bde6a5df9585e1c29aed is like this, but when someone in the chat wrote "WunnyLove: La ultima vez que ví a KaiserGreymon4 fue hace 5 dias, 9 horas, 4 minutos and 47 segundos" it triggered, why? – carlos quintero Oct 22 '16 at 01:51
  • Oh, wait, if you put 4 separate numbers it triggers, how can I fix it? x0 – carlos quintero Oct 22 '16 at 02:08
  • You would need to check if the word is just a number or not. You can add a method to the String class as per this suggestion and change your code to shouted_words += 1 if word.upcase == word && !word.integer? http://stackoverflow.com/questions/1235863/test-if-a-string-is-basically-an-integer-in-quotes-using-ruby – Jack Bracken Oct 22 '16 at 02:14
1
def shouting?(message)
  message.split.count { |word| word == word.upcase } >= 5
end

Nothing original to add, just a cleaner implementation of the same.

ndnenkov
  • 35,425
  • 9
  • 72
  • 104
0

Something like

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
luispcosta
  • 542
  • 1
  • 6
  • 20