11require 'cinch'
2+ require 'sqlite3'
23
3- class QuestionRecord < Struct . new ( :who , :what , :time )
4+ DB = SQLite3 ::Database . new ( "db.sqlite3" )
5+ DB . results_as_hash = true
6+
7+ class Seen < Struct . new ( :who )
8+
9+ #return an array of everyone we've seen
10+ def self . find_all
11+ nicks = DB . execute ( "select * from seen" )
12+ users = { }
13+ nicks . each do |nick |
14+ users [ nick [ 'who' ] . to_sym ] = true
15+ end
16+ users
17+ end
18+
19+ #save to the db
20+ def save
21+ DB . execute ( "insert into seen(who) values(:who)" , 'who' => who )
22+ end
23+ end
24+
25+ class Question < Struct . new ( :who , :what , :time )
426 def to_s
527 "[#{ time . asctime } ] #{ who } had a question, '#{ what } '"
628 end
29+ def save
30+ DB . execute ( "insert into questions(who, what, time) values(:who, :what, :time)" , 'who' => who , 'what' => what , 'time' => time . to_s )
31+
32+ end
33+ def self . find_for_nick nick
34+ questions = DB . execute ( "select * from questions where who = ? limit 5" , nick )
35+ questions . collect { |q | Question . new ( q [ 'who' ] , q [ 'what' ] , Time . parse ( q [ 'time' ] ) ) }
36+ end
737end
838
939bot = Cinch ::Bot . new do
@@ -12,29 +42,34 @@ def to_s
1242 c . password = ""
1343 c . server = "irc.freenode.org"
1444 c . channels = [ "#hacketyhack" ]
15- @users = { }
45+ @users = Seen . find_all
1646 end
1747
1848 on :message , "hello" do |m |
1949 m . reply "Hello, #{ m . user . nick } "
2050 end
2151
2252 on :message , /\? / do |m |
23- @users [ m . user . nick ] ||= [ ]
24- if @users [ m . user . nick ] . length == 0
53+ unless @users . key? m . user . nick . to_sym
2554 m . channel . send "#{ m . user . nick } : Thanks for asking a question! You can also get help here: http://bit.ly/hacketyhelp"
55+ seen = Seen . new ( m . user . nick )
56+ seen . save
57+ @users [ m . user . nick . to_sym ] = true
2658 end
27- @users [ m . user . nick ] << QuestionRecord . new ( m . user . nick , m . message , Time . new )
59+ qr = Question . new ( m . user . nick , m . message , Time . new )
60+ qr . save
2861 m . channel . send "#{ m . user . nick } : your question has been recorded."
2962 end
3063
3164 on :channel , /^!question (.+)/ do |m , nick |
3265 if nick == bot . nick
3366 m . reply "That's me!"
34- elsif @users . key? ( nick )
35- @users [ nick ] . each do |qr |
67+ elsif @users . key? ( nick . to_sym )
68+ m . reply "The last 5 questions by #{ nick } are:"
69+ Question . find_for_nick ( nick ) . each do |qr |
3670 m . reply qr . to_s
3771 end
72+ m . reply "That's all!"
3873 else
3974 m . reply "I haven't seen #{ nick } "
4075 end
0 commit comments