我运行一个 irc 机器人,用 ruby 编写,运行 cinch irc 框架。机器人回复有趣的事实并循环浏览这些事实,这样您就不会厌倦它们。我设置了一个冷却时间,所以它们不能在 6 小时内显示。它不再显示最初显示的事实,而是显示随机选择的事实,这可能是之前显示的事实。
line = IO.readlines("facts.txt")
factnumber = rand(line.length)
if fact_not_next_6_hours[factnumber] == true
factnumber = rand(line.length)
m.reply "fact #{factnumber}: #{line[factnumber]}"
fact_not_next_6_hours[factnumber] = true
fact_not_next_6_hours[factnumber]
是 6 小时冷却的变量;如果设置为 true,则冷却处于活动状态。我需要去做:
factnumber = rand(line.length)
直到它没有将 6 小时冷却设置为 true,然后执行
m.reply "fact #{factnumber}: #{line[factnumber]}"
fact_not_next_6_hours[factnumber] = true
我的第一个想法是做多个if
s,但没有奏效,我相信有更好的方法。