From 0bb3ae9273d6343660f9118579c589f8b5a740af Mon Sep 17 00:00:00 2001 From: dngfx <294904+dngfx@users.noreply.github.com> Date: Fri, 31 Aug 2018 14:10:15 +0100 Subject: [PATCH 1/9] Fix decoys stacking up and attempting to take over irc. --- modules/ducks.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/modules/ducks.py b/modules/ducks.py index 606819ad..5434f1ac 100644 --- a/modules/ducks.py +++ b/modules/ducks.py @@ -12,6 +12,7 @@ class Module(object): self.bot = bot self.events = events self.active_duck = 0 + self.decoy_hooked = 0 events.on("received.command.bef").hook(self.duck_bef, help="Befriend a duck!") @@ -199,7 +200,11 @@ class Module(object): channel = event["target"] next_decoy_time = self.decoy_time() - self.events.on("timer").on("duck-decoy").hook(self.duck_decoy) + + if self.decoy_hooked == 0: + self.events.on("timer").on("duck-decoy").hook(self.duck_decoy) + self.decoy_hooked = 1 + self.bot.add_timer("duck-decoy", next_decoy_time, None, None, False, channel=channel) From 0f1e683cd3684fc5bd66e3973ea21ca3a1fb5d28 Mon Sep 17 00:00:00 2001 From: dngfx <294904+dngfx@users.noreply.github.com> Date: Fri, 31 Aug 2018 14:16:27 +0100 Subject: [PATCH 2/9] Where on earth did this PHP relic come from? --- modules/ducks.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/ducks.py b/modules/ducks.py index 5434f1ac..62a6a317 100644 --- a/modules/ducks.py +++ b/modules/ducks.py @@ -117,7 +117,7 @@ class Module(object): event["stdout"].write( target + ", you've befriended " + str(befriended_ducks + 1) + " duck" + grammar + " in " + event[ - "target"].name); + "target"].name) next_duck_time = self.duck_time(event) self.bot.add_timer("duck-appear", next_duck_time, persist=False) From bce8cef524217e3a86558a9085468cc84af5c7d1 Mon Sep 17 00:00:00 2001 From: dngfx <294904+dngfx@users.noreply.github.com> Date: Fri, 31 Aug 2018 14:36:25 +0100 Subject: [PATCH 3/9] Migrate duck data to user_channel_settings. --- modules/ducks.py | 28 ++++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/modules/ducks.py b/modules/ducks.py index 62a6a317..aec604cf 100644 --- a/modules/ducks.py +++ b/modules/ducks.py @@ -21,8 +21,8 @@ class Module(object): events.on("received.command.decoy").hook(self.set_decoy, help="Be a sneaky fellow and make a decoy duck.") - # events.on("received.command.friends").hook(self.duck_friends, - # help="See who the friendliest people to ducks are!") + events.on("received.command.friends").hook(self.duck_friends, + help="See who the friendliest people to ducks are!") # events.on("received.command.killers").hook(self.duck_killers, # help="See who shoots the most amount of ducks.") # events.on("received.command.ducks").hook(self.duck_list, @@ -92,9 +92,14 @@ class Module(object): def decoy_time(self): return random.randint(10, 20) + def duck_friends(self, event): + pass def duck_bef(self, event): - target = event["user"].nickname + user = event["user"] + target = user.nickname + id = user.id + active_duck = event["target"].get_setting("active-duck", 0) active_duck = int(active_duck) if isinstance(active_duck, str) else active_duck @@ -108,9 +113,11 @@ class Module(object): else: event["stderr"].write("That user is not in this channel") else: - setting = "bef-ducks-%s-%s" % (target, event["target"].name) - befriended_ducks = event["server"].get_setting(setting, 0) - event["server"].set_setting(setting, befriended_ducks + 1) + befriended_ducks = event["target"].get_user_setting(id, + "ducks-befriended", 0) + event["target"].set_user_setting(id, + "ducks-befriended", + befriended_ducks + 1) event["target"].set_setting("active-duck", 0) grammar = "" if befriended_ducks == 0 else "s" @@ -123,7 +130,9 @@ class Module(object): self.bot.add_timer("duck-appear", next_duck_time, persist=False) def duck_bang(self, event): - target = event["user"].nickname + user = event["user"] + target = user.nickname + id = user.id if event["target"].get_setting("active-duck", 0) == 0: event["stderr"].set_prefix("Kick") if event["server"].has_user(target): @@ -134,9 +143,8 @@ class Module(object): else: event["stderr"].write("That user is not in this channel") else: - setting = "shot-ducks-%s-%s" % (target, event["target"].name) - shot_ducks = event["server"].get_setting(setting, 0) - event["server"].set_setting(setting, shot_ducks + 1) + shot_ducks = event["target"].get_user_setting(id, "ducks-shot", 0) + event["target"].set_user_setting(id, "ducks-shot", shot_ducks + 1) event["target"].set_setting("active-duck", 0) grammar = "" if shot_ducks == 0 else "s" From 5eaf4a8790fa05ce7f6f23ffc6a02740a125358c Mon Sep 17 00:00:00 2001 From: jesopo Date: Fri, 31 Aug 2018 15:13:56 +0100 Subject: [PATCH 4/9] Add Database.UserChannelSettings.find_all_by_setting --- Database.py | 20 ++++++++++++++++---- IRCChannel.py | 3 +++ 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/Database.py b/Database.py index c315d801..8ea22b9e 100644 --- a/Database.py +++ b/Database.py @@ -207,13 +207,25 @@ class UserChannelSettings(Table): def find_prefix(self, user_id, channel_id, prefix, default=[]): return self.find_user_settings(user_id, channel_id, "%s%" % prefix, default) - def find_by_setting(self, server_id, user_id, setting, default=[]): + def find_by_setting(self, user_id, setting, default=[]): values = self.database.execute_fetchall( """SELECT channels.name, user_channel_settings.value FROM user_channel_settings INNER JOIN channels ON user_channel_settings.channel_id=channels.channel_id - WHERE channels.server_id=? AND - user_channel_settings.user_id=?""", [server_id, user_id]) + WHERE user_channel_settings.setting=? + AND user_channel_settings.user_id=?""", [setting, user_id]) + if values: + for i, value in enumerate(values): + values[i] = value[0], json.loads(value[1]) + return values + return default + def find_all_by_setting(self, channel_id, setting, default=[]): + values = self.database.execute_fetchall( + """SELECT users.name, user_channel_settings.value FROM + user_channel_settings INNER JOIN users ON + user_channel_settings.user_id=users.user_id + WHERE user_channel_setting.setting=? + AND user_channel_settings.channel_id=?""", [setting, channel_id]) if values: for i, value in enumerate(values): values[i] = value[0], json.loads(value[1]) @@ -354,4 +366,4 @@ class Database(object): DELETE CASCADE, PRIMARY KEY (user_id, channel_id, setting))""") self.execute("""CREATE INDEX user_channel_settings_index - ON user_channel_settings (user_id, channel_id, setting)""") \ No newline at end of file + ON user_channel_settings (user_id, channel_id, setting)""") diff --git a/IRCChannel.py b/IRCChannel.py index 15d2e5df..11a5bf9e 100644 --- a/IRCChannel.py +++ b/IRCChannel.py @@ -99,6 +99,9 @@ class Channel(object): def del_user_setting(self, user_id, setting): self.bot.database.user_channel_settings.delete(user_id, self.id, setting) + def find_all_by_setting(self, setting, default=[]): + return self.bot.database.user_channel_settings.find_all_by_setting( + self.id, setting, default) def send_message(self, text, prefix=None): self.server.send_message(self.name, text, prefix=prefix) From edc0d9d391dbccb4c66b070c4910663b553c0db2 Mon Sep 17 00:00:00 2001 From: jesopo Date: Fri, 31 Aug 2018 15:17:15 +0100 Subject: [PATCH 5/9] Turns out we didn't need find_all_by_setting --- Database.py | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/Database.py b/Database.py index 8ea22b9e..80444cef 100644 --- a/Database.py +++ b/Database.py @@ -219,18 +219,6 @@ class UserChannelSettings(Table): values[i] = value[0], json.loads(value[1]) return values return default - def find_all_by_setting(self, channel_id, setting, default=[]): - values = self.database.execute_fetchall( - """SELECT users.name, user_channel_settings.value FROM - user_channel_settings INNER JOIN users ON - user_channel_settings.user_id=users.user_id - WHERE user_channel_setting.setting=? - AND user_channel_settings.channel_id=?""", [setting, channel_id]) - if values: - for i, value in enumerate(values): - values[i] = value[0], json.loads(value[1]) - return values - return default def delete(self, user_id, channel_id, setting): self.database.execute( """DELETE FROM user_channel_settings WHERE 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 6/9] 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) From 6e400a547049f707685bc7d6d8ff6419a848f42e Mon Sep 17 00:00:00 2001 From: dngfx <294904+dngfx@users.noreply.github.com> Date: Fri, 31 Aug 2018 16:06:11 +0100 Subject: [PATCH 7/9] Just duck things~ --- modules/ducks.py | 63 +++++++++++++++++++++++++++++++----------------- 1 file changed, 41 insertions(+), 22 deletions(-) diff --git a/modules/ducks.py b/modules/ducks.py index aec604cf..1ac284c9 100644 --- a/modules/ducks.py +++ b/modules/ducks.py @@ -15,14 +15,14 @@ class Module(object): self.decoy_hooked = 0 events.on("received.command.bef").hook(self.duck_bef, - help="Befriend a duck!") + help="Befriend a duck!") events.on("received.command.bang").hook(self.duck_bang, - help="Shoot a duck! Meanie.") + help="Shoot a duck! Meanie.") events.on("received.command.decoy").hook(self.set_decoy, - help="Be a sneaky fellow and make a decoy duck.") + help="Be a sneaky fellow and make a decoy duck.") events.on("received.command.friends").hook(self.duck_friends, - help="See who the friendliest people to ducks are!") + help="See who the friendliest people to ducks are!") # events.on("received.command.killers").hook(self.duck_killers, # help="See who shoots the most amount of ducks.") # events.on("received.command.ducks").hook(self.duck_list, @@ -58,7 +58,8 @@ class Module(object): for server in self.bot.servers.values(): for channel in server.channels.values(): ducks_enabled = channel.get_setting("ducks-enabled", 0) - ducks_enabled = int(ducks_enabled) if isinstance(ducks_enabled, str) else ducks_enabled + ducks_enabled = int(ducks_enabled) if isinstance(ducks_enabled, + str) else ducks_enabled min_time = "min-duck-time-%s" % channel.name max_time = "max-duck-time-%s" % channel.name @@ -66,8 +67,10 @@ class Module(object): min_duck_time = channel.get_setting("min-duck-time", 240) max_duck_time = channel.get_setting("max-duck-time", 1200) - min_duck_time = int(min_duck_time) if isinstance(min_duck_time, str) else min_duck_time - max_duck_time = int(max_duck_time) if isinstance(max_duck_time, str) else max_duck_time + min_duck_time = int(min_duck_time) if isinstance(min_duck_time, + str) else min_duck_time + max_duck_time = int(max_duck_time) if isinstance(max_duck_time, + str) else max_duck_time self.duck_times[min_time] = min_duck_time self.duck_times[max_time] = max_duck_time @@ -84,8 +87,10 @@ class Module(object): min = "min-duck-time-%s" % (channel_name) max = "max-duck-time-%s" % (channel_name) - self.bot.log.debug("Attempting to set %s to %s", [str(min), str(self.duck_times[min])]); - self.bot.log.debug("Attempting to set %s to %s", [str(max), str(self.duck_times[max])]); + self.bot.log.debug("Attempting to set %s to %s", + [str(min), str(self.duck_times[min])]); + self.bot.log.debug("Attempting to set %s to %s", + [str(max), str(self.duck_times[max])]); return random.randint(self.duck_times[min], self.duck_times[max]) @@ -93,7 +98,10 @@ class Module(object): return random.randint(10, 20) def duck_friends(self, event): - pass + friends = event["server"].find_all_user_channel_settings( + "ducks-befriended") + + print(friends) def duck_bef(self, event): user = event["user"] @@ -101,29 +109,33 @@ class Module(object): id = user.id active_duck = event["target"].get_setting("active-duck", 0) - active_duck = int(active_duck) if isinstance(active_duck, str) else active_duck + active_duck = int(active_duck) if isinstance(active_duck, + str) else active_duck if active_duck == 0: event["stderr"].set_prefix("Kick") if event["server"].has_user(target): if not event["server"].is_own_nickname(target): - event["target"].send_kick(target, "You tried befriending a non-existent duck. Creepy!") + event["target"].send_kick(target, + "You tried befriending a non-existent duck. Creepy!") else: event["stderr"].write("Nope.") else: event["stderr"].write("That user is not in this channel") else: befriended_ducks = event["target"].get_user_setting(id, - "ducks-befriended", 0) + "ducks-befriended", + 0) event["target"].set_user_setting(id, - "ducks-befriended", - befriended_ducks + 1) + "ducks-befriended", + befriended_ducks + 1) event["target"].set_setting("active-duck", 0) grammar = "" if befriended_ducks == 0 else "s" event["stdout"].write( - target + ", you've befriended " + str(befriended_ducks + 1) + " duck" + grammar + " in " + event[ + target + ", you've befriended " + str( + befriended_ducks + 1) + " duck" + grammar + " in " + event[ "target"].name) next_duck_time = self.duck_time(event) @@ -137,7 +149,8 @@ class Module(object): event["stderr"].set_prefix("Kick") if event["server"].has_user(target): if not event["server"].is_own_nickname(target): - event["target"].send_kick(target, "You tried shooting a non-existent duck. Creepy!") + event["target"].send_kick(target, + "You tried shooting a non-existent duck. Creepy!") else: event["stderr"].write("Nope.") else: @@ -150,7 +163,9 @@ class Module(object): grammar = "" if shot_ducks == 0 else "s" event["stdout"].write( - target + ", you've shot " + str(shot_ducks + 1) + " duck" + grammar + " in " + event["target"].name); + target + ", you've shot " + str( + shot_ducks + 1) + " duck" + grammar + " in " + event[ + "target"].name); next_duck_time = self.duck_time(event) self.bot.add_timer("duck-appear", next_duck_time, persist=False) @@ -159,13 +174,16 @@ class Module(object): for server in self.bot.servers.values(): for channel in server.channels.values(): ducks_enabled = channel.get_setting("ducks-enabled", 0) - ducks_enabled = int(ducks_enabled) if isinstance(ducks_enabled, str) else ducks_enabled + ducks_enabled = int(ducks_enabled) if isinstance(ducks_enabled, + str) else ducks_enabled if ducks_enabled == 0: continue - self.bot.log.info("Ducks enabled for %s: %s", [str(channel.name), str(ducks_enabled)]) + self.bot.log.info("Ducks enabled for %s: %s", + [str(channel.name), str(ducks_enabled)]) active_duck = channel.get_setting("active-duck", 0) - active_duck = int(active_duck) if isinstance(active_duck, str) else active_duck + active_duck = int(active_duck) if isinstance(active_duck, + str) else active_duck if ducks_enabled == 1 and active_duck == 0: ducks = [ @@ -189,7 +207,8 @@ class Module(object): channel.set_setting("active-duck", 0) next_duck_time = self.duck_time(channel.name) - self.bot.add_timer("duck-appear", next_duck_time, persist=False) + self.bot.add_timer("duck-appear", next_duck_time, + persist=False) def duck_decoy(self, event): ducks = [ From faff26f58a87aa5ea6194b652ee9d0bbc81dbd54 Mon Sep 17 00:00:00 2001 From: dongfix <294904+dngfx@users.noreply.github.com> Date: Fri, 31 Aug 2018 16:10:16 +0100 Subject: [PATCH 8/9] Update Database.py --- Database.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Database.py b/Database.py index a0aa3529..c344c71e 100644 --- a/Database.py +++ b/Database.py @@ -225,7 +225,7 @@ class UserChannelSettings(Table): 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 + INNER JOIN users on user_channel_settings.user_id=users.user_id WHERE user_channel_settings.setting=? AND users.server_id=?""", [setting, server_id]) if values: From 95d1475f2c62f04f818b3862491cc3599ed85639 Mon Sep 17 00:00:00 2001 From: dngfx <294904+dngfx@users.noreply.github.com> Date: Fri, 31 Aug 2018 16:58:25 +0100 Subject: [PATCH 9/9] .friends support! Yay, lets be friends. --- modules/ducks.py | 31 +++++++++++++++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) diff --git a/modules/ducks.py b/modules/ducks.py index 1ac284c9..bb386a28 100644 --- a/modules/ducks.py +++ b/modules/ducks.py @@ -1,3 +1,4 @@ +from operator import itemgetter import datetime import random @@ -101,7 +102,33 @@ class Module(object): friends = event["server"].find_all_user_channel_settings( "ducks-befriended") - print(friends) + friendliest = {} + friend_nicks = [] + friend_ducks = [] + + for i in friends: + if i[1] in friendliest.keys(): + friendliest[i[1]] += i[2] + else: + friendliest[i[1]] = i[2] + + for user, friends in sorted(friendliest.items(), key = itemgetter(1), + reverse = True): + friend_nicks.append(user) + friend_ducks.append(friends) + + sentence = "Friendliest Users -- " + + length = len(friend_nicks) if len(friend_nicks) < 11 else 11 + + for i in range(0, length): + sentence += friend_nicks[i] + " (" + str(friend_ducks[i]) + ")" + if i < 10: + sentence += ", " + + sentence = sentence[0:-2] + + event["target"].send_message(sentence) def duck_bef(self, event): user = event["user"] @@ -165,7 +192,7 @@ class Module(object): event["stdout"].write( target + ", you've shot " + str( shot_ducks + 1) + " duck" + grammar + " in " + event[ - "target"].name); + "target"].name) next_duck_time = self.duck_time(event) self.bot.add_timer("duck-appear", next_duck_time, persist=False)