3
3
import java .lang .reflect .Method ;
4
4
import java .lang .reflect .Parameter ;
5
5
import java .util .ArrayList ;
6
+ import java .util .HashMap ;
6
7
import java .util .List ;
8
+ import java .util .Map ;
7
9
8
10
import com .javadiscord .jdi .core .Discord ;
9
11
import com .javadiscord .jdi .core .EventListener ;
@@ -19,6 +21,8 @@ public class InteractionEventHandler implements EventListener {
19
21
private final Object slashCommandLoader ;
20
22
private final Discord discord ;
21
23
24
+ private final Map <String , Object > cachedInstances = new HashMap <>();
25
+
22
26
public InteractionEventHandler (Object slashCommandLoader , Discord discord ) {
23
27
this .slashCommandLoader = slashCommandLoader ;
24
28
this .discord = discord ;
@@ -46,7 +50,9 @@ public void onInteractionCreate(Interaction interaction, Guild guild) {
46
50
(Class <?>) commandClassMethodInstance .getClass ().getMethod ("clazz" )
47
51
.invoke (commandClassMethodInstance );
48
52
49
- Method method = commandClassMethodInstance .getClass ().getMethod ("method" );
53
+ Method method =
54
+ (Method ) commandClassMethodInstance .getClass ().getMethod ("method" )
55
+ .invoke (commandClassMethodInstance );
50
56
51
57
List <Object > paramOrder = new ArrayList <>();
52
58
Parameter [] parameters = method .getParameters ();
@@ -71,10 +77,12 @@ public void onInteractionCreate(Interaction interaction, Guild guild) {
71
77
);
72
78
}
73
79
74
- method .invoke (commandClassMethodInstance , paramOrder .toArray ());
75
-
76
- Object handlerInstance = handler .getDeclaredConstructor ().newInstance ();
77
- method .invoke (handlerInstance );
80
+ if (cachedInstances .containsKey (handler .getName ())) {
81
+ method .invoke (cachedInstances .get (handler .getName ()), paramOrder .toArray ());
82
+ } else {
83
+ Object handlerInstance = handler .getDeclaredConstructor ().newInstance ();
84
+ method .invoke (handlerInstance , paramOrder .toArray ());
85
+ }
78
86
79
87
} catch (Exception e ) {
80
88
LOGGER .error ("Failed to invoke handler for /{}" , command , e );
0 commit comments