Skip to content

Commit 30ff380

Browse files
committed
Fix invoking handler
1 parent 04dbec3 commit 30ff380

File tree

2 files changed

+16
-6
lines changed

2 files changed

+16
-6
lines changed

cache/src/main/java/com/javadiscord/jdi/internal/cache/Cache.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ public CacheInterface<Object> getCacheForGuild(long guildId) {
2020
if (!cache.containsKey(guildId)) {
2121
return cache.put(guildId, getCacheForType());
2222
}
23-
return cache.get(guildId);
23+
return cache.get(guildId) == null
24+
? cache.put(guildId, getCacheForType())
25+
: cache.get(guildId);
2426
}
2527

2628
public boolean isGuildCached(long guildId) {

core/src/main/java/com/javadiscord/jdi/core/interaction/InteractionEventHandler.java

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
import java.lang.reflect.Method;
44
import java.lang.reflect.Parameter;
55
import java.util.ArrayList;
6+
import java.util.HashMap;
67
import java.util.List;
8+
import java.util.Map;
79

810
import com.javadiscord.jdi.core.Discord;
911
import com.javadiscord.jdi.core.EventListener;
@@ -19,6 +21,8 @@ public class InteractionEventHandler implements EventListener {
1921
private final Object slashCommandLoader;
2022
private final Discord discord;
2123

24+
private final Map<String, Object> cachedInstances = new HashMap<>();
25+
2226
public InteractionEventHandler(Object slashCommandLoader, Discord discord) {
2327
this.slashCommandLoader = slashCommandLoader;
2428
this.discord = discord;
@@ -46,7 +50,9 @@ public void onInteractionCreate(Interaction interaction, Guild guild) {
4650
(Class<?>) commandClassMethodInstance.getClass().getMethod("clazz")
4751
.invoke(commandClassMethodInstance);
4852

49-
Method method = commandClassMethodInstance.getClass().getMethod("method");
53+
Method method =
54+
(Method) commandClassMethodInstance.getClass().getMethod("method")
55+
.invoke(commandClassMethodInstance);
5056

5157
List<Object> paramOrder = new ArrayList<>();
5258
Parameter[] parameters = method.getParameters();
@@ -71,10 +77,12 @@ public void onInteractionCreate(Interaction interaction, Guild guild) {
7177
);
7278
}
7379

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+
}
7886

7987
} catch (Exception e) {
8088
LOGGER.error("Failed to invoke handler for /{}", command, e);

0 commit comments

Comments
 (0)