added a new settings table for bot settings in general.

This commit is contained in:
jesopo 2016-04-14 16:44:27 +01:00
parent 390eff4c2b
commit 644b2ea630
No known key found for this signature in database
GPG key ID: 0BBDEB2AEFCFFCB3

View file

@ -11,6 +11,7 @@ class Database(object):
self.cursors = {} self.cursors = {}
self.make_servers_table() self.make_servers_table()
self.make_bot_settings_table()
self.make_server_settings_table() self.make_server_settings_table()
self.make_channel_settings_table() self.make_channel_settings_table()
self.make_user_settings_table() self.make_user_settings_table()
@ -30,6 +31,12 @@ class Database(object):
username TEXT, realname TEXT)""") username TEXT, realname TEXT)""")
except sqlite3.Error as e: except sqlite3.Error as e:
pass pass
def make_bot_settings_table(self):
try:
self.cursor().execute("""CREATE TABLE bot_settings
(setting TEXT PRIMARY KEY, value TEXT)""")
except sqlite3.Error as e:
pass
def make_server_settings_table(self): def make_server_settings_table(self):
try: try:
self.cursor().execute("""CREATE TABLE server_settings self.cursor().execute("""CREATE TABLE server_settings
@ -58,6 +65,29 @@ class Database(object):
except sqlite3.Error as e: except sqlite3.Error as e:
pass pass
def set_bot_setting(self, setting, value):
self.cursor().execute("""INSERT OR REPLACE INTO bot_settings
VALUES (?, ?)""", [setting.lower(), json.dumps(value)])
def get_bot_setting(self, setting, default=None):
self.cursor().execute("""SELECT value FROM bot_settings
WHERE setting=?""", [setting.lower()])
value = self.cursor().fetchone()
if value:
return json.loads(value[0])
return default
def find_bot_settings(self, pattern, default=[]):
self.cursor().execute("""SELECT setting, value FROM bot_settings
WHERE setting LIKE ?""", [pattern.lower()])
values = self.cursor().fetchall()
if values:
for i, value in enumerate(values):
values[i] = value[0], json.loads(value[1])
return values
return default
def del_bot_setting(self, setting):
self.cursor().execute("""DELETE FROM bot_settings WHERE
setting=?""", [setting.lower()])
def set_server_setting(self, server_id, setting, value): def set_server_setting(self, server_id, setting, value):
self.cursor().execute("""INSERT OR REPLACE INTO server_settings self.cursor().execute("""INSERT OR REPLACE INTO server_settings
VALUES (?, ?, ?)""", [server_id, setting.lower(), VALUES (?, ?, ?)""", [server_id, setting.lower(),