Closed nickrobson closed 6 years ago
+1
Hey!
Things look pretty good, and I'd be happy to accept these changes. I just woke up, so I'll find time during the day today to look over the code and either approve it for merging or request changes. If possible, would you be willing to update the wiki if and when I accept this PR? Thanks!
TL;DR: Relatively minor revamp of how commands are created by API consumers.
Example Usage
The current method of adding command handlers involves:
Why do I think the current system is convoluted?
I personally do not see why listeners and handlers should be entirely separated in functionality. There is enough overlap between the two types where a handler could be a listener that when testing just executes the desired functionality and says "Yup! It passed me!"
Information on the new command filters API
This API merges the concepts of listeners and handlers into filters. Handlers should extend
CommandHandler
, and filters should extendCommandFilter
, though this is not enforced. It is however highly recommended that new filters add a constructor that has a super call providing children to the CommandFilter constructor:CommandFilter(CommandFilter... children)
. This allows for layering.I decided not to use
CommandFilter[]
as the parameter type to prevent horrifically verbose code such asnew CommandFilter(new CommandFilter[]{ new MentionFilter(), ... });
It comes with three filter implementations out of the box: TextFilter (matches text directly), RegexFilter (matches a regular expression), and MentionFilter (was the bot mentioned?).
I believe that this API simplifies the creation of more complex command handlers as they can be layered, instead of reinventing the wheel each time more complex functionality is required.
As evident in the implementations of the three provided filters, it is easy to implement new filters - the longest implementation is the MentionFilter which is two lines of code split up for legibility, the others are one line each.