Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
surajkumar committed May 29, 2024
1 parent deb8ac8 commit b880043
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 37 deletions.
12 changes: 12 additions & 0 deletions core/src/main/java/com/javadiscord/jdi/core/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,23 @@
public class Constants {
public static final String COMMAND_OPTION_CHOICE_ANNOTATION =
"com.javadiscord.jdi.core.annotations.CommandOptionChoice";

public static final String SLASH_COMMAND_ANNOTATION =
"com.javadiscord.jdi.core.annotations.SlashCommand";

public static final String LISTENER_LOADER_CLASS =
"com.javadiscord.jdi.core.processor.loader.ListenerLoader";
public static final String COMPONENT_LOADER_CLASS =
"com.javadiscord.jdi.core.processor.loader.ComponentLoader";
public static final String SLASH_COMMAND_LOADER_CLASS =
"com.javadiscord.jdi.core.processor.loader.SlashCommandLoader";

public static final String LAUNCH_HEADER = """
_ ____ ___
| | _ \\_ _| https://github.com/javadiscord/java-discord-api
_ | | | | | | Open-Source Discord Framework
| |_| | |_| | | GPL-3.0 license
\\___/|____/___| Version 1.0
""";

}
58 changes: 21 additions & 37 deletions core/src/main/java/com/javadiscord/jdi/core/Discord.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@
import com.javadiscord.jdi.core.api.builders.command.CommandOptionType;
import com.javadiscord.jdi.core.interaction.InteractionEventHandler;
import com.javadiscord.jdi.core.models.ready.ReadyEvent;
import com.javadiscord.jdi.internal.ReflectiveComponentLoader;
import com.javadiscord.jdi.internal.ReflectiveLoader;
import com.javadiscord.jdi.internal.ReflectiveSlashCommandClassMethod;
import com.javadiscord.jdi.internal.*;
import com.javadiscord.jdi.internal.api.DiscordRequest;
import com.javadiscord.jdi.internal.api.DiscordRequestDispatcher;
import com.javadiscord.jdi.internal.api.DiscordResponseFuture;
Expand Down Expand Up @@ -112,13 +110,7 @@ public Discord(String botToken, IdentifyRequest identifyRequest) {
}

public Discord(String botToken, IdentifyRequest identifyRequest, Cache cache) {
System.err.println("""
_ ____ ___
| | _ \\_ _| https://github.com/javadiscord/java-discord-api
_ | | | | | | Open-Source Discord Framework
| |_| | |_| | | GPL-3.0 license
\\___/|____/___| Version 1.0
""");
System.err.println(Constants.LAUNCH_HEADER);

this.botToken = botToken;
this.discordRequestDispatcher = new DiscordRequestDispatcher(botToken);
Expand Down Expand Up @@ -150,7 +142,7 @@ private void registerLoadedAnnotationsWithDiscord() {
for (Annotation annotation : annotations) {
if (
annotation.annotationType().getName()
.equals("com.javadiscord.jdi.core.annotations.SlashCommand")
.equals(Constants.SLASH_COMMAND_ANNOTATION)
) {
CommandBuilder builder = buildCommand(annotation);
createInteractionRequests.add(builder);
Expand Down Expand Up @@ -183,22 +175,18 @@ private CommandBuilder buildCommand(Annotation annotation) throws ReflectiveOper
private void addCommandOption(
CommandBuilder builder,
Object option
) throws ReflectiveOperationException {
Method optionNameMethod = option.getClass().getMethod("name");
String optionName = (String) optionNameMethod.invoke(option);

Method optionDescriptionMethod = option.getClass().getMethod("description");
String optionDescription = (String) optionDescriptionMethod.invoke(option);
) {

Method optionTypeMethod = option.getClass().getMethod("type");
Enum<?> optionType = (Enum<?>) optionTypeMethod.invoke(option);
String optionTypeValue = optionType.name();
ReflectiveCommandOption reflectiveCommandOption =
ReflectiveLoader.proxy(option, ReflectiveCommandOption.class);

Method optionRequiredMethod = option.getClass().getMethod("required");
boolean optionRequired = (boolean) optionRequiredMethod.invoke(option);
String optionName = reflectiveCommandOption.name();
String optionDescription = reflectiveCommandOption.description();
String optionTypeValue = reflectiveCommandOption.type().name();
boolean optionRequired = reflectiveCommandOption.required();

List<CommandOptionChoice> choices = new ArrayList<>();
Object[] choicesArray = (Object[]) option.getClass().getMethod("choices").invoke(option);
Object[] choicesArray = reflectiveCommandOption.choices();
for (Object choice : choicesArray) {
addCommandOptionChoice(choices, choice);
}
Expand All @@ -209,24 +197,21 @@ private void addCommandOption(
optionDescription,
CommandOptionType.fromName(optionTypeValue),
optionRequired
).addChoice(choices)
)
.addChoice(choices)
);
}

private void addCommandOptionChoice(
List<CommandOptionChoice> choices,
Object choice
) throws ReflectiveOperationException {
Annotation annotation1 = (Annotation) choice;
private void addCommandOptionChoice(List<CommandOptionChoice> choices, Object choice) {
Annotation annotation = (Annotation) choice;
if (
annotation1.annotationType().getName()
.equals(Constants.COMMAND_OPTION_CHOICE_ANNOTATION)
annotation.annotationType().getName().equals(Constants.COMMAND_OPTION_CHOICE_ANNOTATION)
) {
Method nameMethod1 = annotation1.annotationType().getMethod("name");
Method valueMethod1 = annotation1.annotationType().getMethod("value");
String name1 = (String) nameMethod1.invoke(annotation1);
String value1 = (String) valueMethod1.invoke(annotation1);
choices.add(new CommandOptionChoice(value1, name1));
ReflectiveCommandOptionChoice commandOptionChoice =
ReflectiveLoader.proxy(annotation, ReflectiveCommandOptionChoice.class);
choices.add(
new CommandOptionChoice(commandOptionChoice.value(), commandOptionChoice.name())
);
}
}

Expand Down Expand Up @@ -307,7 +292,6 @@ private void loadSlashCommands() {

public void start() {
started = true;

webSocketManager = new WebSocketManager(gatewaySetting, identifyRequest, cache);
WebSocketManagerProxy webSocketManagerProxy = new WebSocketManagerProxy(webSocketManager);
ConnectionDetails connectionDetails =
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.javadiscord.jdi.internal;

public interface ReflectiveCommandOption {
String name();

String description();

Enum<?> type();

Object[] choices();

boolean required();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.javadiscord.jdi.internal;

public interface ReflectiveCommandOptionChoice {
String name();

String value();
}

0 comments on commit b880043

Please sign in to comment.