From b96499541927d81e25172c56aa94c4a6c474e6c1 Mon Sep 17 00:00:00 2001 From: dongfix <294904+dngfx@users.noreply.github.com> Date: Fri, 31 Aug 2018 15:49:57 +0100 Subject: [PATCH] All the settings (#11) * Add Database.UserChannelSettings.find_all_by_setting * Turns out we didn't need find_all_by_setting * Actually, we do need find_all_by_setting --- Database.py | 14 ++++++++++++++ IRCServer.py | 5 ++++- IRCUser.py | 2 +- 3 files changed, 19 insertions(+), 2 deletions(-) diff --git a/Database.py b/Database.py index 80444cef..a0aa3529 100644 --- a/Database.py +++ b/Database.py @@ -219,6 +219,20 @@ class UserChannelSettings(Table): values[i] = value[0], json.loads(value[1]) return values return default + def find_all_by_setting(self, server_id, setting, default=[]): + values = self.database.execute_fetchall( + """SELECT channels.name, users.nickname, + user_channel_settings.value FROM + user_channel_settings INNER JOIN channels ON + user_channel_settings.channel_id=channels.channel_id + INNER JOIN users on user_channel_setting.user_id=users.user_id + WHERE user_channel_settings.setting=? AND + users.server_id=?""", [setting, server_id]) + if values: + for i, value in enumerate(values): + values[i] = value[0], value[1], json.loads(value[2]) + return values + return default def delete(self, user_id, channel_id, setting): self.database.execute( """DELETE FROM user_channel_settings WHERE diff --git a/IRCServer.py b/IRCServer.py index 5ed33baf..34358e44 100644 --- a/IRCServer.py +++ b/IRCServer.py @@ -112,9 +112,12 @@ class Server(object): self.id, prefix, default) def del_setting(self, setting): self.bot.database.server_settings.delete(self.id, setting) - def get_all_user_settings(self, setting, default): + def get_all_user_settings(self, setting, default=[]): return self.bot.database.user_settings.find_all_by_setting( self.id, setting, default) + def find_all_user_channel_settings(self, setting, default=[]): + return self.bot.database.user_channel_settings.find_all_by_setting( + self.id, setting, default) def set_own_nickname(self, nickname): self.nickname = nickname diff --git a/IRCUser.py b/IRCUser.py index c99372db..ea54bf6b 100644 --- a/IRCUser.py +++ b/IRCUser.py @@ -39,7 +39,7 @@ class User(object): self.bot.database.user_settings.delete(self.id, setting) def get_channel_settings_per_setting(self, setting, default=[]): return self.bot.database.user_channel_settings.find_by_setting( - self.server.id, self.id, setting, default) + self.id, setting, default) def send_message(self, message, prefix=None): self.server.send_message(self.nickname, message, prefix=prefix)