Skip to content

Commit 734ed76

Browse files
committed
added sqlite
1 parent 7fa1cf1 commit 734ed76

File tree

4 files changed

+55
-7
lines changed

4 files changed

+55
-7
lines changed

Gemfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
source "http://rubygems.org"
22

33
gem "cinch"
4+
gem "sqlite3-ruby"

Gemfile.lock

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
GEM
2+
remote: http://rubygems.org/
3+
specs:
4+
cinch (1.0.1)
5+
sqlite3-ruby (1.3.1)
6+
7+
PLATFORMS
8+
ruby
9+
10+
DEPENDENCIES
11+
cinch
12+
sqlite3-ruby

db.sqlite3

4 KB
Binary file not shown.

robot.rb

Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,39 @@
11
require '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
737
end
838

939
bot = 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

Comments
 (0)