diff --git a/sopel_modules/SpiceBot/Commands.py b/sopel_modules/SpiceBot/Commands.py index 368e80c..2f00517 100644 --- a/sopel_modules/SpiceBot/Commands.py +++ b/sopel_modules/SpiceBot/Commands.py @@ -12,7 +12,7 @@ import threading import spicemanip from .Logs import logs -from .Database import botdb +from .Database import db as botdb class BotCommands(): diff --git a/sopel_modules/SpiceBot/Config.py b/sopel_modules/SpiceBot/Config.py index 0c66dc1..6d6508c 100644 --- a/sopel_modules/SpiceBot/Config.py +++ b/sopel_modules/SpiceBot/Config.py @@ -44,4 +44,4 @@ class BotConfig(): return None -botconfig = BotConfig() +config = BotConfig() diff --git a/sopel_modules/SpiceBot/Database.py b/sopel_modules/SpiceBot/Database.py index d5424e6..6edec2f 100644 --- a/sopel_modules/SpiceBot/Database.py +++ b/sopel_modules/SpiceBot/Database.py @@ -4,9 +4,11 @@ from __future__ import unicode_literals, absolute_import, division, print_functi This is the SpiceBot Database """ +from sopel.db import SopelDB from sopel.tools import Identifier import threading +from .Config import config as botconfig class BotDatabase(): @@ -14,7 +16,7 @@ class BotDatabase(): def __init__(self): self.lock = threading.Lock() - self.db = None + self.db = SopelDB(botconfig.config) self.dict = { "nicks": {}, "channels": {}, @@ -22,18 +24,18 @@ class BotDatabase(): """Nick""" - def get_nick_value(self, bot, nick, key, sorting_key='unsorted'): + def get_nick_value(self, nick, key, sorting_key='unsorted'): self.lock.acquire() nick = Identifier(nick) - nick_id = bot.db.get_nick_id(nick, create=True) + nick_id = self.db.get_nick_id(nick, create=True) if nick_id not in self.dict["nicks"].keys(): self.dict["nicks"][nick_id] = {} if sorting_key not in self.dict["nicks"][nick_id].keys(): - self.dict["nicks"][nick_id][sorting_key] = bot.db.get_nick_value(nick, sorting_key) or dict() + self.dict["nicks"][nick_id][sorting_key] = self.db.get_nick_value(nick, sorting_key) or dict() self.lock.release() @@ -42,55 +44,55 @@ class BotDatabase(): else: return self.dict["nicks"][nick_id][sorting_key][key] - def set_nick_value(self, bot, nick, key, value, sorting_key='unsorted'): + def set_nick_value(self, nick, key, value, sorting_key='unsorted'): self.lock.acquire() nick = Identifier(nick) - nick_id = bot.db.get_nick_id(nick, create=True) + nick_id = self.db.get_nick_id(nick, create=True) if nick_id not in self.dict["nicks"].keys(): self.dict["nicks"][nick_id] = {} if sorting_key not in self.dict["nicks"][nick_id].keys(): - self.dict["nicks"][nick_id][sorting_key] = bot.db.get_nick_value(nick, sorting_key) or dict() + self.dict["nicks"][nick_id][sorting_key] = self.db.get_nick_value(nick, sorting_key) or dict() self.dict["nicks"][nick_id][sorting_key][key] = value - bot.db.set_nick_value(nick, sorting_key, self.dict["nicks"][nick_id][sorting_key]) + self.db.set_nick_value(nick, sorting_key, self.dict["nicks"][nick_id][sorting_key]) self.lock.release() - def delete_nick_value(self, bot, nick, key, sorting_key='unsorted'): + def delete_nick_value(self, nick, key, sorting_key='unsorted'): self.lock.acquire() nick = Identifier(nick) - nick_id = bot.db.get_nick_id(nick, create=True) + nick_id = self.db.get_nick_id(nick, create=True) if nick_id not in self.dict["nicks"].keys(): self.dict["nicks"][nick_id] = {} if sorting_key not in self.dict["nicks"][nick_id].keys(): - self.dict["nicks"][nick_id][sorting_key] = bot.db.get_nick_value(nick, sorting_key) or dict() + self.dict["nicks"][nick_id][sorting_key] = self.db.get_nick_value(nick, sorting_key) or dict() del self.dict["nicks"][nick_id][sorting_key][key] - bot.db.set_nick_value(nick, sorting_key, self.dict["nicks"][nick_id][sorting_key]) + self.db.set_nick_value(nick, sorting_key, self.dict["nicks"][nick_id][sorting_key]) self.lock.release() - def adjust_nick_value(self, bot, nick, key, value, sorting_key='unsorted'): + def adjust_nick_value(self, nick, key, value, sorting_key='unsorted'): self.lock.acquire() nick = Identifier(nick) - nick_id = bot.db.get_nick_id(nick, create=True) + nick_id = self.db.get_nick_id(nick, create=True) if nick_id not in self.dict["nicks"].keys(): self.dict["nicks"][nick_id] = {} if sorting_key not in self.dict["nicks"][nick_id].keys(): - self.dict["nicks"][nick_id][sorting_key] = bot.db.get_nick_value(nick, sorting_key) or dict() + self.dict["nicks"][nick_id][sorting_key] = self.db.get_nick_value(nick, sorting_key) or dict() if key not in self.dict["nicks"][nick_id][sorting_key].keys(): oldvalue = [] @@ -101,11 +103,11 @@ class BotDatabase(): self.dict["nicks"][nick_id][sorting_key][key] = value else: self.dict["nicks"][nick_id][sorting_key][key] = oldvalue + value - bot.db.set_nick_value(nick, sorting_key, self.dict["nicks"][nick_id][sorting_key]) + self.db.set_nick_value(nick, sorting_key, self.dict["nicks"][nick_id][sorting_key]) self.lock.release() - def adjust_nick_list(self, bot, nick, key, entries, adjustmentdirection, sorting_key='unsorted'): + def adjust_nick_list(self, nick, key, entries, adjustmentdirection, sorting_key='unsorted'): self.lock.acquire() @@ -113,13 +115,13 @@ class BotDatabase(): entries = [entries] nick = Identifier(nick) - nick_id = bot.db.get_nick_id(nick, create=True) + nick_id = self.db.get_nick_id(nick, create=True) if nick_id not in self.dict["nicks"].keys(): self.dict["nicks"][nick_id] = {} if sorting_key not in self.dict["nicks"][nick_id].keys(): - self.dict["nicks"][nick_id][sorting_key] = bot.db.get_nick_value(nick, sorting_key) or dict() + self.dict["nicks"][nick_id][sorting_key] = self.db.get_nick_value(nick, sorting_key) or dict() if key not in self.dict["nicks"][nick_id][sorting_key].keys(): self.dict["nicks"][nick_id][sorting_key][key] = [] @@ -132,23 +134,23 @@ class BotDatabase(): for entry in entries: while entry in self.dict["nicks"][nick_id][sorting_key][key]: self.dict["nicks"][nick_id][sorting_key][key].remove(entry) - bot.db.set_nick_value(nick, sorting_key, self.dict["nicks"][nick_id][sorting_key]) + self.db.set_nick_value(nick, sorting_key, self.dict["nicks"][nick_id][sorting_key]) self.lock.release() """Channels""" - def get_channel_value(self, bot, channel, key, sorting_key='unsorted'): + def get_channel_value(self, channel, key, sorting_key='unsorted'): self.lock.acquire() - channel = Identifier(channel) + channel = Identifier(channel).lower().lower() if channel not in self.dict["channels"].keys(): self.dict["channels"][channel] = {} if sorting_key not in self.dict["channels"][channel].keys(): - self.dict["channels"][channel][sorting_key] = bot.db.get_channel_value(channel, sorting_key) or dict() + self.dict["channels"][channel][sorting_key] = self.db.get_channel_value(channel, sorting_key) or dict() self.lock.release() @@ -157,51 +159,51 @@ class BotDatabase(): else: return self.dict["channels"][channel][sorting_key][key] - def set_channel_value(self, bot, channel, key, value, sorting_key='unsorted'): + def set_channel_value(self, channel, key, value, sorting_key='unsorted'): self.lock.acquire() - channel = Identifier(channel) + channel = Identifier(channel).lower() if channel not in self.dict["channels"].keys(): self.dict["channels"][channel] = {} if sorting_key not in self.dict["channels"][channel].keys(): - self.dict["channels"][channel][sorting_key] = bot.db.get_channel_value(channel, sorting_key) or dict() + self.dict["channels"][channel][sorting_key] = self.db.get_channel_value(channel, sorting_key) or dict() self.dict["channels"][channel][sorting_key][key] = value - bot.db.set_channel_value(channel, sorting_key, self.dict["channels"][channel][sorting_key]) + self.db.set_channel_value(channel, sorting_key, self.dict["channels"][channel][sorting_key]) self.lock.release() - def delete_channel_value(self, bot, channel, key, sorting_key='unsorted'): + def delete_channel_value(self, channel, key, sorting_key='unsorted'): self.lock.acquire() - channel = Identifier(channel) + channel = Identifier(channel).lower() if channel not in self.dict["channels"].keys(): self.dict["channels"][channel] = {} if sorting_key not in self.dict["channels"][channel].keys(): - self.dict["channels"][channel][sorting_key] = bot.db.get_channel_value(channel, sorting_key) or dict() + self.dict["channels"][channel][sorting_key] = self.db.get_channel_value(channel, sorting_key) or dict() del self.dict["channels"][channel][sorting_key][key] - bot.db.set_channel_value(channel, sorting_key, self.dict["channels"][channel][sorting_key]) + self.db.set_channel_value(channel, sorting_key, self.dict["channels"][channel][sorting_key]) self.lock.release() - def adjust_channel_value(self, bot, channel, key, value, sorting_key='unsorted'): + def adjust_channel_value(self, channel, key, value, sorting_key='unsorted'): self.lock.acquire() - channel = Identifier(channel) + channel = Identifier(channel).lower() if channel not in self.dict["channels"].keys(): self.dict["channels"][channel] = {} if sorting_key not in self.dict["channels"][channel].keys(): - self.dict["channels"][channel][sorting_key] = bot.db.get_channel_value(channel, sorting_key) or dict() + self.dict["channels"][channel][sorting_key] = self.db.get_channel_value(channel, sorting_key) or dict() if key not in self.dict["channels"][channel][sorting_key].keys(): oldvalue = None @@ -212,24 +214,24 @@ class BotDatabase(): self.dict["channels"][channel][sorting_key][key] = value else: self.dict["channels"][channel][sorting_key][key] = oldvalue + value - bot.db.set_channel_value(channel, sorting_key, self.dict["channels"][channel][sorting_key]) + self.db.set_channel_value(channel, sorting_key, self.dict["channels"][channel][sorting_key]) self.lock.release() - def adjust_channel_list(self, bot, channel, key, entries, adjustmentdirection, sorting_key='unsorted'): + def adjust_channel_list(self, channel, key, entries, adjustmentdirection, sorting_key='unsorted'): self.lock.acquire() if not isinstance(entries, list): entries = [entries] - channel = Identifier(channel) + channel = Identifier(channel).lower() if channel not in self.dict["channels"].keys(): self.dict["channels"][channel] = {} if sorting_key not in self.dict["channels"][channel].keys(): - self.dict["channels"][channel][sorting_key] = bot.db.get_channel_value(channel, sorting_key) or dict() + self.dict["channels"][channel][sorting_key] = self.db.get_channel_value(channel, sorting_key) or dict() if key not in self.dict["channels"][channel][sorting_key].keys(): self.dict["channels"][channel][sorting_key][key] = [] @@ -242,9 +244,119 @@ class BotDatabase(): for entry in entries: while entry in self.dict["channels"][channel][sorting_key][key]: self.dict["channels"][channel][sorting_key][key].remove(entry) - bot.db.set_channel_value(channel, sorting_key, self.dict["channels"][channel][sorting_key]) + self.db.set_channel_value(channel, sorting_key, self.dict["channels"][channel][sorting_key]) + + self.lock.release() + + """Plugins""" + + def get_plugin_value(self, plugin, key, sorting_key='unsorted'): + + self.lock.acquire() + + plugin = plugin.lower() + + if plugin not in self.dict["plugins"].keys(): + self.dict["plugins"][plugin] = {} + + if sorting_key not in self.dict["plugins"][plugin].keys(): + self.dict["plugins"][plugin][sorting_key] = self.db.get_plugin_value(plugin, sorting_key) or dict() + + self.lock.release() + + if key not in self.dict["plugins"][plugin][sorting_key].keys(): + return None + else: + return self.dict["plugins"][plugin][sorting_key][key] + + def set_plugin_value(self, plugin, key, value, sorting_key='unsorted'): + + self.lock.acquire() + + plugin = plugin.lower() + + if plugin not in self.dict["plugins"].keys(): + self.dict["plugins"][plugin] = {} + + if sorting_key not in self.dict["plugins"][plugin].keys(): + self.dict["plugins"][plugin][sorting_key] = self.db.get_plugin_value(plugin, sorting_key) or dict() + + self.dict["plugins"][plugin][sorting_key][key] = value + self.db.set_plugin_value(plugin, sorting_key, self.dict["plugins"][plugin][sorting_key]) + + self.lock.release() + + def delete_plugin_value(self, plugin, key, sorting_key='unsorted'): + + self.lock.acquire() + + plugin = plugin.lower() + + if plugin not in self.dict["plugins"].keys(): + self.dict["plugins"][plugin] = {} + + if sorting_key not in self.dict["plugins"][plugin].keys(): + self.dict["plugins"][plugin][sorting_key] = self.db.get_plugin_value(plugin, sorting_key) or dict() + + del self.dict["plugins"][plugin][sorting_key][key] + self.db.set_plugin_value(plugin, sorting_key, self.dict["plugins"][plugin][sorting_key]) + + self.lock.release() + + def adjust_plugin_value(self, plugin, key, value, sorting_key='unsorted'): + + self.lock.acquire() + + plugin = plugin.lower() + + if plugin not in self.dict["plugins"].keys(): + self.dict["plugins"][plugin] = {} + + if sorting_key not in self.dict["plugins"][plugin].keys(): + self.dict["plugins"][plugin][sorting_key] = self.db.get_plugin_value(plugin, sorting_key) or dict() + + if key not in self.dict["plugins"][plugin][sorting_key].keys(): + oldvalue = None + else: + oldvalue = self.dict["plugins"][plugin][sorting_key][key] + + if not oldvalue: + self.dict["plugins"][plugin][sorting_key][key] = value + else: + self.dict["plugins"][plugin][sorting_key][key] = oldvalue + value + self.db.set_plugin_value(plugin, sorting_key, self.dict["plugins"][plugin][sorting_key]) + + self.lock.release() + + def adjust_plugin_list(self, plugin, key, entries, adjustmentdirection, sorting_key='unsorted'): + + self.lock.acquire() + + if not isinstance(entries, list): + entries = [entries] + + plugin = plugin.lower() + + if plugin not in self.dict["plugins"].keys(): + self.dict["plugins"][plugin] = {} + + if sorting_key not in self.dict["plugins"][plugin].keys(): + self.dict["plugins"][plugin][sorting_key] = self.db.get_plugin_value(plugin, sorting_key) or dict() + + if key not in self.dict["plugins"][plugin][sorting_key].keys(): + self.dict["plugins"][plugin][sorting_key][key] = [] + + if adjustmentdirection == 'add': + for entry in entries: + if entry not in self.dict["plugins"][plugin][sorting_key][key]: + self.dict["plugins"][plugin][sorting_key][key].append(entry) + elif adjustmentdirection == 'del': + for entry in entries: + while entry in self.dict["plugins"][plugin][sorting_key][key]: + self.dict["plugins"][plugin][sorting_key][key].remove(entry) + self.db.set_plugin_value(plugin, sorting_key, self.dict["plugins"][plugin][sorting_key]) self.lock.release() -botdb = BotDatabase() +db = BotDatabase() diff --git a/sopel_modules/SpiceBot/Prerun.py b/sopel_modules/SpiceBot/Prerun.py index 6f4a137..a0cb4f3 100644 --- a/sopel_modules/SpiceBot/Prerun.py +++ b/sopel_modules/SpiceBot/Prerun.py @@ -13,7 +13,7 @@ import spicemanip from .Tools import command_permissions_check from .Commands import commands -from .Database import botdb +from .Database import db as botdb class BotPrerun(): @@ -106,7 +106,7 @@ class BotPrerun(): # don't run commands that are disabled in channels if not trigger.is_privmsg: - disabled_list = botdb.get_channel_value(bot, trigger.sender, 'disabled_commands', 'commands') or {} + disabled_list = botdb.get_channel_value(trigger.sender, 'disabled_commands', 'commands') or {} if trigger.sb["realcom"] in disabled_list.keys(): reason = disabled_list[trigger.sb["realcom"]]["reason"] timestamp = disabled_list[trigger.sb["realcom"]]["timestamp"]