-
Notifications
You must be signed in to change notification settings - Fork 53
Working with Bot Events
In an effort to clean up the bot's code, event and plugin systems was introduced. It has since been expanded to allow pipeline-based processing for events, allowing the bot to transition from its brittle current worker-based system to this new architecture.
This is a brief overview of how this works:
-
The bot sets up the plugin manager.
-
The plugin manager loads all plugins in
/pluginsthat have not been excluded from running via the bot configuration. -
Each plugin imports the global event manager instance and registers its event handlers using
manager.on. All parameters to event handlers should be appropriately named and should use default arguments where appropriate. -
When the bot instance has an event occur, it fires
manager.fire_with_context, passing itself as thebotargument. -
The event manager looks up the event and triggers all associated event handlers in order of priority (lower numbers first, with default priority being 0), passing them the event name and bot context as named parameters along with any other arguments passed in by the bot. Only the arguments that the handler specifies are passed in.
-
Each handler performs its task. If it wishes to change the data in the event pipeline, it returns a dict with any updates arguments for the next handler to process. (ie. a filter function might take in an argument named
item_list; it would filter that list and then return{"item_list": filtered_item_list}. -
The event manager, after each function, updates its own dictionary of arguments passed to it and then calls the next handler with these arguments. If nothing was returned, then the dictionary remains intact and the next handler is called anyway.
-
This repeats until all handlers have been called.
For an example of this in action, see plugins/transfer_pokemon/__init__.py.