diff --git a/Database.py b/Database.py index 3e0a11e7..5024f558 100644 --- a/Database.py +++ b/Database.py @@ -270,7 +270,7 @@ class Database(object): return default def find_user_channel_settings(self, server_id, channel, nickname, pattern, default=[]): - values = self.cursor().execute( + values = self.execute_fetchall( """SELECT setting, value FROM user_channel_settings WHERE server_id=? AND channel=? AND nickname=? AND setting LIKE '?'""", [server_id, channel.lower(), nickname.lower(), pattern.lower()]) @@ -283,6 +283,17 @@ class Database(object): prefix, default=[]): return self.find_user_settings(server_id, nickname, "%s%" % prefix, default) + def get_user_channel_settings_per_setting(self, server_id, nickname, + setting, default=[]): + values = self.execute_fetchall( + """SELECT channel, value FROM user_channel_settings WHERE + server_id=? AND nickname=? AND setting=?""", + [server_id, nickname.lower(), setting]) + if values: + for i, value in enumerate(values): + values[i] = value[0], json.loads(value[1]) + return values + return default def del_user_channel_setting(self, server_id, channel, nickname, setting): self.execute( """DELETE FROM user_channel_settings WHERE diff --git a/IRCUser.py b/IRCUser.py index dc3b71df..45981f87 100644 --- a/IRCUser.py +++ b/IRCUser.py @@ -34,6 +34,9 @@ class User(object): def del_setting(self, setting): self.bot.database.del_user_setting(self.server.id, self.nickname, setting) + def get_channel_settings_per_setting(self, setting, default=[]): + return self.bot.database.get_user_channel_settings_per_setting( + self.server.id, self.nickname, setting, default) def send_message(self, message, prefix=None): self.server.send_message(self.nickname, message, prefix=prefix) diff --git a/modules/words.py b/modules/words.py index bc864daa..68971f24 100644 --- a/modules/words.py +++ b/modules/words.py @@ -22,11 +22,11 @@ class Module(object): words = list(filter(None, event["message_split"])) word_count = len(words) - user_words = event["user"].get_setting("words", {}) - if not event["channel"].name in user_words: - user_words[event["channel"].name] = 0 - user_words[event["channel"].name] += word_count - event["user"].set_setting("words", user_words) + user_words = event["channel"].get_user_setting( + event["user"].nickname, "words", 0) + user_words += word_count + event["channel"].set_user_setting(event["user"].nickname, + "words", user_words) tracked_words = set(event["server"].get_setting( "tracked-words", [])) @@ -43,8 +43,10 @@ class Module(object): ][0]) else: target = event["user"] - words = target.get_setting("words", {}) + words = dict(target.get_channel_settings_per_setting( + "words", [])) this_channel = words.get(event["target"].name, 0) + total = 0 for channel in words: total += words[channel]