Extending Listener base class
So I wanted to extend the Listener base class by running some code for every listener before the actual
run
handler is invoked. Ideally I'd like to shim it somehow so existing listeners still just implement run
so removing or adding more shims like this is easily possible without renaming methods implemented in the listener itself.13 Replies
I am expecting this just isnt possible with how inheritance works and would require changes to the Listener class of saphhire
isn't this just what you want? Then you extend
MyCustomListener
in your pieces that need to run the code and extends Listener
for those that dontyeah but this doesnt work cause super has no run method as its an abstract method
oh wait it overrides the run
yeah no still limited due to abstract not being callable/accessible
oh wait right yeah well then you dont need to call the super at all.
just extend the class then make your class extend your extended class
yeah that would fully override the run of the custom listener though
edited
ah yeah the super call is what i wanna sort of avoid as it would mean duplicate code again (i could just call another function the without having a custom listener). i guess ill just have a different abstract function in my custom class and run that one from its implementation of
run
and then ill just have to compomise with thatyou technically could override our internal _run method for it, but be careful
yeah no thats smth id not want to do personally. they are internal for good reasons usually.
might be something nice to have though as an extendable optional feature to have pre and post functions on the listener class which can be handled by a custom listener class
just keep in mind that if we add that, then an error in prerun or postrun wont end up in ListenerError https://github.com/sapphiredev/framework/blob/4cc93a925916346bd713dff1e015dc0092e10bcf/src/lib/structures/Listener.ts#L98-L101
we'd get PreListenerError and PostListenerError
yeah that would be fine though as it also means that its easier to know if the error is from the listener itself or a pre/post condition
only thing I'm doubting about now is how far we want to take this. For example do we want all
Piece
s to support it. And if we do, then how do we handle messageRun
vs chatInputRun
vs contextMenuRun
for commands.commands already have pre and post events
realistically, all _run does is call your actual run method and emit listenererror if an error is thrown