我在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 :(得分:2)
如果邮件包含5个或更多大写单词,则以下内容将返回true。
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
答案 1 :(得分:1)
def shouting?(message)
message.split.count { |word| word == word.upcase } >= 5
end
没有任何原创内容可以添加,只是更清晰的实现。
答案 2 :(得分: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