Added EventHook.call_for_result to get a result or default from an event

This commit is contained in:
jesopo 2018-07-15 15:15:52 +01:00
parent 06de65b1c1
commit 68298bbccf
3 changed files with 9 additions and 3 deletions

View file

@ -84,6 +84,9 @@ class EventHook(object):
return self.get_child(subevent) 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): def call(self, max=None, **kwargs):
event = Event(self.bot, self.name, **kwargs) event = Event(self.bot, self.name, **kwargs)
if self._call_notify: if self._call_notify:

View file

@ -25,8 +25,8 @@ class Module(object):
return data["data"]["url"] return data["data"]["url"]
def shorten(self, event): def shorten(self, event):
link = self.bot.events.on("get").on("shortlink").call( link = self.bot.events.on("get").on("shortlink"
url=event["args"])[0] ).call_for_result(url=event["args"])
if link: if link:
event["stdout"].write("Short URL: %s" % link) event["stdout"].write("Short URL: %s" % link)
else: else:

View file

@ -9,10 +9,13 @@ class Module(object):
def SIGINT(self, signum, frame): def SIGINT(self, signum, frame):
print() print()
self.bot.events.on("signal").on("interrupt").call(signum=signum, frame=frame) self.bot.events.on("signal").on("interrupt").call(signum=signum, frame=frame)
for server in self.bot.servers.values(): 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) server.send_quit(quote)
self.bot.register_write(server) self.bot.register_write(server)
self.bot.running = False self.bot.running = False
def SIGUSR1(self, signum, frame): def SIGUSR1(self, signum, frame):