Config: Rework commands, tasks, and special handlers to run through the internal event system
Created by: agraubert
Commands should be moved to run like events as follows:
- The message content starts with a known command (say
!command
) -
self.dispatch('!command', message, content)
is run:- 'before:!command' is dispatched
- '!command' is dispatched
- The
add_command()
decorator should now attach some permissions logic to the function and then subscribe it to the '!command' event. - Careful this means that listeners of the '!command' event will run even if the user doesn't have permissions
- The
- 'after:!command' is dispatched
Tasks and special handlers should also run in a similar manner, with their triggering system dispatching 'task:' or 'special:' as appropriate and the decorated handlers being assigned to the respective event. In the case of tasks and special handlers, I think 'before:' and 'after:' should be run only if the task or handler will be run, not every time they are checked (unlike commands which run 'before:' and 'after:' even if the user doesn't have permissions).
In general:
- The main event should not be subscribed to (ie: directly subscribing to '!command', 'task:some_task', or 'special:some_handler') unless it is required that your event listener run in that group. Note that listeners to these events should not depend on the main event handler having been run. It is preferred that you use 'before:' if you do not require that the main event handler has been run, or 'after:' if you do.
- Be careful with permissions when subscribing to command events. 'before:!command' and 'after:!command' are run even if the user doesn't have permissions. Listeners directly subscribing to '!command' will also be run without a permissions check. Only
add_command()
will set the permissions to be checked