diff --git a/bin/linkbot b/bin/linkbot index 1798258..8ce5da3 100755 --- a/bin/linkbot +++ b/bin/linkbot @@ -19,7 +19,8 @@ at_exit { options = { :console => false, :dev => false, - :config_path => File.expand_path(File.join(File.dirname(__FILE__), "..", "config", "config.json")) + :config_path => File.expand_path(File.join(File.dirname(__FILE__), "..", "config", "config.json")), + :database_path => File.expand_path(File.join(File.dirname(__FILE__), "..", "data.sqlite3")), } OptionParser.new do |opts| @@ -29,6 +30,10 @@ OptionParser.new do |opts| options[:config_path] = v end + opts.on("-D", "--database FILE", "Use the SQLite database at this file path") do |v| + options[:database_path] = v + end + opts.on("-k", "--console", "Run a local chat console for testing plugins") do |v| options[:console] = v end diff --git a/lib/linkbot.rb b/lib/linkbot.rb index e9364b6..e032392 100755 --- a/lib/linkbot.rb +++ b/lib/linkbot.rb @@ -17,6 +17,7 @@ class Bot def initialize(options) Linkbot::Config.load(options[:config_path]) + Linkbot.db(options[:database_path]) if options[:console] Linkbot::Config["connectors"] = [{ "type" => "console", "periodic" => true, "receive_broadcasts" => true}] diff --git a/lib/linkbot/db.rb b/lib/linkbot/db.rb index fb8c7e2..0e60ea8 100644 --- a/lib/linkbot/db.rb +++ b/lib/linkbot/db.rb @@ -2,12 +2,12 @@ require 'sqlite3' module Linkbot - def self.db - @@db ||= SQLite3::Database.new('data.sqlite3') + def self.db(database_path = 'data.sqlite3') + @@db ||= SQLite3::Database.new(database_path) @@db.type_translation = true @@db.busy_timeout(1000) end - + def self.load_users if Linkbot.db.table_info('users').empty? Linkbot.db.execute('CREATE TABLE users (user_id STRING, username TEXT, showname TEXT)'); @@ -16,15 +16,15 @@ def self.load_users @@user_ids = Hash[rows] @@users = Hash[rows.collect {|a,b| [b,a]}] end - + def self.users @@users end - + def self.user_ids @@user_ids end - + def self.user_exists?(user) rows = Linkbot.db.execute("select * from users where username = ?", user) if rows.empty? @@ -34,20 +34,20 @@ def self.user_exists?(user) true end end - + def self.username(user_id) Linkbot.db.execute("select username from users where user_id = ?", user_id)[0][0] end - + def self.user_id(username) Linkbot.db.execute("select user_id from users where username = ?", username)[0][0] end - + # Update a username based on the user_id - def self.update_user(username,user_id) + def self.update_user(username,user_id) Linkbot.db.execute("update users set username = ? where user_id = ?", username, user_id) end - + def self.add_user(username,user_id=nil) rows = Linkbot.db.execute("select * from users where username = ?", username) if rows.empty? @@ -63,6 +63,6 @@ def self.add_user(username,user_id=nil) Linkbot.db.execute("update users set user_id=? where username=?", user_id, username) end end - + end