1212import de .derioo .javautils .discord .command .parsed .ParsedCommand ;
1313import de .derioo .javautils .discord .command .parsed .parser .CronExtractor ;
1414import de .derioo .javautils .discord .command .parsed .parser .DateExtractor ;
15+ import de .derioo .javautils .discord .command .reciever .ReceiveContext ;
1516import de .derioo .javautils .discord .command .reciever .Receiver ;
1617import kotlin .Pair ;
1718import lombok .extern .java .Log ;
1819import net .dv8tion .jda .api .events .message .MessageReceivedEvent ;
20+ import org .jetbrains .annotations .Contract ;
1921import org .jetbrains .annotations .NotNull ;
2022
2123import java .lang .reflect .InvocationTargetException ;
@@ -36,19 +38,18 @@ public PrefixedReceiver(CommandManager commandManager) {
3638
3739 @ Override
3840 public boolean receive (MessageReceivedEvent event ) {
39- for (ParsedCommand command : getCommandManager ()
40- . getCommands ()) {
41+ for (ParsedCommand command : getCommandManager (). getCommands ()) {
42+ List < Object > args = new ArrayList <>();
4143 try {
4244 if (!(command .getReceiver () instanceof PrefixedReceiver )) return false ;
4345 if (!event .getMessage ().getContentRaw ().startsWith (command .getPrefix ()))
44- return false ; //TODO @Hello test 12:06
45- String after = event .getMessage ().getContentRaw ().replaceFirst (command .getPrefix (), "" ).trim (); //TODO test 12:06
46+ return false ;
47+ String after = event .getMessage ().getContentRaw ().replaceFirst (command .getPrefix (), "" ).trim ();
4648
47- boolean matches = true ;
4849 for (ParsedCommand .ParsedArgument argument : command .getArguments ()) {
4950 Pair <Boolean , List <Object >> booleanListPair = checkIfArgumentCouldMatch (argument , after );
51+ args = booleanListPair .getSecond ();
5052 if (!booleanListPair .getFirst ()) {
51- matches = false ;
5253 break ;
5354 }
5455 List <Object > parameters = new ArrayList <>();
@@ -72,11 +73,23 @@ public boolean receive(MessageReceivedEvent event) {
7273 }
7374 return true ;
7475 }
75- } catch (Exception e ) {
76+ } catch (Throwable e ) {
7677 try {
7778 Method method = command .getCommand ().getClass ().getDeclaredMethod ("catcher" );
7879 method .setAccessible (true );
79- method .invoke (command .getCommand ());
80+ List <Object > params = new ArrayList <>();
81+ for (Parameter parameter : method .getParameters ()) {
82+ if (parameter .getType ().isInstance (Throwable .class )) {
83+ params .add (e );
84+ continue ;
85+ }
86+ if (parameter .getType ().isAssignableFrom (ReceiveContext .class )) {
87+ params .add (new ReceiveContext (command , args ));
88+ continue ;
89+ }
90+ params .add (null );
91+ }
92+ method .invoke (command .getCommand (), params .toArray (Object []::new ));
8093 } catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException ex ) {
8194 throw new RuntimeException (ex );
8295 }
@@ -86,7 +99,8 @@ public boolean receive(MessageReceivedEvent event) {
8699 return false ;
87100 }
88101
89- private Pair <Boolean , List <Object >> checkIfArgumentCouldMatch (ParsedCommand .@ NotNull ParsedArgument argument , String after ) {
102+ @ Contract ("_, _ -> new" )
103+ private @ NotNull Pair <Boolean , List <Object >> checkIfArgumentCouldMatch (ParsedCommand .@ NotNull ParsedArgument argument , String after ) {
90104 boolean matches = true ;
91105 List <Object > args = new ArrayList <>();
92106 String current = after ;
0 commit comments