Skip to content

Commit b880043

Browse files
committed
Refactoring
1 parent deb8ac8 commit b880043

File tree

4 files changed

+53
-37
lines changed

4 files changed

+53
-37
lines changed

core/src/main/java/com/javadiscord/jdi/core/Constants.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,23 @@
33
public class Constants {
44
public static final String COMMAND_OPTION_CHOICE_ANNOTATION =
55
"com.javadiscord.jdi.core.annotations.CommandOptionChoice";
6+
7+
public static final String SLASH_COMMAND_ANNOTATION =
8+
"com.javadiscord.jdi.core.annotations.SlashCommand";
9+
610
public static final String LISTENER_LOADER_CLASS =
711
"com.javadiscord.jdi.core.processor.loader.ListenerLoader";
812
public static final String COMPONENT_LOADER_CLASS =
913
"com.javadiscord.jdi.core.processor.loader.ComponentLoader";
1014
public static final String SLASH_COMMAND_LOADER_CLASS =
1115
"com.javadiscord.jdi.core.processor.loader.SlashCommandLoader";
1216

17+
public static final String LAUNCH_HEADER = """
18+
_ ____ ___
19+
| | _ \\_ _| https://github.com/javadiscord/java-discord-api
20+
_ | | | | | | Open-Source Discord Framework
21+
| |_| | |_| | | GPL-3.0 license
22+
\\___/|____/___| Version 1.0
23+
""";
24+
1325
}

core/src/main/java/com/javadiscord/jdi/core/Discord.java

Lines changed: 21 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,7 @@
2222
import com.javadiscord.jdi.core.api.builders.command.CommandOptionType;
2323
import com.javadiscord.jdi.core.interaction.InteractionEventHandler;
2424
import com.javadiscord.jdi.core.models.ready.ReadyEvent;
25-
import com.javadiscord.jdi.internal.ReflectiveComponentLoader;
26-
import com.javadiscord.jdi.internal.ReflectiveLoader;
27-
import com.javadiscord.jdi.internal.ReflectiveSlashCommandClassMethod;
25+
import com.javadiscord.jdi.internal.*;
2826
import com.javadiscord.jdi.internal.api.DiscordRequest;
2927
import com.javadiscord.jdi.internal.api.DiscordRequestDispatcher;
3028
import com.javadiscord.jdi.internal.api.DiscordResponseFuture;
@@ -112,13 +110,7 @@ public Discord(String botToken, IdentifyRequest identifyRequest) {
112110
}
113111

114112
public Discord(String botToken, IdentifyRequest identifyRequest, Cache cache) {
115-
System.err.println("""
116-
_ ____ ___
117-
| | _ \\_ _| https://github.com/javadiscord/java-discord-api
118-
_ | | | | | | Open-Source Discord Framework
119-
| |_| | |_| | | GPL-3.0 license
120-
\\___/|____/___| Version 1.0
121-
""");
113+
System.err.println(Constants.LAUNCH_HEADER);
122114

123115
this.botToken = botToken;
124116
this.discordRequestDispatcher = new DiscordRequestDispatcher(botToken);
@@ -150,7 +142,7 @@ private void registerLoadedAnnotationsWithDiscord() {
150142
for (Annotation annotation : annotations) {
151143
if (
152144
annotation.annotationType().getName()
153-
.equals("com.javadiscord.jdi.core.annotations.SlashCommand")
145+
.equals(Constants.SLASH_COMMAND_ANNOTATION)
154146
) {
155147
CommandBuilder builder = buildCommand(annotation);
156148
createInteractionRequests.add(builder);
@@ -183,22 +175,18 @@ private CommandBuilder buildCommand(Annotation annotation) throws ReflectiveOper
183175
private void addCommandOption(
184176
CommandBuilder builder,
185177
Object option
186-
) throws ReflectiveOperationException {
187-
Method optionNameMethod = option.getClass().getMethod("name");
188-
String optionName = (String) optionNameMethod.invoke(option);
189-
190-
Method optionDescriptionMethod = option.getClass().getMethod("description");
191-
String optionDescription = (String) optionDescriptionMethod.invoke(option);
178+
) {
192179

193-
Method optionTypeMethod = option.getClass().getMethod("type");
194-
Enum<?> optionType = (Enum<?>) optionTypeMethod.invoke(option);
195-
String optionTypeValue = optionType.name();
180+
ReflectiveCommandOption reflectiveCommandOption =
181+
ReflectiveLoader.proxy(option, ReflectiveCommandOption.class);
196182

197-
Method optionRequiredMethod = option.getClass().getMethod("required");
198-
boolean optionRequired = (boolean) optionRequiredMethod.invoke(option);
183+
String optionName = reflectiveCommandOption.name();
184+
String optionDescription = reflectiveCommandOption.description();
185+
String optionTypeValue = reflectiveCommandOption.type().name();
186+
boolean optionRequired = reflectiveCommandOption.required();
199187

200188
List<CommandOptionChoice> choices = new ArrayList<>();
201-
Object[] choicesArray = (Object[]) option.getClass().getMethod("choices").invoke(option);
189+
Object[] choicesArray = reflectiveCommandOption.choices();
202190
for (Object choice : choicesArray) {
203191
addCommandOptionChoice(choices, choice);
204192
}
@@ -209,24 +197,21 @@ private void addCommandOption(
209197
optionDescription,
210198
CommandOptionType.fromName(optionTypeValue),
211199
optionRequired
212-
).addChoice(choices)
200+
)
201+
.addChoice(choices)
213202
);
214203
}
215204

216-
private void addCommandOptionChoice(
217-
List<CommandOptionChoice> choices,
218-
Object choice
219-
) throws ReflectiveOperationException {
220-
Annotation annotation1 = (Annotation) choice;
205+
private void addCommandOptionChoice(List<CommandOptionChoice> choices, Object choice) {
206+
Annotation annotation = (Annotation) choice;
221207
if (
222-
annotation1.annotationType().getName()
223-
.equals(Constants.COMMAND_OPTION_CHOICE_ANNOTATION)
208+
annotation.annotationType().getName().equals(Constants.COMMAND_OPTION_CHOICE_ANNOTATION)
224209
) {
225-
Method nameMethod1 = annotation1.annotationType().getMethod("name");
226-
Method valueMethod1 = annotation1.annotationType().getMethod("value");
227-
String name1 = (String) nameMethod1.invoke(annotation1);
228-
String value1 = (String) valueMethod1.invoke(annotation1);
229-
choices.add(new CommandOptionChoice(value1, name1));
210+
ReflectiveCommandOptionChoice commandOptionChoice =
211+
ReflectiveLoader.proxy(annotation, ReflectiveCommandOptionChoice.class);
212+
choices.add(
213+
new CommandOptionChoice(commandOptionChoice.value(), commandOptionChoice.name())
214+
);
230215
}
231216
}
232217

@@ -307,7 +292,6 @@ private void loadSlashCommands() {
307292

308293
public void start() {
309294
started = true;
310-
311295
webSocketManager = new WebSocketManager(gatewaySetting, identifyRequest, cache);
312296
WebSocketManagerProxy webSocketManagerProxy = new WebSocketManagerProxy(webSocketManager);
313297
ConnectionDetails connectionDetails =
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.javadiscord.jdi.internal;
2+
3+
public interface ReflectiveCommandOption {
4+
String name();
5+
6+
String description();
7+
8+
Enum<?> type();
9+
10+
Object[] choices();
11+
12+
boolean required();
13+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.javadiscord.jdi.internal;
2+
3+
public interface ReflectiveCommandOptionChoice {
4+
String name();
5+
6+
String value();
7+
}

0 commit comments

Comments
 (0)