| @@ 2-49 (lines=48) @@ | ||
| 1 | from event import Event |
|
| 2 | class BaseModule(object): |
|
| 3 | """ |
|
| 4 | A base module class for deriving modules (anything you fine folk write, probably) to inherit from. |
|
| 5 | The nice this is this allows you to define your own post_init and handle functions. |
|
| 6 | ||
| 7 | In your module's post_init, define and register your own events, and pass your module in. |
|
| 8 | ||
| 9 | .. code-block:: python |
|
| 10 | ||
| 11 | def MyModule(BaseModule): |
|
| 12 | def post_init(self): |
|
| 13 | e = Event("__wee__") |
|
| 14 | e.define("foo") |
|
| 15 | self.bot.register_event(e,self) |
|
| 16 | ||
| 17 | ||
| 18 | Bam, you've got the things you need (a bot handle, mostly) and by extending BaseModuleyou implement the right things to be called without error. |
|
| 19 | Elzar. |
|
| 20 | """ |
|
| 21 | def __init__(self, events=None, printer_handle=None, bot=None, say=None): |
|
| 22 | """ |
|
| 23 | This is called by load_modules for every .py in the modules directory. Your module must implement this, one way or the other, either directly, |
|
| 24 | by implementing this function signature, or indirectly, by inheriting from BaseModule. I suggest the latter. |
|
| 25 | """ |
|
| 26 | self.events = events |
|
| 27 | self.printer = printer_handle |
|
| 28 | self.interests = [] |
|
| 29 | self.bot = bot |
|
| 30 | self.say = say |
|
| 31 | ||
| 32 | # IMPORTANT: you must subscribe to events before you add your own below, or you'll subscribe twice. |
|
| 33 | # register ourself for any events that we're interested in that exist already |
|
| 34 | for event in events: |
|
| 35 | if event._type in self.interests: |
|
| 36 | event.subscribe(self) |
|
| 37 | ||
| 38 | self.help = None |
|
| 39 | ||
| 40 | self.post_init() |
|
| 41 | ||
| 42 | def handle(self, event): |
|
| 43 | pass |
|
| 44 | ||
| 45 | def post_init(self): |
|
| 46 | """ |
|
| 47 | Called after init is set up and builds out our basic module's needs. Allows you to do your own post-processing when inheriting from BaseModule. |
|
| 48 | """ |
|
| 49 | pass |
|
| 50 | ||
| @@ 1-20 (lines=20) @@ | ||
| 1 | class Dance: |
|
| 2 | def __init__(self, events=None, printer_handle=None, bot=None, say=None): |
|
| 3 | self.events = events |
|
| 4 | self.printer = printer_handle |
|
| 5 | self.interests = ['__.dance__'] |
|
| 6 | self.bot = bot |
|
| 7 | ||
| 8 | self.help = ".dance" |
|
| 9 | ||
| 10 | for event in events: |
|
| 11 | if event._type in self.interests: |
|
| 12 | event.subscribe(self) |
|
| 13 | ||
| 14 | def handle(self, event): |
|
| 15 | try: |
|
| 16 | self.printer("PRIVMSG " + event.channel + " : :D-\-< " + '\n') |
|
| 17 | self.printer("PRIVMSG " + event.channel + " : :D-|-< " + '\n') |
|
| 18 | self.printer("PRIVMSG " + event.channel + " : :D-/-< " + '\n') |
|
| 19 | except TypeError: |
|
| 20 | pass |
|
| 21 | ||