· 6 years ago · Mar 11, 2019, 10:32 PM
1class Buttbot
2 include Cinch::Plugin
3 set :prefix, /^\./
4
5 DB = ENV["HOME"] + "/buttbot.db"
6
7 def initialize(*args)
8 @db = SQLite3::Database.new(DB)
9 @db.execute(
10 "CREATE TABLE IF NOT EXISTS buttbot(
11 id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
12 term TEXT NOT NULL,
13 value TEXT NOT NULL
14 )")
15 super
16 end
17
18 match(/r ([^ ]+) = (.+)$/, method: :remember)
19 def remember(m, term, val)
20 @db.execute("INSERT OR REPLACE INTO buttbot (term, value) VALUES (?, ?)", term, val)
21 m.reply("#{term} -> #{val}")
22 rescue => e
23 exception(e)
24 m.reply("Data corrupted, '#{term}' not found (lol fuckin rekt)")
25 end
26
27 # !<keyword>
28 match(/([^ ]+)$/, method: :lookup)
29 def lookup(m, term)
30 res = @db.get_first_value("SELECT value FROM buttbot WHERE term = ?", term)
31 m.reply(res) if res
32 end
33end