From 68298bbccf715fb4d8c82e45a74c309a74d0c77e Mon Sep 17 00:00:00 2001 From: jesopo Date: Sun, 15 Jul 2018 15:15:52 +0100 Subject: [PATCH] Added EventHook.call_for_result to get a result or default from an event --- EventManager.py | 3 +++ modules/bitly.py | 4 ++-- modules/signals.py | 5 ++++- 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/EventManager.py b/EventManager.py index b93f170b..a5b7bd37 100644 --- a/EventManager.py +++ b/EventManager.py @@ -84,6 +84,9 @@ class EventHook(object): return self.get_child(subevent) + def call_for_result(self, default=None, max=None, **kwargs): + results = self.call(max=max, **kwargs) + return default if not len(results) else results[0] def call(self, max=None, **kwargs): event = Event(self.bot, self.name, **kwargs) if self._call_notify: diff --git a/modules/bitly.py b/modules/bitly.py index ac7835a1..f31ca347 100644 --- a/modules/bitly.py +++ b/modules/bitly.py @@ -25,8 +25,8 @@ class Module(object): return data["data"]["url"] def shorten(self, event): - link = self.bot.events.on("get").on("shortlink").call( - url=event["args"])[0] + link = self.bot.events.on("get").on("shortlink" + ).call_for_result(url=event["args"]) if link: event["stdout"].write("Short URL: %s" % link) else: diff --git a/modules/signals.py b/modules/signals.py index 79eab553..8f556e23 100644 --- a/modules/signals.py +++ b/modules/signals.py @@ -9,10 +9,13 @@ class Module(object): def SIGINT(self, signum, frame): print() self.bot.events.on("signal").on("interrupt").call(signum=signum, frame=frame) + for server in self.bot.servers.values(): - quote = self.bot.events.on("get.quit-quote").call()[0] + quote = self.bot.events.on("get.quit-quote" + ).call_for_result(default="Leaving") server.send_quit(quote) self.bot.register_write(server) + self.bot.running = False def SIGUSR1(self, signum, frame):