From: kaliko Date: Fri, 17 Mar 2023 15:35:23 +0000 (+0100) Subject: Add helpers to add/remove handlers X-Git-Tag: 0.3.0~19 X-Git-Url: https://git.kaliko.me/?a=commitdiff_plain;h=3989896273f97b95cbb26fc87021870d778e03d8;p=sid.git Add helpers to add/remove handlers --- diff --git a/sid/plugin.py b/sid/plugin.py index b21cff9..efc9a14 100644 --- a/sid/plugin.py +++ b/sid/plugin.py @@ -31,6 +31,20 @@ class Plugin: def __init__(self, bot): self.bot = bot self.log = bot.log.getChild(self.__class__.__name__) + #: :py:obj:`list` : List of tuples (event, handler) + self.handlers = [] + + def add_handlers(self): + """Add handlers declared in self.hanlders""" + for event, handler in self.handlers: + self.log.debug(f'Add {event} > {self.__class__.__name__}().{handler.__name__}') + self.bot.add_event_handler(event, handler) + + def rm_handlers(self): + """Remove handlers declared in self.hanlders""" + for event, handler in self.handlers: + self.log.debug(f'Remove {event} > {self.__class__.__name__}().{handler.__name__}') + self.bot.del_event_handler(event, handler) def send(self, dest, msg, mtype='chat'): """Send msg to dest @@ -67,4 +81,5 @@ class Plugin: self.send(to, msg, mtype=rcv['type']) def shutdown(self): + """Empty method to override. Called on bot shutdown""" pass