carlos quintero carlos quintero - 4 years ago 107
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)
@data = {}

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[] and @data[] == 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" %
@client.ban, "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."
@data[] = 0
elsif(@data[] and @data[] == 2)
@data[] ||= 0
@data[] += 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" %
elsif(@data[] and @data[] == 1)
@data[] ||= 0
@data[] += 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" %
@data[] ||= 0
@data[] += 1
@client.send_msg "%s: [[Wiki_Freddy_Fazbear's_Pizza:Reglas_y_Lineamientos|Por favor, no repitas carácteres]], 1/3" %

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 Source

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

  shouted_words >= 5

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'


Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download