]> kaliko git repositories - sid.git/commitdiff
Add helpers to add/remove handlers
authorkaliko <kaliko@azylum.org>
Fri, 17 Mar 2023 15:35:23 +0000 (16:35 +0100)
committerkaliko <kaliko@azylum.org>
Fri, 17 Mar 2023 15:35:23 +0000 (16:35 +0100)
sid/plugin.py

index b21cff9e0897c556d0248abc3cd4ae2e68bcfb54..efc9a146c31f798eedb48cdb22e0d799b5a704f1 100644 (file)
@@ -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