Adds onAttach event.

This still needs ~~a lot of~~ some work.

– ~~The build system needs to be updated~~ (no it doesn't)
– ~~Tests need to be added~~
– The code needs cleaned up. I did it pretty fast
– Docs

=====

This adds `onAttach`. Ref #1856 for more about why this event is important.
I think it could be useful to people. I can't come up with any reason to make it internal, really.