carlos quintero carlos quintero - 1 month ago 5
Ruby Question

How to have upper case methods in ruby

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

Answer

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