Skip to content

Commit

Permalink
Merge pull request #92 from robbkidd/accept-a-path-to-database
Browse files Browse the repository at this point in the history
add command line option for setting path to bot database
  • Loading branch information
robbkidd committed Sep 25, 2016
2 parents ad7676e + d90746c commit 8ba4275
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 13 deletions.
7 changes: 6 additions & 1 deletion bin/linkbot
Original file line number Diff line number Diff line change
Expand Up @@ -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|
Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions lib/linkbot.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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}]
Expand Down
24 changes: 12 additions & 12 deletions lib/linkbot/db.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)');
Expand All @@ -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?
Expand All @@ -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?
Expand All @@ -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

0 comments on commit 8ba4275

Please sign in to comment.